C#中调用C++写的DLL时字符串交互的问题
根据项目要求,需要在一个WinForm程序中调用C++写的DLL,DLL中有一个导出函数,返回值是MFC中的CString,函数如下:
const CString ExecuteCMD(const char* ParamIn){//…………CString strResponse;int iRet = ((CZJRCIApp*)AfxGetApp())->ExecuteTellerCash();if (iRet != ZJRCI_SUCCESS){strResponse.Format("RetCode=[%d]",iRet);}else{XmlPackage response = ((CZJRCIApp*)AfxGetApp())->GetResponse();strResponse = response.GetXmlDataAlign();}return strResponse;
}
在C#中的调用如下:
[DllImport("ZJRCI.dll",CallingConvention=CallingConvention.Cdecl)]
public static extern string ExecuteCMD(string strXML);
private void button_Click(object sender, EventArgs e)
{
string strXML = "<?xml version=\"1.0\" encoding=\"gb2312\"?><root></root>";
string str = ExecuteCMD(strXML);
this.TextView.Text = str;
}
结果是在C#中返回给str的值是乱码,后来查了许多资料后发现,C#不支持MFC的CString,只能将DLL中的函数返回值改成了char*或者LPCTSTR
浙公网安备 33010602011771号