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

5 篇文章 0 订阅
3 篇文章 0 订阅

        共享内存的步骤为: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 0session 0参考这里),和我的另一个程序运行的命名空间是不一样的,所以要在共享内存名前面加上"Global\",问题就解决了。
char * name = "Global\ShareMemory";
        看一下效果吧:


  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值