Calling managed code from unmanaged code, VC++

Posted on 2011-10-05
Last Modified: 2012-05-12
We have a legacy VC++ application ( unmanaged code in VS2005 ) and a C# application (managed code in VS2010) .

Can any one tell what is the best possible way to call the C# application from the Legacy application?Something like creating a interface dll or call the C# exe directly from the code.?What is the best possible way to proceed keeping in mind the future too!

I know there are lots of tutorial in the web, but if any one can point a good reference, that would be great.

Question by:rbhargaw
    LVL 33

    Expert Comment

    by:Todd Gerbert
    One option might be to post a message to the C# application's window.

    Alternatively you can have the C# application call an exported function in your C++ app, and provide a function pointer/delegate. Then, once the C# application has provided the C++ application a function pointer, the C++ application can just call the function pointer.

    Since C# code is compiled at run-time, the location of a given function is not known in advance, therefore native C++ cannot "directly" execute code in a C# application (without the C# app initiating the process by providing a delegate). Of course, the exception to that is managed C++.Net - maybe a mixed-mode C++ module would work for you.
    LVL 33

    Expert Comment

    by:Todd Gerbert
    I've still got some example code handy from when I was tinkering with this myself...

    Note that the C# application must run first, because the location of FnPtrProc() isn't known until the C# program starts (i.e. is compiled).  The C# program calls a function in the C++ DLL, CallbackTest(), and passes in a pointer (or delegate, in C# terms) to FnPtrProc(). The C++ code can then make a call to the C# function.

    Apparently I was messing with this example a little bit, so I can't say for certain it'll run as-is - e.g. you might need to change which C# line is commented...

    C# Code:
    using System;
    using System.Runtime.InteropServices;
    class Program
    	private static extern void CallbackTest(string addr);
    	delegate void FnPtr(string message, int number);
    	static void Main(string[] args)
    		FnPtr proc = new FnPtr(FnPtrProc);
    	static void FnPtrProc(string message, int number)
    		Console.WriteLine("Message: {0}", message);
    		Console.WriteLine("Number: {0}", number);

    Open in new window

    C++ Code:
    // MyDll.cpp : Defines the exported functions for the DLL application.
    #include "stdafx.h"
    #include "MyDll.h"
    using namespace std;
    typedef void(*CallbackPtr)(const char*,int);
    extern "C" __declspec(dllexport)
    void WINAPI CallbackTest(const char* callbackAddr)
    	stringstream ss(callbackAddr);
    	INT_PTR pFn;
    	ss >> pFn;
    	CallbackPtr fn;
    	fn = (CallbackPtr)pFn;
    	for (int i = 0; i< 3; i++)
    		fn("Hello World", i);

    Open in new window


    Author Comment

    Thanks tgerbert!

    The legacy application has to provide data/parameters to C# application which is internally calling webservice and get back the results. So do you think if the C# application calling the exported function in C++ will work?or rather this has to be C++ calling C# application, please let me know!

    LVL 33

    Expert Comment

    by:Todd Gerbert
    Honestly, I'm a little curious why I used a char* and stringstream instead of a void*?
    LVL 33

    Accepted Solution

    >> So do you think if the C# application calling the exported function in C++ will work?

    Yes, that will work. BUT, it can only be done by passing a function pointer from C# to C++ and then letting C++ call that function pointer. C++ can not directly invoke functions in a C# .DLL or .EXE.
    LVL 33

    Expert Comment

    by:Todd Gerbert
    >> Honestly, I'm a little curious why I used a char* and stringstream instead of a void*?

    ...because I was working on a question where the asker specifically wanted to pass the function pointer value as a numeric string (just remembered). ;)

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now