SafeArrayTypeMismatchException when passing c# struct to unmanaged C++ DLL -
hi i'm new c# , c++ programming , trying use unmanaged c++ dll in c# project (.net 3.5). i'm stuck on error:
system.runtime.interopservices.safearraytypemismatchexception: specified array not of expected type.
here header file 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 c++ code can change following c# code. relevant code 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 use dumpbin entrypoint name due 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 yet stfunctionopt.outbufsize = 10000; // result dll return functionlib_snapshot(stfunctionopt); }
how pass struct unmanaged dll? error seems simple haven't been able narrow down problem. appreciated!
several problems:
public char[] username;
that needs marshaled embedded string:
[marshalas(unmanagedtype.byvaltstr, sizeconst = 32)] public string username;
repeat other fields declared this.
public char *poutbuf; // example c++ usage: mystruct.poutbuf = (char*)malloc(1920 * 1080 * 3);
it not clear whether c++ function allocates buffer or client code supposed that. if former have big problem, cannot call free() function release buffer again. hope c# code supposed allocate it, not unlikely. in case is:
public byte[] poutbuf; ... stfunctionopt.poutbuf = new byte[10000]; stfunctionopt.outbufsize = stfunctionopt.poutbuf.length;
the pinvoke declaration wrong:
[dllimport(@"c:\location\to\project\bin\functionliblib.dll", entrypoint = "?functionlib_snapshot@@yahpau_functionopt@@@z")] public static extern int functionlib_snapshot(pstfunctionopt pstfunctionopt);
the argument ref pstfunctionopt pstfunctionopt
. drop p structure name, not pointer type. avoid hard-coding path dll, isn't going work on user's machine. make sure have copy of dll in build output directory.
Comments
Post a Comment