无法将托管c#中的字符串传递给非托管c

时间:2014-12-27 16:50:36

标签: c# c marshalling dllimport

我已经尝试过这个站点中显示的多个方法来将字符串从c#传递给c,但是C代码中的函数不断变为空参数。

这是我的C函数:

__declspec(dllexport) BOOL __cdecl GetSignatureInfo(char* fileName, char* output)

和我的c#用法:

[DllImport("GetSignature.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int GetSignatureInfo([MarshalAs(UnmanagedType.LPStr)]string filePath, StringBuilder output);

我在这里做错了吗? 提前谢谢。

编辑:这是我调用函数的方式:

 StringBuilder output = new StringBuilder(256);
    string spath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
    Console.WriteLine(Detect.GetSignatureInfo(spath, output));

1 个答案:

答案 0 :(得分:1)

问题是字符串格式不正确。将lstrcpynW切换为mbstowcs,现在可以正常使用。

相关问题