Question

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?

Was it helpful?

Solution

First, "request" is misspelled.

Second, a c# 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),

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top