• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

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
0
rbhargaw
Asked:
rbhargaw
  • 5
1 Solution
 
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
 
rbhargawAuthor 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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
 
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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now