views:

779

answers:

2

I wrote three programs that modifies the registry in Windows Mobile to install and remove a todayscreen plugin for debugging purposes. They worked great for a while, but one by one they have suddenly been giving "UnauthorizedAccessException"s.

See the code for two of the programs below (note that the following code just sits directly in Main, so it runs and then the program terminates)...

RegistryInit.exe:

RegistryKey CustomItem = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"Software\Microsoft\Today\Items\TodayLauncher");
CustomItem.SetValue("Type", 4, RegistryValueKind.DWord);
CustomItem.SetValue("Enabled", 1, RegistryValueKind.DWord);
CustomItem.SetValue("Options", 1, RegistryValueKind.DWord);
CustomItem.SetValue("DLL", @"\Program Files\TodayLauncher\TodayLauncher.dll", RegistryValueKind.String);
CustomItem.SetValue("Config", @"\Program Files\TodayLauncher\Settings.cfg", RegistryValueKind.String);
CustomItem.SetValue("Selectability", 1, RegistryValueKind.DWord);

SendMessage((IntPtr)HWND_BROADCAST, WM_WININICHANGE, 0xF2, 0);

RegistryClear:

Microsoft.Win32.Registry.LocalMachine.DeleteSubKey(@"Software\Microsoft\Today\Items\TodayLauncher");
SendMessage((IntPtr)HWND_BROADCAST, WM_WININICHANGE, 0xF2, 0);

The third program was a configuration program that had options to add and remove the registry keys using exact copies of the code above, but it never quite worked. For a while it could run the code for "RegistryInit", but that didn't last long. Now the original "RegistryClear" program doesn't work, giving the same UnauthorizedAccessException. I find it very weird that these programs worked find for a while, then suddenly have stopped working.

Any ideas?

A: 

I see you don't call RegistryKey.Close() (i.e. CustomItem.Close() in your example), so therefore the key might still be open and not flushed to disk, hence you get the UnAuthorizedAccessException?

tomlog
A: 

One change I would make is to change "DeleteSubKey" to "DeleteSubKeyTree" as "DeleteSubKey" will fail if there are any sub keys under the key you are deleting.

Have you tried to write them as a native application in C? See if you get the same problems.

Shane Powell