内存共享函数OpenFileMapping,在release模式下返回NULL

来源:互联网 发布:阿里云服务器安全问题 编辑:程序博客网 时间:2024/05/18 02:39

        共享内存的步骤为:CreateFileMapping创建共享的内存空间;OpenFileMapping打开共享内存,返回HANDLE型句柄;MapViewOfFile获取内存映射到该程序的内存,可以进行读写操作。

        在实验中遇到了意外的情况,具体应用情景为:win10环境下使用共享内存在两个不同程序之间传递数据。在release模式下第二个程序调用OpenFileMapping始终返回NULL。

        使用getLastError函数返回错误2:系统找不到指定文件。

        查看了微软OpenFileMapping官方文档(链接),里面有一段话:

       The name of the file mapping object to be opened. If there is an open handle to a file mapping object by this name and the security descriptor on the mapping object does not conflict with the dwDesiredAccess parameter, the open operation succeeds. The name can have a "Global\" or "Local\" prefix to explicitly open an object in the global or session namespace. The remainder of the name can contain any character except the backslash character (\). For more information, see Kernel Object Namespaces. Fast user switching is implemented using Terminal Services sessions. The first user to log on uses session 0, the next user to log on uses session 1, and so on. Kernel object names must follow the guidelines outlined for Terminal Services so that applications can support multiple users.

        没怎么看懂,应该是和windows程序管理有关吧,好像是win7之后没有界面的程序视为“服务”,运行在系统的session 0(session 0参考这里),和我的另一个程序运行的命名空间是不一样的,所以要在共享内存名前面加上"Global\",问题就解决了。
char * name = "Global\ShareMemory";
        看一下效果吧:


0 0
原创粉丝点击