简体   繁体   中英

Calling C/C++ method in C#

Suppose you have a library my_library.dll with following methods declared in it:

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#. To call method_1 , I can do this:

[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.

Can somebody help me to do this for 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

My goal is to call extraPutty API methods. I am able to call Connexion , but unable to call Connexion_F .

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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