Click here to Skip to main content
15,936,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a dll that is written in C# and I would like to use it in my Unmanaged VC++ application. Every way I find is convoluted and requires that I change the DLL code itself. What if I do not have access to the DLL source code. Any help would be appreciated.

You should either compile your C++ code as managed (mixed-mode) or add a managed (mixed-mode) C++ DLL to your project.

Which one you choose will depend mainly on your existing code. If you application is quite complex, then it might be difficult to convert it so that it would compile and works with /clr. In that case, adding an extra DLL might be the simpler solution to avoid conflicts that might otherwise arise.

But if you application can easily be compiled and properly works with /clr, you would avoid an extra indirection doing that way.
Share this answer
Sergey Alexandrovich Kryukov 2-Aug-11 0:01am    
My 5. There is another little known but robust method -- please see my solution.
Philippe Mori 2-Aug-11 7:48am    
I have never heard of it before...
There is a method of exporting methods from a .NET assembly as unmanaged, so it can be used as a regular unmanaged DLL by the unmanaged codes, C++ or not. This method is not well-known, but it is strictly based on the CLR standard. The problem is that is is not possible to achieve with high-level CLR languages but with IL it can be done.

The trick it to provide this functionality to high-level CLR (.NET) languages. The idea is to introduce a special attribute use to make a method as "export to unmanaged". A build tool should disassemble the assembly from its binary form to IL, modify IL code using the attribute to add the export and assemble it from modified IL to executable assembly again. A resulting assembly can be used as a regular unmanaged DLL. This build step is fully automated; and the used does not need to be familiar with IL.

You can find the solution in the following CodeProject articles:
Unmanaged code can wrap managed methods[^],
How to Automate Exporting .NET Function to Unmanaged Programs[^].

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900