Getting syswow64 directory using 32-bit application
Asked Answered
L

4

13

I'm trying to find a file inside the system directory. The problem is that when using

Environment.SystemDirectory

On a x64 machine, i'm still getting the System32 directory, instead of the Systemwow64 directory.

I need to get the "System32" directory on x86 machines, and "SystemWow64" directory on x64

Any ideas?

EDIT: To find the SysWow64 i'm using the "GetSystemWow64Directory". (more information here: pinvoke Notice that on non-x64 machines - result is '0'. Hope this helps someone

Lefevre answered 22/8, 2010 at 9:31 Comment(1)
May be duplicate #3095020Zosema
L
11

Using the SHGetSpecialFolderPath function:

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

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

Will return System32 on x86, and SysWow64 on x64

Lefevre answered 2/9, 2010 at 6:39 Comment(0)
A
14

Use Environment.GetFolderPath(Environment.SpecialFolder.SystemX86) instead.

Amanuensis answered 22/8, 2010 at 9:37 Comment(4)
I don't have the SystemX86 in my enum (only "system", which returns the system32 folder). I'm under .net 2.0Lefevre
If you're getting "System32" in a 32-bit process on a 64-bit machine, it sounds like you have Windows' file system redirection (msdn.microsoft.com/en-us/library/aa384187(VS.85).aspx) turned off. Could that be the case? You can call Wow64EnableWow64FsRedirection to ensure that it's enabled (see msdn.microsoft.com/en-us/library/aa365744(VS.85).aspx).Amanuensis
SystemX86 was added to the Environment.SpecialFolder enum in version 4 of the .NET Framework. See here.Chagrin
Just in case it wasn't clear as I wasn't sure myself this will do the switch to the SysWow64 bit folder on 64 bit systems. I was unsure because of the .SystemX86 extension property so I tested it out and it did the trick!Courteous
L
11

Using the SHGetSpecialFolderPath function:

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

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

Will return System32 on x86, and SysWow64 on x64

Lefevre answered 2/9, 2010 at 6:39 Comment(0)
I
3

What your 32-bit program thinks is System32 is really SysWOW64 - don't code 32-bit apps to have any explicit knowledge of 64-bit, that's what WOW64 redirection is for

Indemnification answered 22/8, 2010 at 9:39 Comment(4)
Exactly. I just want to get the default system directory, but I'm always getting the system32 directoryLefevre
@Nissim: System32 is the default directory. It is just a different place in 32bit vs 64bit processes. Perhaps if you said why you need this information we might be able to help more (i.e. expand your question).Causeuse
I need to get the IIS exe in order to determine the IIS version. I'm doing this by locating the file, and retrieving it's information through FileVersionInfo. But when i'm using Path.Combine(Environment.SystemDirectory, @"inetsrv\inetinfo.exe") I get FileNotFoundException. This is not a critical issue, since if this test fails, i'm retrieving this information through other sources, but still...Lefevre
You keep saying you want SysWOW64, but I think that's where you believe the 64-bit modules reside. The Sys-WOW64 directory is where the 32-bit modules are. You want the (Real) System32 folder, where the 64-bit modules are. For that, try %SystemRoot%\SysNative.Indemnification
C
0

I had the same problem. The solutions is to set the "Platform target" as x86 instead of AnyCPU or x64 in project properties in Visual Studio. In this case the path will be "C:\Windows\system32" but it actually redirects to "C:\Windows\SysWOW64" You can check this by placing any file in the "C:\Windows\SysWOW64" folder and then use File.Exists command to check if file is found in that folder:

File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), sFileName));

Or

File.Exists(Path.Combine(Environment.SystemDirectory, sFileName));

enter image description here

Curator answered 27/4, 2014 at 18:50 Comment(1)
And then you will lose all the advantages of the 64 bitness... touchéLefevre

© 2022 - 2024 — McMap. All rights reserved.