使用 c++/cli 中的 std::string 参数调用本机 c++ dll
Calling a native c++ dll with std::string argument from c++/cli
我有一个本机的c ++ dll,我正在尝试从c ++/cli项目中调用它。 这是 dll 的函数
extern "C"
{
int DLL_EXPORT Add(std::string s1, std::string s2, std::string s3)
{
[do stuff]
}
}
以下是 c++/cli 中的参考:
using namespace System::Runtime::InteropServices;
[DllImport("my_dll.dll")]
extern "C" int Add(std::string, std::string, std::string);
当我调用该函数时,我将 String^ 对象封送到 std::string:
Add(msclr::interop::marshal_as<std::string>(stringA),
msclr::interop::marshal_as<std::string>(stringB),
msclr::interop::marshal_as<std::string>(stringC));
调用 DLL 时出现访问冲突异常。
DLL
和CLI是否使用相同的编译器编译?通常,不同的编译器可能具有不同的std::string
定义,这可能会导致错误。
因此,我一般不建议在 DLL 接口中使用std::string
或任何编译器特定类型。
我想这就是你使函数extern "C"
的原因。如果它没有extern "C"
就不起作用,它很可能不起作用(因为不同的重整规则,这意味着不同的编译器或至少不同的std::string
定义)。
在这种情况下,我更愿意创建一个接受const char *
指针的包装函数。
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 使用指向成员的指针将成员函数作为参数传递
- 没有名称的C++模板参数
- 如何将enable-if与模板参数和参数包一起使用