我已經在C++中編寫了一個DLL。其中一個函數寫入字符數組。將字符串從原生C++ DLL傳遞到C#應用程序
C++函數
EXPORT int xmain(int argc, char argv[], char argv2[])
{
char pTypeName[4096];
...
//Other pTypeName ends up populated with "Portable Network Graphics"
//This code verifies that pTypeName is populated with what I think it is:
char szBuff[64];
sprintf(szBuff, pTypeName, 0);
MessageBoxA(NULL, szBuff, szBuff, MB_OK);
//The caption and title are "Portable Network Graphics"
...
//Here, I attempt to copy the value in pTypeName to parameter 3.
sprintf(argv2, szBuff, 0);
return ret;
}
C#導入
//I believe I have to use CharSet.Ansi because by the C++ code uses char[],
[DllImport("FirstDll.dll", CharSet=CharSet.Ansi)]
public static extern int xmain(int argc, string argv, ref string zzz);
C#函數
private void button2_Click(object sender, EventArgs e)
{
string zzz = "";
int xxx = xmain(2, @"C:\hhh.bmp", ref zzz);
MessageBox.Show(zzz);
//The message box displays
//MessageBox.Show displays "IstuÈst¼ÓstÄstlÄstwÄstiÑstõÖstwÍst\
// aÖst[ÖstÃÏst¯ÄstÐstòÄstŽÐstÅstpÅstOleMainThreadWndClass"
}
我試圖通過引用傳遞從C#的參數和有C++ DLL填充參數。即使我已經驗證該值在DLL中是正確的,亂碼仍會傳遞給C#應用程序。
如何才能將正確的字符串值寫入C#字符串?
嘗試創建在C#項目 「不安全」 的空間。 – dotTutorials