Click here to Skip to main content
15,868,127 members
Articles / Desktop Programming / MFC
Article

Unmanaged to Managed Calls

Rate me:
Please Sign up or sign in to vote.
4.96/5 (42 votes)
3 Apr 2003CPOL3 min read 234K   3K   57   36
Call managed code from unmanaged code
Image 1

Introduction

If you're developing in a pure .NET environment count yourself lucky. Many of us are still working with languages that, nicely put, are somewhat less than recent. Many have vivid memories of working with software like FoxPro 2.6 which is roughly 10 years old. Unfortunately, in these environments starting over isn't always possible, and so integration of new technology is often a necessity. It can often save development time, provide more robust solutions, increase efficiency and speed, or even preserve your sanity when you're asked to modify that 'particular' application.

One such integration solution is to make your .NET libraries available to existing code through the use of exported functions which reside in a managed C++ bridge dll. In many instances this solution proves adequate and even simpler than other such techniques using COM and other facilities that may or may not be available to you.

The .NET Library

The first thing to do is to develop a .NET library for your required task. There are a variety of ways to expose your .NET solution but the simplest is probably via a class library which is the method used here. Keep in mind that you'll need to provide access to this library through your bridge dll. You may be fortunate enough to be able to expose that library through a single function call (i.e. 'object.do_this' ). However, you may need to preserve state between your calling application and your library, using handles, argument passing, etc.

A tip for those of you unfamiliar with the conversion between managed and unmanaged types; you may want to increase your effort to minimize arguments to the functions which will be called by your bridge dll. Minimizing arguments can often save headaches. Conversion of arguments from unmanaged to managed types and back again (if arguments are begin returned) can be one of the most time consuming parts of the process.  You may be using char *'s, CStrings, arrays, etc. They each have their own intricacies.  Once you've done it a few times though it becomes less of an issue.

The Bridge

The bridge is simply a managed C++ dll in which you export functions with the familiar dllexport attribute. From this dll you'll access your .NET library. Once you've included your .NET dll with the #using directive and optionally imported the namespace, the facilities in your .NET library are available to use. You may need only a single exported function here to provide access to your library, or you may need several. You might want to keep your bridge dll as simple as possible, a bridge only. However, you may want to provide facilities within the bridge specific to the interaction between the calling application and the library. This may provide a more elegant solution for those attempting to implement isolation and data hiding techniques.

The Calling Application

The calling application simply requires the ability to load a dll and make calls to exported functions. The example given in the source is a simple unmanaged C++ console application. It makes calls to several bridge functions and prints the output to the console window. 

The rest of the details are left to the source code which can be downloaded above. The process is relatively simple and so the source included should provide more than enough detail to get you started. Coordinating 3 projects during the debugging process can be tricky so they have all been included in a single solution with a common output directory (/common) in the root of the solution. The bridge dll and console (calling) application build debug output to the common directory. However, the vb.NET library build will not do this so you will need to copy that build to the common directory. All of the paths in the solution are relative, but be sure to check those if you encounter any problems, especially those related to dlls not being loaded, etc.

License

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


Written By
Architect
United States United States
Michael has been developing software for about 19 years primarily in C#, C/C++, Fortran, and Visual Basic. His previous experience includes Internet data services (communication protocols, data storage and GIS) for the mortgage industry, oil platform instrumentation and explosives simulation and testing. He holds a B.S. in astrophysics and computer science. He is currently working for Global Software in Oklahoma City developing law enforcement and emergency services related software.

Comments and Discussions

 
GeneralDeployment problem! Pin
sam2519-Jan-10 14:36
sam2519-Jan-10 14:36 
GeneralSending byte array from unmanaged to managed C++ Pin
Member 476777810-Aug-09 21:21
Member 476777810-Aug-09 21:21 
GeneralNice but how about already allocated Managed Object Pin
nawash11-Jun-09 5:41
nawash11-Jun-09 5:41 
GeneralModified version of bridge program for C++/CLI, for Visual Studio 2005/2008 Pin
RenniePet8-May-08 3:09
RenniePet8-May-08 3:09 
QuestionAdditional Dependencies Pin
Barry.Voce2-May-08 2:35
Barry.Voce2-May-08 2:35 
Generalunhandled exception Pin
setori8811-Mar-08 18:27
setori8811-Mar-08 18:27 
GeneralRe: unhandled exception Pin
setori8812-Mar-08 16:57
setori8812-Mar-08 16:57 
GeneralRe: unhandled exception Pin
Mark Horton25-Jun-09 9:44
Mark Horton25-Jun-09 9:44 
GeneralRe: unhandled exception Pin
negm_phlestin19-Jun-10 22:00
negm_phlestin19-Jun-10 22:00 
hello , I have this message also , how you solved it ?
GeneralRe: unhandled exception Pin
negm_phlestin20-Jun-10 3:54
negm_phlestin20-Jun-10 3:54 
GeneralRe: unhandled exception Pin
fastfred200025-Jan-11 22:08
fastfred200025-Jan-11 22:08 
GeneralSmart device problem Pin
Beorne20-Jan-08 22:48
Beorne20-Jan-08 22:48 
Generalwindows has triggered a breakpoint... Pin
InstallerGuy10-Sep-07 18:05
InstallerGuy10-Sep-07 18:05 
GeneralCalling from MFC Pin
UAVonix21-May-07 4:43
UAVonix21-May-07 4:43 
GeneralThanks Pin
wangjfcn11-Mar-07 17:13
wangjfcn11-Mar-07 17:13 
GeneralUser Breakpoint error Pin
larstbone20-Jan-06 5:25
larstbone20-Jan-06 5:25 
GeneralLinking problem - tip not mentioned in article. Pin
Matt Brunell17-Jan-06 6:42
Matt Brunell17-Jan-06 6:42 
GeneralSimple, good... But does it support dotnet objects viz Collection classes etc. Pin
Krishnk25-Oct-05 7:51
Krishnk25-Oct-05 7:51 
GeneralRe: Simple, good... But does it support dotnet objects viz Collection classes etc. Pin
Emil S27-Sep-06 20:21
Emil S27-Sep-06 20:21 
GeneralThanks ! Pin
NiFF11-Oct-05 5:17
NiFF11-Oct-05 5:17 
GeneralSTAThread Pin
fxetc18-Aug-05 23:25
fxetc18-Aug-05 23:25 
GeneralRe: STAThread Pin
fxetc19-Aug-05 9:11
fxetc19-Aug-05 9:11 
QuestionHow can I convert a Gdiplus.Graphic* to System.Drawing.Graphics*? Pin
ananymous24-Jan-05 20:14
ananymous24-Jan-05 20:14 
GeneralUsing with Powerbuilder 6.5 Pin
vmalenkov2-Apr-04 11:17
vmalenkov2-Apr-04 11:17 
GeneralRe: Using with Powerbuilder 6.5 Pin
tangts17-May-04 16:03
tangts17-May-04 16:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.