在調用飛易來C/C++ dll中有如下接口,調用該接口需要傳遞中文字符串InputStr。我需要將我的中文字符通過該接口傳遞給dll。
從網上資料查到
C++中的PBYTE對應C#中的ref byte。
PBYTE 就是 BYTE *,BYTE就是unsigned char。
char* 對應C#中的Byte[]
所以我該傳遞的是byte[]類的數據。
[DllImport("msdk.dll", EntryPoint = "M_KeyInputStringGBK", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int M_KeyInputStringGBK(IntPtr m_hdl, ref byte InputStr, int InputLen);
最終代碼如下:
string str = "這是測試ABC@123!""
Encoding gbk = Encoding.GetEncoding("gbk");
byte[] inputByte = gbk.GetBytes(str);
int res = Msdk.M_KeyInputStringGBK(M_Handle, ref inputByte[0], inputByte.Length);
調用成功。請注意,在調用函數時,傳遞的是 ref inputByte[0],也就是第一個byte的首地址。