如何从C#调用C ++ DLL方法(将const char *作为输入参数和返回值) [英] How do I call a C++ dll method (having const char* as input params and return value) from C#
问题描述
我在C#项目中添加了一个C ++库DLL。
我可以在C#中调用下面的C ++方法。
以下是C ++中的方法定义
I have added a C++ library DLL in my C# project.
I am able to call the below method of C++ in my C#.
Below is method definition in C++
static APICLIENT_API char* logIn1(const char* url, const char* loginID, const char* password, const char* salt, const char* timeStamp, const char* refererURL, int iterations)
我试图通过许多不同的方式在C#中调用此方法但是找到了N. o运气。
我的最后一次尝试是
I tried to call this method in C# through many different ways but found No Luck.
My last try was
[DllImport(@"F:\bin\APIClient.dll", EntryPoint = "?logIn1@CAPIClient@API@@SAPADPBD00000H@Z", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string logIn1([MarshalAs(UnmanagedType.LPStr)]string a, [MarshalAs(UnmanagedType.LPStr)]string b, [MarshalAs(UnmanagedType.LPStr)]string c, [MarshalAs(UnmanagedType.LPStr)]string d, [MarshalAs(UnmanagedType.LPStr)]string e, [MarshalAs(UnmanagedType.LPStr)]string f, int g);
string myStr= APICaller.logIn1("https://api.net/api/0.1/session/login","James@gpla.com", "123123","", "1403762225", "https://api.net/", 4096);
但它也是没有用。(Visual Studio崩溃)
注意:如果C ++方法的数据类型很简单,如int或double,那么我就可以调用它们。 br />
请帮忙解决这个问题。
But it also didn't work.(Visual Studio crashes)
NOTE: If the datatypes of the C++ methods are simple like int or double then I am able to Call them .
Please help in resolving this.
推荐答案
一开始:不要使用完整的补丁。 dll属于你构建的输出目录(每个副本)
我多年来一直使用Stringbuilder对象进行这些任务。分配一些大小和大小作为第二个参数。
at first: dont use full pathes. The dll belongs in the output dir of your build (per copy)
I use Stringbuilder objects for these taskes for years. Allocated with some size and the size as second parameter.
StringBuilder sb = new StringBuilder(32);
MycppFunc( sb, 32 );
所以我知道我有多少空间。
阅读此 MSDN文章了解详情。重要的是使用认可的字符集。
So I know how many space I have.
Read this MSDN article for the details. Important is the use of the approbiate charset.
这篇关于如何从C#调用C ++ DLL方法(将const char *作为输入参数和返回值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!