从C#程序到C++ win32 DLL共享结构内存的最佳实践是什么?
我在托管共享内存中使用了结构,在两个C++程序之间使用Boost,它工作得很好。我对在C#程序中填充结构和作为subagent的C++ DLL之间实现此操作的最佳方法感到迷茫。
下面是C++ DLL:
//==================== Code Excerpt from the main cpp file ======================
#include "stdafx.h"
//================= Here we are setting up the shared memory area =====================
#pragma data_seg (".SHAREDMEMORY")
struct sharedData {
int sharedA;
int sharedB;
};
static sharedData A;
#pragma data_seg()
#pragma comment(linker,"/SECTION:.SHAREDMEMORY,RWS")
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
return TRUE;
}
//=============================================================================================
//====================== Here we are writing wrappers to the shared memory area ===========================
//=You must declare it as an Extern "C" to prevent name mangling. This is absolutely necessary in order to import it into c# =
//=============================================================================================
extern "C" __declspec(dllexport) sharedData __stdcall getMyData()
{
A.sharedA = 1237;
A.sharedB = 31337;
//return gshared_nTest;
return A;
}
extern "C" __declspec(dllexport) void __stdcall setMyData( sharedData buff )
{
A = buff;
}下面是调用C#函数:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace sharedMemTestCS
{
public partial class frmSharedMemTestCS : Form
{
struct sharedData {
int sharedA;
int sharedB;
};
static sharedData A;
//============== here we are importing the methods from the win32 dll into the c# console application =================
[DllImport(@"C:\Documents and Settings\My Documents\Visual Studio 2010\Projects\sharedMemTestCPP\Debug\sharedMemTestCPP.dll")]
public static extern sharedData getMyData();
[DllImport(@"C:\Documents and Settings\My Documents\Visual Studio 2010\Projects\sharedMemTestCPP\Debug\sharedMemTestCPP.dll")]
public static extern void setMyData(int data);
public frmSharedMemTestCS()
{
InitializeComponent();
//============== here i am incrementing the value =================
//== i use a message box so that i can have multiple console applications running at once and it will pause at the messagebox (if i don't click ok)
//== i do this so i can see the values changing in the shared memory.
//MessageBox.Show( getMyData().ToString() );
getMyData();
//txtBoxA.Text = (getMyData().ToString() );
}
private void btnAdd_Click(object sender, EventArgs e)
{
//setMyData( getMyData() + 100 );
//txtBoxA.Text = (getMyData().ToString() );
}
}
}我得到的错误消息是:
错误1不一致的可访问性:返回类型 'sharedMemTestCS.frmSharedMemTestCS.sharedData‘比方法'sharedMemTestCS.frmSharedMemTestCS.getMyData()’c:\‘sharedMemTestCS.frmSharedMemTestCS.getMyData()’和设置更难访问\mconrad\my documents\visual studio 2010\Projects\sharedMemTestCS\sharedMemTestCS\Form1.cs 23 37 sharedMemTestCS
发布于 2011-07-25 16:42:08
共享内存的最佳实践是在C#中使用C#类,在C++中使用CreateFileMapping/MapViewOfFile。
发布于 2011-07-23 08:37:41
首先,您不能立即使用Boost进行数据共享。您需要在托管和非托管世界之间共享一些定义良好的数据结构。
您可以启动这里
发布于 2011-07-25 16:31:26
好吧,您的实际问题是您的p/invoke表达式是公共的,但是您的结构是私有的,这就是错误告诉您的。将p/invoke表达式设置为私有或公共结构将解决当前问题。
至于实际的数据共享,我从来没有尝试过这样做,所以我无法告诉您它是否会起作用。我处理过的所有片段都是来回排列的。看看您的示例代码,很有可能它可以工作。您可能希望将数据复制到c#的新结构中,或者将您的结构插入回原来的位置,这样GC就不会在内存中移动它。
https://stackoverflow.com/questions/6799192
复制相似问题