开发者

Inter-process communication using MemoryMappedFiles or Named Pipes in VC++ and C# in Windows 7

开发者 https://www.devze.com 2023-02-19 08:21 出处:网络
I\'m trying to communicate between a VC++ DLL and a C# application. I\'ve read tons of resources and material, I tried to do it with both MemoryMapped files and Pipes, but unfortunately I couldn\'t ge

I'm trying to communicate between a VC++ DLL and a C# application. I've read tons of resources and material, I tried to do it with both MemoryMapped files and Pipes, but unfortunately I couldn't get to work.

Here's how I create the "receiver"/server part written in C#:

// MemoryMappedFile approach
mmf = MemoryMappedFile.CreateNew(
            @"myMMF",
            1024*1024,
            MemoryMappedFileAcces开发者_Go百科s.ReadWriteExecute);

// Named Pipe approach
NamedPipeServerStream pipe = new NamedPipeServerStream("myPipe", PipeDirection.InOut);

This is the VC++ counterpart:

// MMF approach
HANDLE fmap = ::OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE, false, (LPCWSTR)"myMMF");
if(fmap == NULL)
    return errorMsg();

// Named pipe
HANDLE pipe = ::CreateFile((LPCWSTR)"\\\\.\\pipe\\myPipe", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if(pipe == INVALID_HANDLE_VALUE)
    return errorMsg();

My problem is, that regardless of which approach I use, the open calls always return a "file not found" error. The applications both run under the same user account on my 64-bit Windows 7. What am I missing here?


::CreateFile((LPCWSTR)"\\\\.\\pipe\\myPipe", ...)

That cannot work, you cannot cast a char* to a wide string, a conversion is required. But not necessary here since this is a string literal. Make it a unicode literal by prefixing an L

::CreateFile(L"\\\\.\\pipe\\myPipe", ...)

Same problem with the MMF. Also consider that it doesn't make much sense to use an out-of-process communication mechanism to talk to a DLL. Just load it in-process. Use pinvoke or a C++/CLI wrapper.

0

精彩评论

暂无评论...
验证码 换一张
取 消