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
rbhargawFounderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Todd GerbertIT ConsultantCommented:
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.
0
Todd GerbertIT ConsultantCommented:
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

0
rbhargawFounderAuthor Commented:
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!

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Todd GerbertIT ConsultantCommented:
Honestly, I'm a little curious why I used a char* and stringstream instead of a void*?
0
Todd GerbertIT ConsultantCommented:
>> 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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Todd GerbertIT ConsultantCommented:
>> 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). ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.