Friday, February 23, 2007

COM Registration Under Microsoft Windows Vista

Steve Trefethen and I have both been working on Windows Vista and I must say there is not one redeemable feature of Windows Vista over Windows XP. I keep hearing how it looks like Mac OSX, but the only resemblance I've been able to find is when minimizing apps they slide into the taskbar.

Enough of my complaining, I had a real issue to discuss in this post, COM Registration. Chances are you will need to use COM objects when you start developing on Windows Vista and there is a real problem that UAC has created. You cannot register a COM object unless you are running as Administrator. I don't mean you have Administrator privileges, I mean the process is running under the Administrator account (root for you Linux heads). Things you took for granted in previous Windows versions like running regedit.exe from a command prompt like I normally do fails with "Access denied". You must run it from the Start Search menu item (similar to the old Run menu item), then grant access for the process to run. Most likely you will have to create a shortcut to 4NT or CMD and right click on it and choose "Run as administrator".

So now let's look at registering your COM object. Take midas.dll as an example. It was written a lone time ago, before UAC. If in a non Administrator privileged command prompt you type "tregsvr midas.dll" you get "Call to DllRegisterServer was successful!", but it actually wasn't. So what happened? tregsvr doesn't have privileges to touch the registry so the DllRegisterServer in midas.dll failed. The implementation of DllRegisterServer in midas.dll could do potential harm to your system so Windows doesn't allow it access to the registry calls and there are sufficient checking down on those calls for DLLRegisterServer to return an error to tregsvr so the tregsvr reports a success. This can happen with any COM DLL so you can't rely on a call to tregsvr (or regsvr32) like in the good old days. Pre UAC COM objects need to be tested and verified and always registered in from an Administrator privileged account. This means that features in Delphi you've grown to know and love like Run | Register ActiveX Server will fail under Vista because of UAC.

4 comments:

Ondrej Kelle said...

Oops! Bummer.

I've found another little quirk: If you've written your own setup program you'll only be able to launch it under Administrator account (after confirming the UAC prompt) but then the process runs under that account, so things like writing to HKEY_CURRENT_USER don't work for the current user anymore.

The funny part is that this only happens for executables which contain the string "setup" in their file name and/or version information.

The workaround is to rename the executable and remove "setup" strings from the file version information. In case you'd like to stay compatible with all your manuals which say something like "launch Setup from the CD" you can still provide a Setup.cmd or a shortcut named "Setup" to launch your executable.

Sounds like an "interesting" security feature designed to break your existing code. Took me a while to discover, too. ;-)

Chris Bensen said...

I was going to talk about this in a future blog post. If a file name contains "setup", "install" or "update" it will automatically invoke the Vista elevation to run the program with administrative permissions/privileges when a user runs it. There are other ways of elevating a process.

Anonymous said...

Gentlemen, thank you.

I just bought a new 'xxxx' with Vista home premium installed. I promptly installed D6 (I have later versions however I have a lot of Apps I wrote on it so that's where I began). I have had a 'merry chase' getting things working. Most of my apps use TClientDataSet so they did not work because the midas.dll would not load. Anyway, your article has saved the day. I was about to 'exercise the wrath of God' on Vista. I have lost enough production time on this. Is there any plan for CodeGear to publish a comprehensive guide to fixing these problems for Dx ? Is there a programmatic work around ? Possible to patch D6 ? Anyway thanks again ;-)

Chris Bensen said...

Hi Nicole,

There are no plans that I am aware of to retrofit or produce a guide for older products about running on Vista. That is what the newer products are for :)

Post a Comment