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:

  1. you may need specify calling convention in dllimport attribute. native code cdecl perhaps?
  2. the return value int in native code you've mapped bool. fine if protocol non-zero return means success. if return value indicates more you'd need use int. i'd inclined use int , stay true native.

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -