如何在 C# 中编写我自己的包装器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/749520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:41:41  来源:igfitidea点击:

How to write my own wrapper in C#?

c#c++wrapper

提问by Skuta

In my other question, I've been trying to find someone who knows where to find free open source OCR library for C#. However, it seems there is only C++ library and obviously, C++ is not C#.

在我的另一个问题中,我一直在寻找知道在哪里可以找到 C# 的免费开源 OCR 库的人。但是,似乎只有 C++ 库,显然,C++ 不是 C#。

One of the responds suggested to write my own wrapper in C#. I have pretty much no idea how to do it, where to learn to do it, or what it actually is - except I believe it's calling C++ methods in dll by changed method calls, .. that's just assumption, indeed.

其中一个回应建议用 C# 编写我自己的包装器。我几乎不知道怎么做,在哪里学习做它,或者它实际上是什么 - 除了我相信它通过更改的方法调用在 dll 中调用 C++ 方法,..这确实只是假设。

Please, help out one more newbie. Thank you

请再帮助一位新手。谢谢

采纳答案by Sunny Milenov

You can get a good start here:

你可以在这里有一个好的开始:

Call Unmanaged Code. Part 1 - Simple DLLImport

调用非托管代码。第 1 部分 - 简单的 DLLImport

回答by Not Sure

If you're wrapping is very simple, you can get away with P/Invoke. If you actually want managed objects, your easiest solution is probably to write the wrapper in C++/CLI (formerly Managed C++), as that provides a full feature set for managed/unmanaged interop.

如果您的包装非常简单,则可以使用P/Invoke。如果您确实需要托管对象,那么最简单的解决方案可能是在 C++/CLI(以前称为托管 C++)中编写包装器,因为它为托管/非托管互操作提供了完整的功能集。

回答by Steve

here's how I do it Interop. It shows you how to do the simple cases (BOOL to bool) as well as a bit of a harder case (passing a struct with pointers to managed memory). Good luck.

这就是我如何做到Interop。它向您展示了如何处理简单的情况(BOOL 到 bool)以及一些更困难的情况(传递带有指向托管内存的指针的结构)。祝你好运。