c++ - C# P-Invoke:how to convert INT fun( BYTE *bStream, UINT16 *nCount, const UINT8 nblk ) to C#? -
with c/c++ dll sdk fun,like this:
int cmdgetalllog( byte *bstream, uint16 *ncount, const uint8 nblk ) but in project use c#,i with:
[dllimport("c:\\prbioapi.dll", entrypoint = "cmdgetalllog")] private static extern bool cmdgetalllog(intptr bstream, ref uint16 ncount, byte nblk); and use with:
int nmallocsize = marshal.sizeof(new log_record()) * stusystem.wlogcnt + 4096; byte[] precord = new byte[nmallocsize]; intptr p = marshal.allochglobal(marshal.sizeof(nmallocsize)); marshal.copy(precord, 0, p, precord.length); bgetsucc = cmdgetalllog(p, ref nget, nblk++); marshal.freehglobal(p); but did not work. can me ?thanks.
your code copies between managed array, , unmanaged pointer, in wrong place. need after call unmanaged function.
but may let p/invoke marshaller work you:
[dllimport(@"c:\prbioapi.dll")] private static extern bool cmdgetalllog( byte[] bstream, ref ushort ncount, byte nblk ); int nmallocsize = ...; byte[] precord = new byte[nmallocsize]; bool bgetsucc = cmdgetalllog(precord, ref nget, nblk++); because byte array blittable marshaller pin array during call , hand off native code.
i'm assuming other 2 parameters passed correctly. since did not specify more details of interface, wrong. i'd guess nget used tell function how big buffer is, , return how copied function. cannot see specify nget in question. i'm trusting got bit right.
some other comments:
- you may need specify calling convention in
dllimportattribute. native codecdeclperhaps? - the return value
intin native code you've mappedbool. fine if protocol non-zero return means success. if return value indicates more you'd need useint. i'd inclined useint, stay true native.
Comments
Post a Comment