C ++中的字符串? [英] Strings in c++?

查看:124
本文介绍了C ++中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我在DLL中有一些功能,例如:

#include <string>
using namespace std;
extern "C" __declspec(dllexport) string __cdecl encryption(string s)
{
 return s;
}

每当我尝试从C#调用此函数时,这是使用以下代码的代码:

[DllImport("Packer.dll", EntryPoint = "encryption")]
static extern string encryption(string s);

我得到一个错误: 调用PInvoke函数包装器"已使堆栈不平衡.这可能是因为托管PInvoke签名与非托管目标签名不匹配.检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配.

我猜测我会收到此错误,因为我没有该函数的正确声明 任何人都可以指导我如何解决该问题,在此先感谢

解决方案

std::string不能与PInvoke一起使用,因为C ++的对象没有ABI,因此不能正确清理堆栈,复制对象等.这是C ++最大的麻烦之一.

您必须使用char*指针和普通C API.简而言之,PInvoke不适用于C ++.

My problem is , i have some functions in a DLL some of these functions are for example :

#include <string>
using namespace std;
extern "C" __declspec(dllexport) string __cdecl encryption(string s)
{
 return s;
}

Whenever i try to call this function from C# , here is the code im using :

[DllImport("Packer.dll", EntryPoint = "encryption")]
static extern string encryption(string s);

i get an error : A call to PInvoke function 'Packer' 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.

im guessing i get this error because i dont have the right declarations for the function can anyone guide me how to fix that , thanks in advance

解决方案

std::string can not be used with PInvoke, because C++ does not have an ABI for its objects which is required to properly clean stack, copy objects, etc. This is one of the greatest pains of C++.

You have to use char* pointers and plain C APIs. Simply put, PInvoke does not work with C++.

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

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