我怎么能叫接受来自C#类型字符串流的参数的C ++ DLL的功能? [英] How can I call a function of a C++ DLL that accepts a parameter of type stringstream from C#?
问题描述
我要导入非托管C ++ DLL并调用一个函数,字符串流
作为参数。在C#中,没有字符串流
类,所以任何人都可以告诉我如何从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也将获得字符串流
的自己的私人执行。他们之间传递意味着你在不经意间编织两个不相干的实现在一起。对于很多项目,如果你使用的是完全相同的构建设置,它可能是没有问题的。
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.
但是,即使您使用相同的编译器,混合使用调试EXE释放DLL,你会发现栈/堆腐败和其他难以找到的问题。
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
)后,或只是一个C风格的界面就像WINAPI。
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#类型字符串流的参数的C ++ DLL的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!