开发者

Help Marshaling a C function in C#

I'm trying to call a C function from C#.

Here is the function from the C header file :

int __stdcall GetImageKN (unsigned short *ndat );

And from the documentation about this function :

ndat :

Pointer of the grayscale image data acquiring buffer.

Always secure the rang开发者_如何学Pythone image storage area using the application program.

The size of the range image data storage area should be:

160’ 120 ‘2 = 38400 bytes

The grayscales are returned as 8 bit that is doubled from 7-bit.

How do I invoke this function and read the image data ?

Thanks,

SW


30Kb is a small buffer. If your function runs quickly, you can rely on default marshaling behaviour and do this:

[DllImport ("your.dll")]
extern int GetImageKN (short[] ndat) ;

var buffer = new short[160 * 120] ;
var result = GetImageKN (buffer)  ;

Even if it can block for a long time, you can get away with this if you don't call this function on many threads at once.


[DllImport ("your.dll")]
extern int GetImageKN (IntPtr ndat);

will probably do...

EDIT

generally pointers are represented as IntPtr. you can create a managed array and Marshal it to IntPtr,


I would try the following:

[DllImportAttribute("your.dll", CallingConvention=CallingConvention.StdCall)]
extern int GetImageKN(
    [Out, MarshalAs(UnmanagedType.LPArray, SizeConst=38400)] ushort[] ndat);

Not really sure however.


ushort ndat= 123;

GetImageKN(ref ndat);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜