开发者

Unmanaged to Managed(.Net)

Is there any possibility in conve开发者_StackOverflowrting an unmanaged DLL into a managed DLL?


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 with tlbimp.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜