[英]How to call an external function that takes pointers as input, loaded from a C++ dll
I have a C# project where I need to use an external dll. 我有一个C#项目,需要使用外部dll。
The dll was built using C++. dll是使用C ++构建的。 I have only the c++ header file (.hpp) that describes functions interfaces (signatures), and the .dll (binary code).
我只有描述函数接口(签名)和.dll(二进制代码)的c ++头文件(.hpp)。
I want to load the dll dynamically in my C# project and use two functions : 我想在我的C#项目中动态加载dll并使用两个函数:
void OpenService(long &handle);
int GetCode(long handle, const char* pin, char* passcode, char* nextPasscode, char* tokencode, char* nextTokencode);
I am able to load the dll using DllImport from System.Runtime.InteropServices and it works for the first function using the code bellow : 我能够使用DllImport从System.Runtime.InteropServices加载dll,并且它使用以下代码可用于第一个功能:
[DllImport(@"C:\Program Files\Path to my dll\myDll.dll")]
public static extern void OpenService(ref long handle);
But for the second one, I tried the code bellow : 但是对于第二个,我尝试了下面的代码:
[DllImport(@"C:\Program Files\Path to my dll\myDll.dll")]
unsafe public static extern int GetCode (long handle, string* pin, string* passcode, string* nextPasscode, string* tokencode, string* nextTokencode);
But the compiler is complaining that I should use the unsafe mode while compiling to be able to use C# pointers which is something I cannot do due to rules imposed by the Tech lead. 但是编译器抱怨我在编译时应该使用不安全模式才能使用C#指针,由于技术负责人强加的规则,我无法做到这一点。
So, my question is how can I call the second function without using C# pointers ? 所以,我的问题是如何在不使用C#指针的情况下调用第二个函数?
I tried to use references as bellow : 我试图将引用用作波纹管:
[DllImport(@"C:\Program Files\Path to my dll\myDll.dll")]
public static extern int GetCode (long handle, ref string pin, ref string passcode, ref string nextPasscode, ref string tokencode, ref string nextTokencode);
But the program crashes whenever I call GetCode. 但是,只要我调用GetCode,程序就会崩溃。
Any idea on how can I solve this problem ? 关于如何解决此问题的任何想法吗?
Thank you in advance. 先感谢您。
Instead of using unsafe and pointers you can marshal (transform) the string to a char* type using the MarshalAs method: 您可以使用MarshalAs方法将字符串封送(转换)为char *类型,而不是使用不安全和指针:
[DllImport(@"C:\Program Files\Path to my dll\myDll.dll")]
public static extern int GetCode (long handle, [MarshalAs(UnmanagedType.LPStr)]string pin, [MarshalAs(UnmanagedType.LPStr)]string passcode, [MarshalAs(UnmanagedType.LPStr)]string nextPasscode, [MarshalAs(UnmanagedType.LPStr)]string tokencode, [MarshalAs(UnmanagedType.LPStr)]string nextTokencode);
UnmanagedType.LPStr
means that the string will be marshalled as UnmanagedType.LPStr
表示该字符串将被编组为
A pointer to a null-terminated array of ANSI characters.
指向以NULL终止的ANSI字符数组的指针。
If you need unicode you can use UnmanagedType.LPWStr
如果需要unicode,可以使用
UnmanagedType.LPWStr
Here is more info: 这是更多信息:
https://docs.microsoft.com/en-us/dotnet/framework/interop/default-marshaling-for-strings https://docs.microsoft.com/en-us/dotnet/framework/interop/default-marshaling-for-strings
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.