如何导入带有某些本机类型参数的函数? [英] How to import a function which takes some parameters of native types?

查看:89
本文介绍了如何导入带有某些本机类型参数的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C编写的本机DLL,其中包含以下功能:

I have a native DLL written in C, which contains this function:

SomeFunction(int cmd, int param, unsigned char* data);



在VS2005上的C#代码中,如何通过DllImport属性导入此函数?请注意,此函数需要一些本机类型的参数.

我应该以这种方式导入:



In my C# code on VS2005, how do I import this function via DllImport attribute? Note this function takes some parameters of native types.

Should I import this way:

[DllImport("SomeNativeDll")]
public static extern int SomeFunction(int cmd, int param, ref byte data);

推荐答案

它实际上取决于参数的用途.在我看来,data byte 缓冲区,因此您的P/Invoke签名应为:

It really depends on what the parameter is used for. It seems to me that data is a byte buffer, so your P/Invoke signature should be:

public static extern int SomeFunction(int cmd, int param, out byte[] data);



如果该方法希望您传递已初始化的数组,则可以使用ref 而不是out.



You may use ref instead of out, if the method expects you to pass an initialized array.


这篇关于如何导入带有某些本机类型参数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆