C#调用C++动态库参数转换

用C#调用C++封装好dll,这个动态库的方法如下:long GetImageData( LPCTSTR i_strImageID, long i_nPhysicalX, float i_fMag,
long *o_nPhysicalWidth, long *o_nPhysicalHeight, void *i_pBuffer, long *io_nBufferSize) ,请问对应的C#方法参数如何写?

3个回答

 int GetImageData( StringBuileder i_strImageID, int i_nPhysicalX, float i_fMag, 
ref int o_nPhysicalWidth, ref int o_nPhysicalHeight, intptr i_pBuffer, ref int io_nBufferSize) 

非常感谢,但测试了一下,报以下错误:Additional information: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏

你可以参考这样写:
C++
extern "C"__declspec(dllexport)
void GetImageData(char*strImageID,int i_nPhysicalX, float i_fMag, int o_nPhysicalWidth,
int o_nPhysicalHeight, ushort* i_pBuffer, int* io_nBufferSize)

c#
[DllImport("xxxxx.dll",EntryPoint="GetImageData")]
public static unsafe extern void GetImageData(char*strImageID,int i_nPhysicalX, float i_fMag, int o_nPhysicalWidth,
int o_nPhysicalHeight, ushort* i_pBuffer, int* io_nBufferSize)

c#中
string strImageID_ptr="A0001"
char*strImageID = (char*)Marshal.StringToCoTaskMemAnsi(strImageID_ptr)
ushort[,] i_pBuffer_t=new double[10,10]
ushort* i_pBuffer =&i_pBuffer_t[0,0]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!