[英]How to transmit c++ strucure from dll to c# program?
I have DLL, written in C++. 我有用C ++编写的DLL。 There is structure in this DLL.
该DLL中有结构。
typedef struct TransmitData
{
wchar_t szPath[MAX_PATH];
DWORD dwResult;
} *lpTransmitData;
And I have one function to fill this struct 我有一个函数来填充此结构
extern "C" __declspec(dllexport) int GetData(struct TransmitData &data)
{
//I HAVE ONE THE SAME STRUCT IN THIS DLL
memcpy(&data, &transmitData, sizeof(struct TransmitData));
return ret_value;
}
In C# program I use functions with simple data types well, but for structure it doesn't work. 在C#程序中,我很好地使用了具有简单数据类型的函数,但是对于结构而言,它不起作用。 There is code on C#:
C#上有代码:
public struct TransmitData
{
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 260)] //260 = MAX_PATH
public string szPath;
public uint dwResult;
}
//...
[DllImport("MyDLL")]
public static extern int GetData(ref TransmitData data);
What am I doing wrong? 我究竟做错了什么? Thanks!
谢谢!
I suggest you to replace 我建议你更换
[DllImport("MyDLL")]
with 与
[DllImport("MyDLL", CallingConvention = CallingConvention.Cdecl)]
and 和
extert "C"
with 与
extern "C"
Moreover, as Joe suggested, add 此外,正如乔建议的那样,添加
[StructLayout(LayoutKind.Sequential)]
before the declaration of the struct in C# code 在C#代码中声明结构之前
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.