How to retrieve correct path of either system32 or SysWOW64?

前端 未结 3 1422
执笔经年
执笔经年 2020-12-21 02:52

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,

相关标签:
3条回答
  • 2020-12-21 03:03

    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

    0 讨论(0)
  • 2020-12-21 03:10

    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

    0 讨论(0)
  • 2020-12-21 03:24

    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

    0 讨论(0)
提交回复
热议问题