Tales From The Geek Side

The geeky musings of Greg Rowe.

Archive for the 'Code' Category

Regenerating Large Mailman Archives

September 3rd, 2007 by greg

In the fall of 2005 a server that I operated was broken into. The perpetrators defaced every index.* file on the system including those in the archives of a mailing list that I’ve run since December of 2005. Recently it came to my attention that those files were overlooked in the cleanup process. I checked my backup CDs from 2005 and found that I did not have any backups that could be used to restore the defaced archives. So I did what anyone would do, I searched for ways to regenerate the archive. Mailman includes tools to regenerate archives but there are some issues.

Read the rest of this entry »

Category: Code, Geek, Site, Software, Tips | No Comments »

Incrementing Version Numbers

August 14th, 2007 by greg

Dealing with software version numbers is a pain. If you try to do it manually you will invariably end up forgetting to update the version information in your source code or forget to commit the changes. Having programatic access to version information is helpful too.

Read the rest of this entry »

Category: Code, Geek, Tips | No Comments »

Visual Studio 2005 Express SP1 Deployment Woes

April 29th, 2007 by greg

This past Friday at work I faced more Microsoft Visual Studio 2005 Express deployment woes. My IT department pushed Service Pack 1 for Visual Studio 2005 Express to my machine. I never pay attention to what is pushed to my machine. Even if I had known I wouldn’t have though twice about my deployment procedures.

I sent out a new version of a software package for internal use. I heard reports back that it didn’t work — it would complain about “invalid application configurations.” …Whatever that’s supposed to mean. MS really has the WORST error messages I’ve ever seen. What’s really happening when that message appears is that the overly complex Windows dynamic linker is not finding one or more DLLs that are required to run the application. Microsoft developed a new method of linking in DLLs which is more complex than the previous method (look in app directory, look in path). I know that the new linker finds dependency information in an embedded manifest but I do not know how to look at that information once the code has been built. Had MS included meaningful error messages or even a reasonable way of finding the dependencies on a DLL or EXE I wouldn’t have wasted the hours that I did on Friday.

If you install Visual Studio 2005 Express service pack 1 you must use a new VC redist package. Credit goes to here for the information I needed to resolve this problem. The new redist is nearly impossible to find on Microsoft’s website. The new redist is available here.

Category: Code, Geek, Tips | No Comments »

New Version of tcl-dox Released

April 26th, 2007 by greg

Nigel Rook found and fixed a bug related to namespace handling. I’ve applied his patch and done some very minor cleanup of existing files. The new release is here.

Category: Code, Geek | No Comments »

Using sizeof in a smart way

February 20th, 2007 by greg

It seems that very few people comprehend how powerful C and C++’s sizeof operator really is. People insist on using sizeof on typenames. Folks, you don’t have to do that!

Typical code:

struct foo *f = malloc(sizeof(struct foo));

The way it should be written:

struct foo *f = malloc(sizeof(*f));

As you can see in the example above you can even dereference a pointer to get the correct size. What about dynamically allocating an array? That’s elegantly written like this:

int *array = malloc(sizeof(array[0]) * 44);

Using variables instead of types with sizeof is simply more robust. If you change the type of a variable you do not need to change your use of sizeof in anyway.

Generally speaking sizeof operates strictly at compile time. That means that anything that the compiler knows the size of will work with the sizeof operator. With C99 there is at least one case that I know of where sizeof is actually dynamic. In C99 you can declare “dynamic” arrays.

int dyn(int numElements) { int dynArray[ numElements]; int x = sizeof(dynArray); }

The example above will not compile in C++ and in most C compiler unless the C compiler supports C99. In the example above there is no way for the compiler to know the size of dynArray at compile time because numElements isn’t known until execution of the function. Regardless, with a C99 compliant compiler the code above will produce correct results.

So go forth and use sizeof on variable names, not on type names!

* Sorry about the crappy formatting. I’m battling the rich editor built into wordpress.

Category: Code, Geek | No Comments »

“Virtual” Serial Ports in win32

December 10th, 2006 by greg

Working with a serial (rs232) port should be an easy thing. Generally you open a serial port as though it were a file, set a few parameters, and you are on your way. This is true on Linux and it’s true on Win32 as well. There’s one gotcha that you need to watch out for. When you open the serial port with CreateFile you must prefix the serial port name with “\\.\”. The gotcha is that if you open com1 through com4 without that prefix everything will work fine. If you try to open a higher numbered port then you’ll get a “file not found” sort of error. I think the English error text on Windows is “The system could not find the file specified.” You should open every port with the prefix as it is the correct operation. I suspect that com1-com4 works only due to backwards compatibility.

I figured this out thanks to this post.

Category: Code, Geek | No Comments »

Win32 is STUPID

November 15th, 2006 by greg

I would like to tear apart the CreateService function call. It takes “only” 13 parameters. Here is some code I just wrote:

svc = CreateService(scm,
SERVICE_NAME,
SERVICE_NAME,
SC_MANAGER_CREATE_SERVICE,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START, // for now require manual
// starting.
SERVICE_ERROR_NORMAL,
binPath,       // Path to exe.
NULL,          // load order doesn't matter
NULL,          // tag order doesn't matter
NULL,          // no dependencies
NULL,          // use local system account
NULL);          // No password needed

The first parameter is a handle to a service control manager which you have to open with another function call that takes only three parameters (that’s reasonable) one of which is a sensible bitflag parameter.

The next 2 parameters are services names and display names. I think these should be the same. Having two makes little sense.

The next parameter specifies what sort of access you want to give the service. I think it would be better if a sensible default were used and a function provided to change the default, maybe ServiceSetAccess().

The next parameter is bit flags specifying what you really want to do. Did you know you can install a kernel driver using CreateService? Sure why not? I mean, who really needs KernelDriverInstall()? That wouldn’t make any sense at all when you can just put a flag in a call that already exists.

The next parameter is the start type. I’m OK with this parameter.

The next parameter is stupid. It specifies what you want the program that starts your program to do if your service errors out. WTF? Why should my service dictate the action of another program? stupid.

The next parameter is a full path to the service executable.

The next THREE parameters can all be NULL or you can specify 14 billion options for dependencies among other services, etc, etc. stupid. Why is this in this function? Why not have ServiceSetDependencies? One of these options is a “Pointer to a double null-terminated array of null-separated names of services or load ordering groups that the system must start before this service.” That’s nice and simple.

The last two parameters are to run the service as a different user. I disagree with providing a password since that means that the system needs to store the password and if the password changes for the user account then the service will not run. In addition this would be better as a different function, maybe ServiceSetUser(). Since a “super user” is starting the services that user ought to have enough privilege to switch to a different user context.

Truly I understand why there are so many crappy programmers out there when you see ridiculous code from a well respected company like Microsoft.

I’m honestly a bit surprised that the win32 API isn’t this:

BOOL win32(LPCSTR lpctstrszNotUsed, …);

CreateService Reference from MSDN

Category: Code, Geek | No Comments »

VIA Rhine PXE boot driver fix

October 31st, 2006 by greg

If you PXE boot a machine that uses the via-rhine driver in Linux you will find that you can boot it only once. After that first boot you have to physically remove power to the box to restore the network controller to the proper state. Well, at least that is the behavior I see with my VIA M10000 board.

As a workaround I commented out the following line in via-rhine.c on or around line 1969

iowrite8(ioread8(ioaddr + StickyHW) | 0x03, ioaddr + StickyHW);

Ahh, once again my Myth frontend reboots!

Category: Code, Geek, MythTV | No Comments »

Tcl-Dox 0.5 released

October 17th, 2006 by greg

Based on feedback from a few users I’ve released a new version of Tcl-Dox. Changes and fixes include:

  • Correct handling of end-of-line on UNIX and Windows now.
  • Vastly improved documentation including examples
  • Corrected errors in the README file
  • Corrected errors in the example Doxyfile
  • Corrected build warnings (fixing a potential issue on 64 bit machines)

Thank you to the users who wrote with suggestions, questions, and feedback.

Category: Code, Geek | 2 Comments »

Tcl Doxygen filter updated

September 1st, 2006 by greg

I’ve posted an updated Doxygen filter for TCL code. I fixed a number of bugs related to OO extensions for TCL. Upon a users request I’ve also included a Doxyfile (doxygen config file) that works with the filter. From this point on I’m going to call the program tcl dox.

Category: Code, Geek | 2 Comments »