Click here to Skip to main content
12,999,760 members (41,167 online)
Rate this:
Please Sign up or sign in to vote.
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.
Posted 1-Aug-11 9:48am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
SAKryukov 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...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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[^].


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 1 Aug 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100