System Error 0x5: CreateFileMapping()
我希望使用命名共享内存来实现IPC。
为此,步骤之一是使用CreateFileMapping()获取映射内存对象的句柄。
我完全按照MSDN网站的建议进行操作:http://msdn.microsoft.com/zh-cn/library/aa366551(v=VS.85).aspx:
1 2 3 4 5 6 7 8 9 10 | hFileMappingHandle = CreateFileMapping ( INVALID_HANDLE_VALUE, // use paging file NULL, // default security PAGE_READWRITE, // read/write access 0, // maximum object size (high-order DWORD) 256, // maximum object size (low-order DWORD) "Global\\\\MyFileMappingObject" // name of mapping object ); DWORD dwError = GetLastError(); |
但是,返回的句柄始终为0x0,并且返回的系统错误代码为:0x5(访问被拒绝。)
- 仅需要"命名内存共享"(不共享文件)。
- Windows 7 x64位操作系统
- 管理员的用户权限可用
- 开发的应用程序:64位插件应用程序(.dll)
请问有人有相同的经验,并且有解决方法吗? 我使用MSDN站点作为参考,所以我不认为代码中有问题。
您似乎没有足够的权限。
从MSDN:
Creating a file mapping object in the
global namespace from a session other
than session zero requires the
SeCreateGlobalPrivilege privilege. For
more information, see Kernel Object
Namespaces....
The creation of a file-mapping object
in the global namespace, by using
CreateFileMapping, from a session
other than session zero is a
privileged operation. Because of this,
an application running in an arbitrary
Remote Desktop Session Host (RD
Session Host) server session must have
SeCreateGlobalPrivilege enabled in
order to create a file-mapping object
in the global namespace successfully.
The privilege check is limited to the
creation of file-mapping objects, and
does not apply to opening existing
ones. For example, if a service or the
system creates a file-mapping object,
any process running in any session can
access that file-mapping object
provided that the user has the
necessary access.
默认情况下,管理员,服务和网络服务具有SeCreateGlobalPrivilege。 但是,您必须记住,Windows7 / Vista并非以管理员身份运行所有内容。 因此,使用"以管理员身份启动"可以使"全局"适用于您的应用程序。 如果要调试,请也以管理员身份启动Visual Studio。
要创建全局文件映射,您需要
有关全局名称空间的文档中对终端服务的引用有点误导,因为这意味着只有在遇到异常情况时才需要担心。
实际上,IIS和系统服务都在会话零中运行,并且第一个/唯一登录的用户在会话1中运行-因此,您必须使用Global名称空间在IIS或服务与普通程序之间进行通信。