我在vc6中有win32 dll,并使用参数char*调用c#?
此vc6代码:我在vc6中有win32 dll,并使用参数char*调用c#?,c#,c++,dll,visual-c++-6,C#,C++,Dll,Visual C++ 6,此vc6代码: MCASMARTMANAGER_API int __stdcall reqeustKey_test(char* prKey) { Xhandeler.GetPrimaryKey(prKey); return 0; } prKey = "AB472EDB9012" 这个C#代码: 我在注册表项{'上收到的运行时い㠶㐵䘷䘰䉆ㄴ㌰'}. 我做错了什么?首先,“请求”拼写错误 其次,c#字符串是unicode(16位)。VC6字符*是ASCII(8位)。您的
MCASMARTMANAGER_API int __stdcall reqeustKey_test(char* prKey)
{
Xhandeler.GetPrimaryKey(prKey);
return 0;
}
prKey = "AB472EDB9012"
这个C#代码:
我在注册表项{'上收到的运行时い㠶㐵䘷䘰䉆ㄴ㌰'}. 我做错了什么?首先,“请求”拼写错误
其次,c#字符串是unicode(16位)。VC6字符*是ASCII(8位)。您的封送处理应该使用marshallas(UnmanagedType.LPStr)
第三,返回类型不是字符串,而是int,应该作为
marshallas(UnmanagedType.I4)进行封送
,我认为应该是一个LPStr
而不是LPWStr
。另外……如何从int
封送string
的返回值?我认为您的代码应该对string prKey
参数使用ref
。在使用非托管类型时。LPStr返回“”为空!?!!?如Alvin Wong指出,如果要设置prKey
[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_);