How to get 64bit system folder in a 32bit application?

2.9k Views Asked by At

In a 32bit application, I have to copy a file to the 64bit system folder
(C:\Windows\System32\ instead of C:\Windows\SysWOW64\)

For this, I tried to get the folder using the WinAPI function SHGetKnownFolderPath with parameter FOLDERID_ProgramFilesX64 (GUID: 6D809377-6AF0-444b-8957-A3773F02200E).

But unfortunately, this is not allowed (as mentioned in the remarks section) and the function result correctly is 'file not found'.

Is there a way to accomplish this?

2

There are 2 best solutions below

0
On BEST ANSWER

You are requesting the PROGRAM FILES folder, not the SYSTEM folder. Look at FOLDERID_System and FOLDERID_SystemX86 instead.

Alternatively, use FOLDERID_Windows to get the Windows installation folder, and then append the special SysNative alias to the end of it, let the File System Redirector locate the actual folder for you, per the documentation:

32-bit applications can access the native system directory by substituting %windir%\Sysnative for %windir%\System32. WOW64 recognizes Sysnative as a special alias used to indicate that the file system should not redirect the access. This mechanism is flexible and easy to use, therefore, it is the recommended mechanism to bypass file system redirection. Note that 64-bit applications cannot use the Sysnative alias as it is a virtual directory not a real one.

6
On

I'm confused why you would try FOLDERID_ProgramFilesX64 when you say you want the System32 folder, since the former would (if it worked) give you the Program Files folder. For the System32 folder, you want the GetSystemDirectory function.

You can use the catchily-named Wow64DisableWow64FsRedirection function to disable WOW64 redirection, which allow your 32-bit app to actually access the 64 bit system folder. Then use Wow64RevertWow64FsRedirection to restore the redirection once you're done.

Also note that it's not actually recommended to store your own files in the System folder any more (and hasn't been for quite some time), as this note in the docs for GetSystemDirectory says:

Applications should not create files in the system directory. If the user is running a shared version of the operating system, the application does not have write access to the system directory.