如何调用 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不应该通过 DLL 公开模板化对象。
模板化对象(例如,
std::
中的几乎所有内容)都变成内联的。这样,您的 DLL 就获得了它自己的私有实现副本。调用 DLL 的模块还将获得其自己的stringstream
私有实现。在它们之间传递意味着您无意中将两个不相关的实现编织在一起。对于许多项目,如果您使用完全相同的构建设置,则可能没有问题。但即使您使用相同的编译器,并将发布 DLL 与调试 EXE 混合在一起,您也会发现堆栈/堆损坏和其他更难发现的问题。
这只是使用另一个非托管 C++ exe/dll 中的 DLL。那么跨界到 .NET 就更成问题了。
解决方案是将 DLL 的接口更改为跨 DLL 边界友好运行的接口。可以是 COM(例如,您可以使用 IStream),也可以只是像 winapi 这样的 C 风格接口。
You should not expose templated objects via a DLL, period.
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 ofstringstream
. 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.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.
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.
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++ 包装器项目,导入其他 C++ dll,将其导出为托管函数,然后从 C# 代码中调用它。 C++ 互操作真的很糟糕。
If you can modify the C++ dll, export a plain string version. Otherwise you have to build a managed C++ wrapper project, import the other C++ dll, export it as a managed function, and call that from your C# code. C++ interop really sucks.
恐怕您必须在 C# 中创建自己的
StringStream
类,以便能够使用从该 DLL 导出的函数。正如您所提到的,.NET Framework 不提供任何开箱即用的类似类。最简单的方法可能是包装 .NET Framework 提供的 StringBuilder 类,使其可以充当流。请参阅这篇博文< /a> 以获得进一步的解释和一些示例代码。
MSDN 杂志也回答了类似的问题:http://msdn.microsoft。 com/en-us/magazine/cc163768.aspx。您可能会发现其中给出的一些提示和/或示例代码很有用。
I'm afraid that you're going to have to create your own
StringStream
class in C# in order to be able to consume the functions exported from that DLL. As you mentioned, the .NET Framework doesn't provide any similar class out of the box.The easiest way is probably to wrap the
StringBuilder
class provided by the .NET Framework such that it can function as a stream. See this blog post for a further explanation and some sample code.A similar question was also answered in MSDN Magazine: http://msdn.microsoft.com/en-us/magazine/cc163768.aspx. You might find some of the hints and/or sample code given there useful.
您正在尝试将本机 C++ 代码绑定到 C# 中的托管代码。一般来说,最好的方法是在托管 C++ 中引入中间层,该中间层将为来自 C# 的调用提供接口。
You are trying to bind native C++ code to managed code in C#. Best way of doing that in general is to introduce middle layer in managed C++ that will provide interface to calls from C#.
使用 c 或 c++ 创建一个 Wrapper dll,公开对该函数的友好调用。这是更好的方法。
例如
Create an Wrapper dll in c, or c++ that exposes an friendly call to that function. It's the better way.
for example an