Windows 服务是否限制从 %WINDIR% (C:\Windows) 读取?
我有一个作为 Windows 服务运行的应用程序,它想要读取由相对路径指定的文件。由于服务在C:\Windows\system32<下运行/em> (在 Server 2003 和 Windows 7 上),我认为它应该从那里读取文件。但是,文件读取总是失败。
我整理了一些简单的测试代码,尝试使用绝对路径打开文件进行读取。虽然服务对于 C:\Temp\foo.txt 等文件成功,但对于 C:\Windows\foo.txt 和 C 等文件总是失败:\Windows\system32\foo.txt 。 GetLastError()
返回 2
。
我遇到了访问问题吗?我找不到这方面的权威文档。有什么解决方法吗?
更新:
文件测试代码是通用且简单的:
std::ofstream out;
//...
std::string fileName("C:\\Windows\\system32\\Foo.txt");
hFile = CreateFile(fileName.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
out << "Could not create file handle! (" << GetLastError() << ")" << std::endl;
}
else {
out << "Successfully opened file!" << std::endl;
CloseHandle(hFile);
}
I've got an application running as a Windows service that wants to read a file specified by a relative path. Since the service is running under C:\Windows\system32 (on Server 2003 and Windows 7), I figure it should be reading the file from there. However, the file read always fails.
I put together some simple test code to try to open a file for reading, using an absolute path. While the service succeeds for files such as C:\Temp\foo.txt, it always fails for files like C:\Windows\foo.txt and C:\Windows\system32\foo.txt . GetLastError()
returns 2
.
Am I running into an access issue? I couldn't find authoritative documentation on this. Is there any workaround?
Update:
The file test code is generic and straightforward:
std::ofstream out;
//...
std::string fileName("C:\\Windows\\system32\\Foo.txt");
hFile = CreateFile(fileName.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
out << "Could not create file handle! (" << GetLastError() << ")" << std::endl;
}
else {
out << "Successfully opened file!" << std::endl;
CloseHandle(hFile);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试从本地系统帐户运行 Windows 服务。默认情况下,该服务可能从“网络服务”帐户运行。
要更改设置,请打开 Windows 服务管理器(运行 -> services.msc)并双击您的服务。在属性窗口中选择第二个选项卡“登录”并将其更改为使用本地系统帐户运行。
Try running the windows service from Local System account. By default the service may be running from "Network Service" account.
To change the settings, Open Windows Service Manager (Run-> services.msc) and double-click your service. On property window select 2nd Tab "Log On" and change it to run with Local System account.
错误代码 2 是
ERROR_FILE_NOT_FOUND
,因此您提供的路径很可能不存在,或者该路径中不存在该文件。如果没有CreateFile
中的相关标志,很难给您更好的答案。但通常 - 在默认条件下 - 将允许服务读取该文件夹。
我又想起一件事。如何获取路径(在您的情况下是
C:\Windows
)?正确的方法是为此使用 API(例如GetWindowsDirectory
),而不是对其进行硬编码。Error code 2 is
ERROR_FILE_NOT_FOUND
so it's likelier that the path you give simply does not exist or the file does not exist in that path. Without the relevant flags fromCreateFile
it's hard to give you a better answer.But generally - under default conditions - a service would be allowed to read in that folder.
One more thing came to mind. How do you obtain the path (
C:\Windows
in your case)? The proper means are to use the API (e.g.GetWindowsDirectory
) for this and not hardcode it.