问题描述
我正在编写Windows应用程序,该应用程序需要在多个实例(进程)之间共享一些日期.我选择使用boost进程间.经过一番调查,我发现managed_windows_shared_memory最适合我的情况.但是第二进程无法打开由第一进程创建的共享内存.
I'm writing a Windows application, which needs to share some date between multiple instances (processes). I choose to use boost interprocess. After some investigation, I find managed_windows_shared_memory is best for my situation. But 2nd process can't open shared memory created by 1st process.
第一个实例的代码:
#include <boost/interprocess/managed_windows_shared_memory.hpp>
using namespace boost::interprocess;
typedef boost::interprocess::managed_windows_shared_memory SharedMemory;
SharedMemory sharedMemory(create_only, "MyTestSharedMemory", 65535);
第二个实例的代码:
#include <boost/interprocess/managed_windows_shared_memory.hpp>
using namespace boost::interprocess;
typedef boost::interprocess::managed_windows_shared_memory SharedMemory;
try
{
SharedMemory sharedMemoryT(open_only, "MyTestSharedMemory");
}
catch (interprocess_exception &ipce)
{
if (ipce.get_error_code() == not_found_error)
{
::OutputDebugStringA("Not found");
}
}
我确定当第二个实例试图打开共享内存时,第一个实例进程仍在运行(未退出).
I'm sure when 2nd instance trying to open the shared memory, 1st instance process is still running (not exited).
通过运行accesschk.exe,我确定共享内存系统对象已创建,并且在第二个实例尝试打开它时仍退出.这是有关此共享内存的输出.这里的我的域",我的帐户"和我的域帐户"在我的环境中具有实际值.
By running accesschk.exe, I'm sure the shared memory system object is created and still exiting when 2nd instance trying to open it. Here is the output about this shared memory. Here "my-domain", "my-account" and "my-domain-account" have actual values in my environment.
\Sessions\5\BaseNamedObjects\MyTestSharedMemory
Type: Section
Medium Mandatory Level (Default) [No-Write-Up]
RW NT AUTHORITY\SYSTEM
SECTION_ALL_ACCESS
RW my-domain\my-account
SECTION_ALL_ACCESS
RW my-domain\my-domain-account
SECTION_ALL_ACCESS
每次,第二个实例都会得到"not_found_error".如果我先尝试创建共享内存,然后再尝试在同一实例中打开它,那么它会成功.任何想法?我在Windows 8.1上.
Every time, 2nd instance gets "not_found_error". If I try to create shared memory first and then try to open it in same instance, it successes. Any idea? I'm on Windows 8.1.
更多信息.这两个实例使用相同的用户帐户在同一会话中运行.
More information.These two instances run in same session with same user account.
我尝试在第一个实例中使用Windows API CreateFileMapping,在第二个实例中使用OpenFileMapping,效果很好!
我还尝试使用boostmanaged_shared_memory而不是managed_windows_shared_memory,它也可以工作.
然后我尝试使用boost windows_shared_memory,第一个实例成功创建了它,但是第二个实例找不到它.
好像在boost native Windows共享内存类(managed_windows_shared_memory/windows_shared_memory)中存在一些问题.
推荐答案
我最好的选择是,您不在与创建共享内存区域的服务器"进程相同的会话中运行(请注意 \ Sessions \ 5
名称空间前缀).
My best bet is you're not running in the same session as the 'server' process that creates the shared memory area (note the \Sessions\5
namespace prefix).
通常在一个进程作为服务运行或假冒其他帐户(例如ASP/IIS工作程序,Citrix/终端服务器会话等)时发生
This usually happens when one process runs as a service or otherwise impersonating a different account (think ASP/IIS workers, Citrix/Terminal server sessions etc.)
这篇关于提升进程间,第二个进程无法打开由第一个进程创建的共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!