Link to home
Start Free TrialLog in
Avatar of rbhargaw
rbhargawFlag for United States of America

asked on

Calling managed code from unmanaged code, VC++

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.

Thanks
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

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.
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
{
	[DllImport("MyDll.dll")]
	private static extern void CallbackTest(string addr);
	
	delegate void FnPtr(string message, int number);

	static void Main(string[] args)
	{
		FnPtr proc = new FnPtr(FnPtrProc);

		CallbackTest(Marshal.GetFunctionPointerForDelegate(proc).ToString());
		//CallbackTest(Marshal.GetFunctionPointerForDelegate(proc).ToString());
		//CallbackTest(Marshal.GetFunctionPointerForDelegate(proc).ToString());
		Console.ReadKey();
	}

	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

Avatar of rbhargaw

ASKER

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!

Honestly, I'm a little curious why I used a char* and stringstream instead of a void*?
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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). ;)