How to retrieve correct path of either system32 or SysWOW64?
Asked Answered
D

3

4

I have a 32-bit process that can run either in 32-bit or 64-bit Windows. So, naturally, if the process tried to access the file c:\windows\system32\file.ext, it would be redirected to c:\windows\SysWOW64\file.ext. So far so good - I don't want to disable the redirection.

My problem is that my process doesn't actually access the file - instead it just takes its path and writes it into a text file, and I want that text file to read SysWOW64 on a 64-bit system, and system32 on a 32-bit system. How can I do that?

Dogged answered 22/6, 2010 at 15:16 Comment(3)
What language are you using? C/C++/C#? .Net, Boost, STL?Eason
Oops, I knew I'd forget something :) Undecided between Python, C# and a simple batch file.Dogged
possible duplicate of Getting syswow64 directory using 32-bit applicationOverarch
P
8

The following code will return the correct system directory (system32\syswow64):

[DllImport("shell32.dll")]
public static extern bool SHGetSpecialFolderPath(
    IntPtr hwndOwner, [Out]StringBuilder lpszPath, int nFolder, bool fCreate
);

public static string GetSystemDirectory()
{
    StringBuilder path = new StringBuilder(260);
    NativeMethods.SHGetSpecialFolderPath(IntPtr.Zero, path, 0x0029, false);
    return path.ToString();
}

On x86 you'll get %windir%\System32 On X64 you'll get %windir%\SysWow64

Hope this is helpful

Pharsalus answered 22/8, 2010 at 12:15 Comment(2)
Marking this as the accepted answer although it suggests the same solution as the other answer, because it provides a working code snippet. Thanks!Dogged
In .NET 4.0 and higher you can do this: Environment.GetFolderPath(Environment.SpecialFolder.SystemX86)Publia
S
4

if I understood it correctly, you can use SHGetSpecialFolderPath passing CSIDL_SYSTEMX86 to the csidl parameter. The documentation for the valid csidl's states that a 32 bit process will get:

  • %windir%\system32 on a 32 bits OS
  • %windir%\syswow64 on a 64 bits OS

Best regards

Sigismondo answered 23/6, 2010 at 1:51 Comment(0)
T
-1

System32 C:\Windows\System32 Windows System folder (system directory) for 64-bit files SysWOW64 C:\Windows\SysWOW64 Windows System folder (system directory) for 32-bit files Program Files C:\Program Files Folder for 64-bit program files Program Files (x86) C:\Program Files (x86) Folder for 32-bit program files

Trude answered 11/1, 2013 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.