5

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?

joe
  • 8,344
  • 9
  • 54
  • 80
  • You're better off making your 32-bit program launch a separate 64-bit program that copies the files. See http://blogs.msdn.com/b/oldnewthing/archive/2008/12/22/9244582.aspx – Jim Mischel Sep 19 '14 at 05:02
  • Solve the problem by avoiding it. The problem vanishes when you obey the rules and don't modify the system directory. – David Heffernan Sep 19 '14 at 05:53
  • @DavidHeffernan: Well my job is to install a DLL in both 32bit and 64bit versions to the system... – joe Sep 19 '14 at 08:06
  • Unless you work for the Microsoft Windows team, it's against the platform rules to install to system directories. Obviously you can elect to ignore the rules, I'm just letting you know that you are breaking rules. – David Heffernan Sep 19 '14 at 08:09
  • OK, so I missed something. What are the rules when I have to install a commonly used DLL (no assembly)? – joe Sep 19 '14 at 08:11

2 Answers2

6

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
4

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.

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • The application helps to install both variants (32 and 64 bit) of a DLL. Therefore I need both system folders *explicitly*. – joe Sep 18 '14 at 09:50
  • For a 32-bit app the path to both 32 and 64 bit system folders is (e.g.) **C:\Windows\System32** - it's Wow64 filesystem redirection that determines which folder you end up accessing. – Jonathan Potter Sep 18 '14 at 09:51
  • So switching the filesystem redirection on and off is the only way to target the correct folder - Is that what you're telling me? – joe Sep 18 '14 at 09:54
  • It's not the only way to target it but I don't think there's a way to actually get the path to the 32 bit folder other than just assuming it's called SysWOW64 and is a sibling of System32. Whereas using the redirection trick you don't need to worry about where it's actually located. – Jonathan Potter Sep 18 '14 at 09:55
  • OK I see. This assumes that my application is 32 bit. The best case would be that it is ensured to target System32 and SysWOW64, no matter if my application is 32 or 64 bit. If I have a 64 application, I could use the FOLDERID_??? then. – joe Sep 18 '14 at 10:02
  • 2
    Why you shouldn't use `Wow64DisableWow64FsRedirection` to solve a local problem and what you should use instead: http://blogs.msdn.com/b/oldnewthing/archive/2013/03/21/10404021.aspx – GSerg Sep 18 '14 at 10:04