如何检索 system32 或 SysWOW64 的正确路径?

发布于 2024-09-06 18:14:58 字数 356 浏览 1 评论 0原文

我有一个 32 位进程,可以在 32 位或 64 位 Windows 中运行。因此,自然地,如果进程尝试访问文件 c:\windows\system32\file.ext,它将被重定向到 c:\windows\SysWOW64\file.ext代码>.到目前为止一切顺利 - 我不想禁用重定向。

我的问题是我的进程实际上并没有访问文件 - 相反,它只是获取其路径并将其写入文本文件,并且我希望该文本文件能够读取64 位系统上为 SysWOW64,32 位系统上为 system32。我怎样才能做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恋你朝朝暮暮 2024-09-13 18:14:58

以下代码将返回正确的系统目录 (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();
}

在 x86 上,您将获得 %windir%\System32
在 X64 上你会得到 %windir%\SysWow64

希望这有帮助

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

爱的十字路口 2024-09-13 18:14:58

如果我理解正确,您可以使用 SHGetSpecialFolderPath 将 CSIDL_SYSTEMX86 传递给 csidl 参数。 有效的 csidl 指出 32 位进程将得到:

  • %windir%\system32 (在 32 位操作系统上)
  • %windir%\syswow64 (在 64 位操作系统上)

最佳问候

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

失而复得 2024-09-13 18:14:58

System32 C:\Windows\System32 64 位文件的 Windows 系统文件夹(系统目录)
SysWOW64 C:\Windows\SysWOW64 32 位文件的 Windows 系统文件夹(系统目录)
Program Files C:\Program Files 64 位程序文件的文件夹
Program Files (x86) C:\Program Files (x86) 32 位程序文件的文件夹

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文