问题描述
我有一个C ++。NET应用程序和C#.NET应用程序。我希望他们通过共享内存进行通信。
I have a C++.NET app and a C#.NET app. I would like them to communicate via shared memory.
怎么可能在.NET版本2.0?
How is it possible in .NET version 2.0 ?
主要是想分享一个队列对象。
Mainly want to share a queue object.
推荐答案
更新:嘿,这里的我只是发现有compleate implmentation一个页面。
Update: Hey, here's a page I just found with a compleate implmentation.
使用C ++ / CLI,这是很容易设置的共享内存按照正常的C ++ API(C ++ / CLI能够与托管和本机堆/内存引用交互)。该UnmanagedMemoryStream可以被用来揭露Stream对象为C#。
Using C++/CLI, it's quite easy to setup shared memory as per normal C++ API (C++/CLI being able to interact with the managed and native HEAP/memory references). The UnmanagedMemoryStream can then be used to expose a Stream object to C#.
我没有附上.h文件,但可以推断出pmapped本土的typedef的布局相当easially)。您可能还需要评估可能的使用取决于你的读/写用例BufferedStream的。而code是,我不使用任何更多,所以我不记得状态是错误回归的一个项目。
I did not attach the .h file, but you can infer the layout of the pmapped native typedef fairly easially ;). You may also want to evaluate the possible use of a BufferedStream depending on your reader/writer use case. And the code is from a project which I do not use any more so I can not remember the status of it's bug regression.
下面是它建立一个文件映射和暴露了一个UnmanagedMemoryStream的C ++ / CLI类;
Here's the C++/CLI class which establishes a file mapping and exposes an UnmanagedMemoryStream;
public ref class MemMapp
{
public:
__clrcall MemMapp(String^ file)
{
map = NULL;
if(!File::Exists(file)) throw gcnew ApplicationException("Can not find file " + file);
marshal_context^ x = gcnew marshal_context();
const char *nname = x->marshal_as<const char*>(file);
map = (pmapped) malloc(sizeof(mapped));
ZeroMemory(map, sizeof(mapped));
map->name = strdup(nname);
InitMap(map);
}
void __clrcall MapBytes(long long loc, long length)
{
map->low = loc & 0xffffffff;
map->high = (loc >> 32) & 0xffffffff;
map->size = length & 0xffffffff;
if(!GetMapForFile(map))
throw gcnew ApplicationException("can not map range " + loc + " :" + length);
if(map->size = 0)
map->size = MAXMAX&0xffffffff;
}
UnmanagedMemoryStream ^View()
{
return gcnew UnmanagedMemoryStream((unsigned char *) map->blok, map->size, map->size, FileAccess::Read);
}
long long __clrcall FileSize()
{
DWORD high, low;
long long rv;
low = GetFileSize(map->hFile, &high);
maxmax = high;
maxmax << 32;
maxmax += low;
rv = high;
rv << 32;
rv = rv & low;
return rv;
}
property unsigned int MinBufSiz { unsigned int get() { return map->dwbufz; } }
property long long BufBase { long long get() { return (map->high << 32) + map->low; } }
property long long BufLim { long long get() { return ((map->high << 32) + map->low) + map->size; } }
property long long MAXMAX { long long get() { return maxmax; } }
static MemMapp() { }
__clrcall ~MemMapp() { if(map != NULL) { CloseMap(map); free(map->name); free(map); map = NULL; } }
protected:
__clrcall !MemMapp() { if(map != NULL) { CloseMap(map); free(map->name); free(map); map = NULL; } }
pmapped map;
long long maxmax;
};
下面是CLoseMap至少...我只是觉得......它不是用/ CLR
Here's CLoseMap at least... I just found it... it was not compiled with /CLR
bool CloseMap(pmapped map)
{
if(map->blok != NULL) {
UnmapViewOfFile(map->blok);
map->blok = NULL;
}
if(map->hMap != INVALID_HANDLE_VALUE && map->hMap != NULL) {
CloseHandle(map->hMap);
map->hMap = INVALID_HANDLE_VALUE;
}
if(map->hFile != INVALID_HANDLE_VALUE && map->hFile != NULL) {
CloseHandle(map->hFile);
map->hFile = INVALID_HANDLE_VALUE;
}
return false;
}
这篇关于如何实现在.NET共享内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!