托管的C#字符串到未管理的C ++ DLL

问题描述:

我发誓,我已经尝试了所有在网上找到的内容,但仍然收到PINVOKE错误.
对PInvoke函数``BioPacVideo!BioPacVideo.VideoWrapper :: SetFName的调用"已使堆栈不平衡.这很可能是因为托管的PInvoke签名与非托管的目标签名不匹配.请检查PInvoke签名的调用约定和参数.匹配目标非托管签名."

在C#端

I swear, I''ve tried everything I''ve found online, and I''m still getting a PINVOKE error.
"A call to PInvoke function ''BioPacVideo!BioPacVideo.VideoWrapper::SetFName'' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."

on the C# side

[DllImport(@".\VideoWrapper.dll")]
        public static extern void SetFName(StringBuilder FName, int FStart);
        //public static extern void SetFName([MarshalAs(UnmanagedType.LPStr)]string FName, int FStart);


在C ++方面


on the C++ side

extern "C" _declspec(dllexport) void SetFName(LPTSTR FName, int FileStart);

我立即可以看到一个错误:您使用LPStr将封送1字节以空值结尾的字符串,但是您需要依赖于平台的字符串(LPTSTR).

试试这个:
I immediately can see a mistake: you use LPStr which will marshal 1-byte null-terminated string, but you need platform-dependent string (LPTSTR).

Try this:
[DllImport(@".\VideoWrapper.dll")]
public static extern void SetFName(
    [MarshalAs(UnmanagedType.LPTStr)]string FName,
    int FStart);



—SA



—SA