如何调用 C++ DLL 的函数,该函数接受来自 C# 的 stringstream 类型的参数? [英] How can I call a function of a C++ DLL that accepts a parameter of type stringstream from C#?

查看:24
本文介绍了如何调用 C++ DLL 的函数,该函数接受来自 C# 的 stringstream 类型的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想导入一个非托管 C++ DLL 并调用一个以 stringstream 作为参数的函数.在 C# 中,没有 stringstream 类,所以谁能告诉我如何从 C# 程序中调用这样的函数?

I want to import an unmanaged C++ DLL and call a function that takes stringstream as a parameter. In C#, there is no stringstream class, so can anyone tell me how to call such a function from a C# program?

推荐答案

您不应通过 DLL 句点公开模板化对象.

You should not expose templated objects via a DLL, period.

模板化对象(例如,std:: 中的几乎所有内容)都被内联了.因此,通过这种方式,您的 DLL 获得了自己的私有实现副本.调用您的 DLL 的模块还将获得自己的 stringstream 私有实现.在它们之间传递意味着您无意中将两个不相关的实现编织在一起.对于许多项目,如果您使用完全相同的构建设置,那可能没问题.

Templated objects (e.g. almost everything in std::) become inlined. So in this way, your DLL gets its own private copy of the implementation. The module calling your DLL will also get its own private implementation of stringstream. Passing between them means you are inadvertently weaving two unrelated implementations together. For many projects, if you are using the exact same build settings, it's probably no problem.

但即使您使用相同的编译器,并且将发布 DLL 与调试 EXE 混合使用,您也会发现堆栈/堆损坏和其他难以发现的问题.

But even if you use the same compiler, and mix a release DLL with a debug EXE, you will find stack / heap corruption and other harder-to-find problems.

那只是使用来自另一个非托管 C++ exe/dll 的 DLL.然后跨越到 .NET 的界限更是一个问题.

And that's only using your DLL from another unmanaged C++ exe/dll. Crossing then the lines to .NET is even more of a problem.

解决方案是将 DLL 的接口更改为跨 DLL 边界友好的接口.COM(例如,您可以使用 IStream),或者只是像 winapi 这样的 C 样式接口.

The solution is to change your DLL's interface to something that plays friendly across DLL bounds. Either COM (you could use IStream for example), or just a C-style interface like the winapi.

这篇关于如何调用 C++ DLL 的函数,该函数接受来自 C# 的 stringstream 类型的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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