[英]SafeArrayTypeMismatchException when passing c# struct to unmanaged C++ DLL
Hi I'm new to C# and C++ programming and trying to use an unmanaged C++ dll in a C# project (.NET 3.5). 嗨,我是C#和C ++编程的新手,正在尝试在C#项目(.NET 3.5)中使用不受管理的C ++ dll。 I'm stuck on this error: 我陷入了这个错误:
System.Runtime.InteropServices.SafeArrayTypeMismatchException: Specified array was not of the expected type. System.Runtime.InteropServices.SafeArrayTypeMismatchException:指定的数组不是预期的类型。
Here is the header file for the DLL 这是DLL的头文件
#ifdef FUNCTIONLIB_EXPORTS
#define FUNCTIONLIB_API __declspec(dllexport)
#else
#define FUNCTIONLIB_API __declspec(dllimport)
#endif
typedef struct _FunctionOpt
{
char UserName[32];
char Password[32];
char ServerIP[128];
int ServerPort;
int Index;
char TargetSubChannel;
long Timeout;
char Filepath[256];
bool isFirst;
DWORD SyncTime;
char *pOutBuf;
int OutBufSize;
unsigned short OutImgResW;
unsigned short OutImgResH;
}STFUNCTIONOPT, *PSTFUNCTIONOPT;
FUNCTIONLIB_API int FunctionLib_Snapshot( PSTFUNCTIONOPT pstFunctionOpt );
I don't have access to the C++ code so I can only change the following C# code. 我无权访问C ++代码,因此只能更改以下C#代码。 My relevant code is as follows: 我的相关代码如下:
public unsafe struct PSTFUNCTIONOPT
{
public char[] UserName;
public char[] Password;
public char[] ServerIP;
public int ServerPort;
public int Index;
public char TargetSubChannel;
public long Timeout;
public char[] Filepath;
public bool isFirst;
public uint SyncTime;
public char *pOutBuf; // example C++ usage: myStruct.pOutBuf = (char*)malloc(1920 * 1080 * 3);
public int OutBufSize;
public ushort OutImgResW;
public ushort OutImgResH;
}
// need to use dumpbin to get entrypoint name due to c++ mangling
[DllImport(@"C:\Location\To\Project\bin\FunctionLibLib.dll", EntryPoint = "?FunctionLib_Snapshot@@YAHPAU_FunctionOpt@@@Z")]
public static extern int FunctionLib_Snapshot(PSTFUNCTIONOPT pstFunctionOpt);
public unsafe int FunctionLib_Run()
{
PSTFUNCTIONOPT stFunctionOpt = new PSTFUNCTIONOPT();
stFunctionOpt.UserName = ("uname").ToCharArray();
stFunctionOpt.Password = ("pword").ToCharArray();
stFunctionOpt.ServerIP = ("192.168.1.1").ToCharArray();
stFunctionOpt.ServerPort = 80;
stFunctionOpt.Index = 255;
stFunctionOpt.Timeout = 15000;
stFunctionOpt.Filepath = ("c:\\temp\\test.jpg").ToCharArray();
stFunctionOpt.isFirst = true;
stFunctionOpt.SyncTime = 0;
//stFunctionOpt.pOutBuf = new char*[10000]; // not sure how to do this yet
stFunctionOpt.OutBufSize = 10000;
// get result from DLL
return FunctionLib_Snapshot(stFunctionOpt);
}
How do I properly pass the struct to this unmanaged DLL? 如何将结构正确传递给此非托管DLL? The error seems like something simple but I haven't been able to narrow down the problem. 该错误似乎很简单,但我无法缩小问题的范围。 Any help is appreciated! 任何帮助表示赞赏!
Several problems: 几个问题:
public char[] UserName;
That needs to be marshaled as an embedded string: 需要将其编组为嵌入的字符串:
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string UserName;
Repeat that for the other fields that are declared like this. 对其他这样声明的字段重复该操作。
public char *pOutBuf; // example C++ usage: myStruct.pOutBuf = (char*)malloc(1920 * 1080 * 3);
It is not clear whether the C++ function allocates that buffer or the client code is supposed to do that. 目前尚不清楚C ++函数是分配该缓冲区还是应该由客户端代码执行此操作。 If it is the former then you have a Big Problem, you cannot call the free() function to release the buffer again. 如果是前者,那么您有一个大问题,则无法调用free()函数再次释放缓冲区。 Your only hope is that your C# code is supposed to allocate it, not unlikely. 您唯一的希望是C#代码应该分配它,这并非没有可能。 In which case it is: 在这种情况下是:
public byte[] pOutBuf;
...
stFunctionOpt.pOutBuf = new byte[10000];
stFunctionOpt.OutBufSize = stFunctionOpt.pOutBuf.Length;
The pinvoke declaration is wrong: pinvoke声明错误:
[DllImport(@"C:\Location\To\Project\bin\FunctionLibLib.dll", EntryPoint = "?FunctionLib_Snapshot@@YAHPAU_FunctionOpt@@@Z")]
public static extern int FunctionLib_Snapshot(PSTFUNCTIONOPT pstFunctionOpt);
The argument is ref PSTFUNCTIONOPT pstFunctionOpt
. 参数为ref PSTFUNCTIONOPT pstFunctionOpt
。 Do drop the P from the structure name, it is not a pointer type. 请从结构名称中删除P,它不是指针类型。 Avoid hard-coding the path to the DLL, that isn't going to work on your user's machine. 避免对DLL的路径进行硬编码,否则将无法在用户的计算机上正常工作。 Just make sure that you have a copy of the DLL in your build output directory. 只要确保您在构建输出目录中有DLL的副本即可。
I think you are missing a couple of things from your structure definition 我认为您在结构定义中缺少了几件事
[StructLayout(LayoutKind.Sequential)]
attribute to your structure 内存中struct的布局在c ++中是不同的,因此您需要向结构中添加[StructLayout(LayoutKind.Sequential)]
属性 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = your_array_lenght)]
, moreover if you do that, the marshaler will handle conversions between string and char array. 在C ++中,您具有固定长度的char数组,因此,您还需要在c#中使用属性[MarshalAs(UnmanagedType.ByValTStr, SizeConst = your_array_lenght)]
,此外,封送处理程序将处理字符串和char数组之间的转换。 Your struct definition should then look something like 然后,您的结构定义应类似于
[StructLayout(LayoutKind.Sequential)]
public struct PSTFUNCTIONOPT
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string UserName;
...
[MarshalAs(UnmanagedType.LPStr)]
public string pOutBuf;
}
For more info check out http://msdn.microsoft.com/en-us/library/s9ts558h.aspx 有关更多信息,请访问http://msdn.microsoft.com/zh-cn/library/s9ts558h.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.