Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:windowssystem32file.ext, it would be redirected to c:windowsSysWOW64file.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

The following code will return the correct system directory (system32syswow64):

[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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...