托管的C#字符串到未管理的C ++ DLL [英] Managed C# String to Unmanged C++DLL

查看:64
本文介绍了托管的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


这篇关于托管的C#字符串到未管理的C ++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆