Click here to Skip to main content
11,496,146 members (791 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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
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...
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 Dnyaneshwar@Pune 692
1 RyanDev 230
2 CHill60 163
3 Sascha Lefèvre 145
4 Sergey Alexandrovich Kryukov 124
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 1 Aug 2011
Copyright © CodeProject, 1999-2015
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