Josh Josh - 1 month ago 13
C++ Question

Open 64 bit regedit from 32 bit application

I'm trying to use ShellExecute to open the 64 bit Regedit from my 32 bit application.

I noticed in Process Explorer that if I open Regedit normally, it says the image is 64 bit, but if I open

C:\Windows\Regedit.exe
from my 32 bit application with ShellExecute, Process Explorer says the image is 32 bit.

(and it does open the regedit in the Windows directory, not in SysWOW64)

I've found that if I use the
Wow64DisableWow64FsRedirection
function before calling ShellExecute, it opens the 64 bit image of it. But my application won't run on 32 bit XP.

What puzzles me is regardless of which way I open regedit, they both reside in
C:\Windows
, and are both the same executable. How can the same executable have 2 different image types? And how can I open the 64 bit one without
Wow64DisableWow64FsRedirection
?

Answer

You need to detect if you are in a 64 bit process with Is64BitProcess, if so, access %windir%\Sysnative as that points to the "Real" System32 folder for when 32 bit applications need to access the 64 bit System32 folder.

string system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "system32");
if(Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
{
    // For 32-bit processes on 64-bit systems, %windir%\system32 folder
    // can only be accessed by specifying %windir%\sysnative folder.
    system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "sysnative");

}
Comments