简体   繁体   English

在C#中调用C / C ++方法

[英]Calling C/C++ method in C#

Suppose you have a library my_library.dll with following methods declared in it: 假设您有一个库my_library.dll,其中声明了以下方法:

int method_1(char *param1, unsigned long *param2, bool param3)
int method_2(char *param1, unsigned long *param2, bool param3, bool &param4, long &param5, char[1024] param6)

and you need to call them in C#. 并且您需要使用C#调用它们。 To call method_1 , I can do this: 要调用method_1 ,我可以这样做:

[DllImport("my_library.dll", EntryPoint = "method_1")]
static extern int mapped_method_1(string param1, UInt32 param2, byte param3)

and the mapped_method_1 can be called as regular C# method. 可以将Mapped_method_1称为常规C#方法。

Can somebody help me to do this for method_2 ? 有人可以帮我为method_2做这个吗? I am struggling how to map the "&" parameters and (possibly) the array. 我在努力映射“&”参数和(可能)数组。 I keep getting AccessViolationException - attempted to read or write protected memory 我不断收到AccessViolationException-尝试读取或写入受保护的内存

My goal is to call extraPutty API methods. 我的目标是调用extraPutty API方法。 I am able to call Connexion , but unable to call Connexion_F . 我可以打电话给Connexion ,但不能打电话给Connexion_F

http://www.extraputty.com/htmldoc/Chapter7.html http://www.extraputty.com/htmldoc/Chapter7.html

Try the following syntax: 尝试以下语法:

[DllImport("my_library.dll", EntryPoint = "method_2")]
static extern int mapped_method_2(string param1, UInt32 param2, byte param3, byte* param4, UInt32 param2, string param6)

This is what you are looking for 这就是你要找的

[DllImport("my_library.dll", EntryPoint = "method_2")]
static extern int mapped_method_2([MarshalAs(UnmanagedType.LPWStr)]string param1, 
UInt32 param2, bool param3, 
ref bool param4,  ref long param5,
ref char[] param6);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM