11

To pop up the UAC dialog in Vista when writing to the HKLM registry hive, we opt to not use the Win32 Registry API, as when Vista permissions are lacking, we'd need to relaunch our entire application with administrator rights. Instead, we do this trick:

ShellExecute(hWnd, "runas" /* display UAC prompt on Vista */, windir + "\\Reg", "add HKLM\\Software\\Company\\KeyName /v valueName /t REG_MULTI_TZ /d ValueData", NULL, SW_HIDE);

This solution works fine, besides that our application is a 32-bit one, and it runs the REG.EXE command as it would be a 32-bit app using the WOW compatibility layer! :( If REG.EXE is ran from the command line, it's properly ran in 64-bit mode. This matters, because if it's ran as a 32-bit app, the registry keys will end up in the wrong place due to registry reflection.

So is there any way to launch a 64-bit app programmatically from a 32-bit app and not have it run using the WOW64 subsystem like its parent 32-bit process (i.e. a "*" suffix in the Task Manager)?

Jonas
  • 1,172
  • 1
  • 16
  • 26

4 Answers4

14

try this (from a 32bit process):

> %WINDIR%\sysnative\reg.exe query ...

(found that here).

akira
  • 6,050
  • 29
  • 37
  • If this works, that sounds like a nice trick to have in one's bag. – Jason R. Coombs May 10 '10 at 11:04
  • @DavidHeffernan: So you trap the failure and try again using System32. – Ben Voigt Dec 28 '11 at 23:51
  • 1
    @DavidHeffernan There's a hotfix that makes sysnative work on XP64: [A 32-bit application cannot access the system32 folder on a computer that is running a 64-bit version of Windows Server 2003 or of Windows XP](http://support.microsoft.com/kb/942589) – azhrei Aug 06 '13 at 12:31
9

Whether a 32-bit or 64-bit native (unmanaged) program is run depends solely on the executable. There are two copies of reg.exe, in C:\Windows\System32 (64-bit) and C:\Windows\SysWOW64 (32-bit). Because you don't specify a path, you're getting whatever appears first in the PATH environment variable, which is the 32-bit version for a 32-bit process.

You should really factor this function out into a separate program or COM object, and mark the program with a manifest, or launch the COM object using the COM elevation moniker.

Mike Dimmick
  • 9,662
  • 2
  • 23
  • 48
  • 1
    Don't forget that due to file system redirection (or whatever MS calls it), a 32-bit process that tries to open/exec "c:\Windows\System32\foo.exe" will have that file open/exec silently redirected to "C:\windows\SysWow64\foo.exe" unless specific measures are taken to disable the redirection. – Michael Burr Oct 02 '08 at 20:08
  • Yep Mike, that's what I noticed after I marked that as an "answer". That is -- I actually *had* written the full "System32" path in my test, so it should have picked the 64-bit version anyway, but didn't. I assumed it was for the reason you point out. :) – Jonas Oct 02 '08 at 21:22
  • The "whatever appears first in the PATH" comment isn't quite right. The PATH says system32 whether it is 32 or 64 bit. When calling ShellExecute from a 32 bit process you get file system redirection to syswow64. It's not as if you could swap the order in which the system directory appears in your PATH variable and change the behaviour!! – David Heffernan Dec 17 '10 at 18:22
  • I don't think this answer will work, for the reasons michael suggests. @Jonas you can "un accept" it by clicking the checkmark again – rogerdpack May 30 '12 at 16:23
2

Have you considered creating a small "helper" application to make the registry update for you? If you compile it to 64bit and include a manifest that indicates it requires administrator rights, then it'll cover both bases for you.

There are API's to detect the "bitness" of the OS you're running on so you could, conceivably, compile both RegistryUpdate32.exe and RegistryUpdate64.exe and call the relevant one.

Rob
  • 45,296
  • 24
  • 122
  • 150
1

One thing that I've done as a solution for myself is to PInvoke disabling redirection:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365744(v=vs.85).aspx

You can always turn it right back on.

ymerej
  • 727
  • 1
  • 8
  • 21