所以在我的c ++ .dll文件中我得到了以下功能:
DLL void GetUserPass(char* &userName, char* &passWord)
{
userName = "ceva";
passWord = "altceva";
}
现在我想从c#中调用它,但它给了我一个错误:
[DllImport("myDLL.dll")]
private static extern void GetUserPass(ref string userName, ref string passWord);
static void f()
{
string userName ="";
string passWord ="";
GetUserPass(ref userName, ref passWord);
}
错误是:
调用PInvoke函数'Download FTP Archive!Download_FTP_Archive.Program :: GetUserPass'使堆栈失衡。这很可能是因为托管PInvoke签名与非托管目标签名不匹配。检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。
我应该尝试使用C ++ dll文件:
using std::string;
DLL void GetUserPass(string &userName, string &passWord)
{
userName = "ceva";
passWord = "altceva";
}
答案 0 :(得分:3)
尝试以下方法:
DLL void __stdcall GetUserPass(char* &userName, char* &passWord)
{
userName = "ceva";
passWord = "altceva";
}
[DllImport("myDLL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern void GetUserPass(ref IntPtr userName, ref IntPtr passWord);
static void f()
{
IntPtr userNamePtr = new IntPtr();
IntPtr passWordPtr = new IntPtr();
GetUserPass(ref userNamePtr, ref passWordPtr);
string userName = Marshal.PtrToStringAnsi( userNamePtr );
string passWord = Marshal.PtrToStringAnsi( passWordPtr );
}
答案 1 :(得分:0)
我不是c ++开发人员,但我对c ++语义有点熟悉。这char* &userName
对我没有意义。也许我错了,但试试char* userName
,看看它是否适合你。