非托管到托管(.Net)
是否有可能将非托管 DLL 转换为托管 DLL?
Is there any possibility in converting an unmanaged DLL into a managed DLL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否有可能将非托管 DLL 转换为托管 DLL?
Is there any possibility in converting an unmanaged DLL into a managed DLL?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
包装类
您可以为 DLL 编写一个包装类。这些都很受欢迎。第三方库提供商通常提供此类包装器 .NET 程序集,用于包装其 DLL API(用 C++、C 等编写)。
在包装器类中,只需
DllImport
库 DLL 中的所有 API 函数。然后您可以像使用任何 .NET 类方法一样使用这些函数。然而,在实践中,由于您需要提供参数类型映射和其他检查,您有时必须编写自己的 .NET 兼容方法来调用这些导入的函数。
还要考虑提供方便的重载方法,因为 CLR 支持它们,但并非所有非托管库都支持。
COM Interop
或者,如果该非托管 DLL 是一个 COM 模块,而您只想按原样使用它,并且如果您使用的是 Visual Studio,则只需“添加引用”即可,Visual Studio 将自动为它创建一个 Interop 包装类你。
类型库导入
如果您的非托管 DLL 不是 COM 模块,但您仍希望将其自动转换为符合 CLR 的程序集(无需编写包装类),则必须在 IDL 文件中记录所有 API 函数。通常,如果您的库是 C++ 或 C DLL,它们将具有 C++/C 源格式的头文件,可以轻松地将其重构为 IDL 文件(使用 C 语法)。然后使用
midl
为该 DLL 创建一个类型库。然后,您可以使用 tlbimp 将该类型库转换为 .NET 程序集。Wrapper classes
You can write a wrapper class for the DLL. These are quite popular. Third-party library providers usually provide such wrapper .NET assemblies that wrap their DLL API (written in C++, C etc.)
In your wrapper class, just
DllImport
all the API functions in the library DLL. Then you can use those functions just like any .NET class method.However, in practice you'll sometimes have to write your own .NET-compliant methods that call those imported functions, because of argument types mapping and other checkings you'll want to provide.
Also consider providing convenient overloaded methods because CLR supports them and not all non-managed libraries do.
COM Interop
Or, if that unmanaged DLL is a COM module and you just want to use it as is, and if you are using Visual Studio, you can simply "Add Reference" to it and Visual Studio will automatically create an Interop wrapper class for you.
Type library import
If your unmanaged DLL is not a COM module, but you still want to have it automatically convert into a CLR-compliant assembly (without having to write a wrapper class), you must document all the API functions in an IDL file. Usually, if your library is a C++ or C DLL, they'll have header files in C++/C source format which can be readily refactored into an IDL file (which uses C syntax). Then use
midl
to create a type library for that DLL. You can then convert that type library into a .NET assembly withtlbimp
.如果您有源代码并且它是 C++,则可以使用 C++/CLI。如果是Delphi,您可以使用Delphi Prism(没有个人经验)。这不是一个简单的“打开并重新编译”,但据我所知,它相当简单,除非您正在做一些疯狂的事情(好吧,哪个 C++ 开发人员不这样做?:-))
如果您没有源代码您无法转换它,但仍然可以通过 P/Invoke 从托管代码中使用它。
If you have the Source Code and it's C++, you can use C++/CLI. If it's Delphi, you can use Delphi Prism (no personal experience with it). It's not a simple "Open and Recompile", but from what I've heard it's reasonably straight-forward unless you are doing crazy stuff (Well, which C++ developer doesn't? :-))
If you do not have the source code you can't convert it, but you can still use it from managed code through P/Invoke.