【问题标题】:i have win32 dll in vc6 and call on c# with parameter char*?我在vc6中有win32 dll并用参数char *调用c#?
【发布时间】:2012-11-29 06:51:35
【问题描述】:

这个vc6代码:

MCASMARTMANAGER_API int  __stdcall reqeustKey_test(char* prKey)
{    
    Xhandeler.GetPrimaryKey(prKey);
    return 0;
}

prKey = "AB472EDB9012"

还有这个 C# 代码:

[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_);

我在 key_ {'い㠶㐵䘷䘰䉆ㄴ㌰'} 上收到的运行时。我做错了什么?

【问题讨论】:

  • 我认为应该是LPStr 而不是LPWStr。另外...您如何从int 编组string 的返回值???我认为您的代码应该使用 ref 作为您的 string prKey 参数。

标签: c# c++ dll visual-c++-6


【解决方案1】:

首先,“request”拼写错误。

其次,c# 字符串是 unicode(16 位)。 VC6 char* 是 ASCII(8 位)。您的 MarshalAs 应该使用 MarshalAs(UnmanagedType.LPStr)

第三,你的返回类型不是字符串,是整数,应该编组为MarshalAs(UnmanagedType.I4),

【讨论】:

  • 使用时UnmanagedType.LPStr返回''为空!?!!?
  • 正如 Alvin Wong 指出的那样,如果您想设置 prKey 的值,您还应该使用 ref 字符串(或 out 字符串,更有可能)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多