Windows7中创建文件错误

发布于 2024-07-21 05:54:40 字数 669 浏览 8 评论 0原文

我注意到,如果 CreateFile 函数的路径参数目标 \Windows\System32\ ,则调用失败并显示以下错误代码 ERROR_PATH_NOT_FOUND

文件路径是正确的,我是该文件夹的所有者,所以问题是为什么调用失败? MS是否添加了特殊策略来禁止访问该文件夹?

示例代码:

TCHAR szFile[MAX_PATH];
PathCombine(szFile, g_szSystemDirectory, "settings.ini");

HANDLE hFile = CreateFile(szFile,
                          GENERIC_READ,
                          0,
                          NULL,
                          OPEN_EXISTING,
                          0,
                          NULL);

if (hFile == INVALID_HANDLE_VALUE)
{
    printf("INVALID FILE: %i", GetLastError());
    return FALSE;
}

I've noticed that if the path parameter to the CreateFile function targets \Windows\System32\ the call is failing with the following error code ERROR_PATH_NOT_FOUND.

The file path is correct, I'm the owner of the folder, so the question is why is the call failing? Did MS add special policy forbidding the folder from being accessed?

Sample code:

TCHAR szFile[MAX_PATH];
PathCombine(szFile, g_szSystemDirectory, "settings.ini");

HANDLE hFile = CreateFile(szFile,
                          GENERIC_READ,
                          0,
                          NULL,
                          OPEN_EXISTING,
                          0,
                          NULL);

if (hFile == INVALID_HANDLE_VALUE)
{
    printf("INVALID FILE: %i", GetLastError());
    return FALSE;
}

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

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

发布评论

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

评论(4

旧瑾黎汐 2024-07-28 05:54:40
  1. 我们可以看一些示例代码吗?
  2. 您是否指定了驱动器,即“C:\Windows\System32\”
  3. 您是否正在尝试打开 system32 内的文件?
  4. 这种情况仅发生在 Windows 7 上吗?
    为什么
  5. 首先需要修改 system32 内部的任何内容?

比利3

  1. Can we see some example code?
  2. Have you specified the drive, I.e. "C:\Windows\System32\"
  3. Are you trying to open a file inside system32?
  4. Does this occur on Windows 7 only?
    and
  5. Why do you need to modify anything inside system32 in the first place?

Billy3

鸠书 2024-07-28 05:54:40

如果它是在 64 位操作系统上运行的 32 位应用程序,则在调用 CreateFile 之前调用 Wow64DisableWow64FsRedirection() 将从“C:\Windows\System32”而不是“C:\Windows\Syswow64”读取,这可能是你发生了什么事。

If it's a 32-bit app running on a 64-bit OS, then calling Wow64DisableWow64FsRedirection() before your call to CreateFile will read from "C:\Windows\System32" instead of "C:\Windows\Syswow64", which is probably what's happening to you.

暗地喜欢 2024-07-28 05:54:40

使用 Windows XP,管理员/标准帐户不需要管理权限即可获取设备句柄。

这在 Vista、Windows 7 (UAC) 上发生了变化,您必须拥有管理员权限才能获取设备句柄。

一些解决方案包括:

  1. 使用服务
  2. 使用 COM 提升名称
  3. 使用清单

注意:如果您只需要从设备查询统计信息,则不需要管理权限。 使用 CreateFile() 时,为 dwDesiredAccess 参数指定零 (0)。

Using Windows XP both administrators/standard accounts don't require administrative rights to obtain a device handles.

This has changed on Vista, Windows 7 (UAC) where you MUST have administrator rights to obtain device handles.

Some solutions are:

  1. Use a service
  2. Use COM elevation moniker
  3. Use Manifest

Note: If you only need to query statistic information from a device this doesn't require administrative rights. When using CreateFile(), specify zero (0) for the dwDesiredAccess parameter.

迷途知返 2024-07-28 05:54:40

您的程序可能需要以管理员身份运行。 即使您是管理员,您也必须升级您的权限。 运行程序时右键单击并单击“以管理员身份运行”,或编辑属性并选择始终以管理员身份运行。

You're program probably needs to run as Administrator. You'll have to escalate your privileges, even if you are an administrator. Right click when you run the program and click "Run as Administrator", or edit the properties and select always run as administrator.

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