Reputation: 10055
I have an off the shelf product which supports custom plugins which need to be written in Unmanaged C. It doesn't support Managed Dll's and my language of preference is c#.
The information which needs to be passed back to the off the shelf product is very basic and can be held in a string.
So I was thinking I could do the following:
Now communicating between a Managed C++ and C# Dll is easy.
But I cant figure out how to call a managed c++ function from an unmanaged c dll. Any help with some simple sample code would be great.
Thanks
EDIT: I created a Code Project article on how I did this with Alex's answer below. http://www.codeproject.com/Tips/695387/Calling-Csharp-NET-methods-from-unmanaged-code
Upvotes: 5
Views: 1935
Reputation: 7858
You almost got it right. Just put numbers 2 and 3 in the same C++/CLI DLL. You can both use .NET classes and export C functions from a C++/CLI project.
extern "C" __declspec( dllexport ) void __stdcall PureExportC(
const wchar_t* param
)
{
CSharpAssemblyNamespace::CSharpWorker worker;
worker.DoWork( gcnew String(param) );
}
Upvotes: 6
Reputation: 526
I don't think, you can do this. But you can write a c++ (unmanaged) plugin for the product. Then write a stand allone managed app with c#, start it from the plugin and communicate between them using named pipes or sockets.
Upvotes: 1