This vc6 code :
MCASMARTMANAGER_API int __stdcall reqeustKey_test(char* prKey)
{
Xhandeler.GetPrimaryKey(prKey);
return 0;
}
prKey = "AB472EDB9012"
And this C# code:
[DllImport(McaSmartManagerDllPath, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
[return:MarshalAs(UnmanagedType.LPStr)]
public static extern string reqeustKey_test([MarshalAs(UnmanagedType.LPWStr), In, Out] string prKey);
var key_ = new string(' ', 17);
_strPrimaryKey = McaSmartNativeCommand.reqeustKey_test(key_);
Runtime I received on key_ {'い㠶㐵䘷䘰䉆ㄴ㌰'}. What am I doing wrong?
First, "request" is misspelled.
Second, ac# string is unicode (16 bit). A VC6 char* is ASCII (8 bit). Your MarshalAs should be using MarshalAs(UnmanagedType.LPStr)
Third, your return type isn't a string, it's an int, and should be marshaled as MarshalAs(UnmanagedType.I4)
,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.