Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET VC++
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 10:48am
bk5151125
Rate this: bad
good
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.
  Permalink  
Comments
SAKryukov at 2-Aug-11 0:01am
   
My 5. There is another little known but robust method -- please see my solution.
--SA
Philippe Mori at 2-Aug-11 7:48am
   
I have never heard of it before...
Rate this: bad
good
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[^].
 
—SA
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 85
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 1 Aug 2011
Copyright © CodeProject, 1999-2014
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