如何解决如何在C#中调用使用指针作为参数的DLL函数C ++?
我正在尝试在C#中调用一个称为Function_A(Class1 * input_class)的DLL函数(用C ++编写)。这是我在C#中声明的类。我已经读过有关在Function_A声明中某处使用ref关键字的信息,但不确定。任何帮助将不胜感激。谢谢!
[DllImport("The_DLL.dll")]
public static extern int Function_A(Class1* input_class);
public struct Class1
{
int vara;
int varB;
}
public struct Class2
{
class1 class1_A;
class1 class1_B;
}
static void Main(string[] args)
{
Class2 a = new Class2();
Function_A(...);
}
解决方法
[DllImport("The_DLL.dll")]
public static extern int Function_A([In,Out]Class1 input_class);
[DllImport("The_DLL.dll",EntryPoint="Function_A")]
public static extern int Function_A2(ref Class1 input_class);
[StructLayout(LayoutKind.Sequential)]
public struct Class1 //a bad name for a struct but hey...
{
int varA;
int varB;
}
static void Main(string[] args)
{
Class1 x = new Class1();
Function_A(x); // your struct is marshalled so that changes by the external function are marshalled back
Function_A2(x); // your struct is marshalled so that changes by the external function are NOT marshalled back
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。