Calling Managed Code from Unmanaged code

R

Roy Chastain

I have a legacy application that currently loads an unmanaged (legacy) DLL via LoadLibrary, finds some entry points in it and then
calls them.

I would like to replace the unmanaged DLL with a managed DLL.
I have created a DLL with C++ that has the managed code. It also has unmanaged entry points that mirror the existing legacy DLL.

My problem is that I don't know how to transition to the managed code in this DLL.

I have found CorBindToRuntimeEx but that is as far as I have gotten.

My thinking is that it should go something like this

__gc class ManagedControl
{
public:
void Method1 (void);
void Method2 (void);
}

#pragma unmanaged

static gcroot<ManagedControl*> mc;

__declspec(dllexport) BOOL __stdcall umCreateControl (void)
{
HRESULT result;
ICorRuntimeHost *pHost = NULL;
result = CorBindToRuntimeEx(NULL,NULL,0,CLSID_CorRuntimeHost,IID_ICorRuntimeHost,(void **)&pHost);
/*
Do something to instantiate the managed class that represents the object being used
store that result in mc
*/
return TRUE;
}

Then in the other entry points I would 'simply' call the corresponding method on the managed class instance (mc)
hopefully by something that looks like

__declspec(dllexport) void__stdcall umMethod1 (void)
{
mc->Method1();
}

etc.

Thanks for getting me to the next step.
 
D

David Browne

Roy Chastain said:
I have a legacy application that currently loads an unmanaged (legacy) DLL
via LoadLibrary, finds some entry points in it and then
calls them.

I would like to replace the unmanaged DLL with a managed DLL.
I have created a DLL with C++ that has the managed code. It also has
unmanaged entry points that mirror the existing legacy

I don't really know how to host the runtime, so I would build expose a COM
interface on the .NET component, then from unmanaged code simply use COM,
and the COM-interop stuff will take care of everyting for me.

Again, I'm not sure if that's the best way, but it's easy and it will work.

David
 
R

Roy Chastain

Well, it might be easy for you, but it is not for me.
I have never really mastered COM.
I don't have any idea how to start to turn a managed DLL into a COM object.
 
R

Roy Chastain

Thanks for the pointer. I was already looking at that, but unfortunately my level of knowledge of COM makes most of that new
information and new technology. I would really rather spend the time working on the CLR hosting.

Hopefully someone out there can give me a little boost on that one.

Thanks.

If you haven't already looked here, then this would be a good place to
start:

Exposing .NET Framework Components to COM:
http://msdn.microsoft.com/library/d.../cpconexposingnetframeworkcomponentstocom.asp

hope that helps..
Imran.

Roy Chastain said:
Well, it might be easy for you, but it is not for me.
I have never really mastered COM.
I don't have any idea how to start to turn a managed DLL into a COM object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top