【发布时间】:2020-11-06 07:06:37
【问题描述】:
我正在尝试使用内存映射文件共享从 C++ 到 C# 的结构。到目前为止,我设法在文件上写入,但我无法读取 C# 中的内容。
- C++ 中的发送数据
struct Bus_1553 // this is the structure to send
{
string name;
int directions;
};
struct Bus_1553* p_1553; // set the pointer to it
HANDLE handle; // create the handle
// here we define the data to send
string name = "IFF";
int directions = 3;
bool startShare() // Open the shared memory
{
try
{
handle = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(Bus_1553), L"DataSend");
p_1553 = (struct Bus_1553*) MapViewOfFile(handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, sizeof(Bus_1553));
return true;
}
catch (...)
{
return false;
}
}
int main()
{
if (startShare() == true)
{
while (true)
{
if (p_1553 != 0) // populate the memory
{
p_1553->name = name;
p_1553->directions = directions;
}
else
puts("create shared memory error");
}
}
if (handle != NULL)
CloseHandle(handle);
return 0;
}
- 尝试用 C# 读取
namespace sharedMemoryGET
{
class sharedMemoryGET
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct Bus_Data_1553
{
public string name;
public int directions; // which directions used
}
public static MemoryMappedFile mmf;
public static MemoryMappedViewStream mmfvs;
static public bool MemOpen() // open the mapped file
{
try
{
mmf = MemoryMappedFile.OpenExisting("DataSend");
return true;
}
catch
{
return false;
}
}
public static void readData()
{
if (MemOpen())
{
using (var accessor = mmf.CreateViewAccessor())
{
accessor.Read(0, out Bus_Data_1553 a);
Console.WriteLine(a.name);
Console.WriteLine(a.directions);
}
}
}
}
}
当结构中存在要共享的字符串时,出现以下错误: 指定的 Type 必须是不包含引用的结构。
当我删除字符串并仅共享 int 方向时,我得到的值为 0。有人可以帮我解决这个问题吗?
【问题讨论】:
-
简单地说,C++
std::string不是 C#string。由于std::string是成员,因此您传递的struct不可轻易复制。此外,sizeof(Bus_1553)并没有按照您的想法去做。如果std::string有一千个字符,sizeof(Bus_1553)不会改变,因为sizeof是编译时值,而不是运行时值。 -
您尝试解决的一般问题称为序列化(或者有时可能是编组)。基本上,您必须设计某种通用数据格式。也许这可能只是确保
Bus_1553是一个标准布局结构,然后您可以在 C# 端使用Marshal.PtrToStructure。 -
@PaulMcKenzie 如果您有答案,请将其放在答案部分。
-
(这次选对了保罗!)
-
对,就像在下面的 cmets 中一样,我移到了
char name[128]和 C# 中的相关编组字符串[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string name;。但是我仍然得到The specified Type must be a struct containing no references. Parameter name: type
标签: c# c++ windows memory-mapped-files