How to marshall byte* from C.dll in C# -
i have 2 functions trying call c# shares similar signatures:
bool read (byte len, byte* databuf) bool write (byte len, byte* databuf)
from doc: databuf destination of transmitted data
what shall use in c# call?
- byte[]
- mybytearr[0]
- p/invoke assistant suggested system.intptr
don't have hardware test yet, trying many of calls right when have.
thanks.
for read function use:
[out] byte[] buffer
for write function use:
[in] byte[] buffer
[in]
default , can omitted not hurt explicit.
the functions therefore be:
[dllimport(filename, callingconvention = callingconvention.cdecl)] static extern bool read(byte len, [out] byte[] buffer); [dllimport(filename, callingconvention = callingconvention.cdecl)] static extern bool write(byte len, [in] byte[] buffer);
obviously you'll need allocate array before passing unmanaged functions.
because byte
blittable marshaller, optimisation, pins array , passes address of pinned object. means no copying performed , parameter passing efficient.
Comments
Post a Comment