Paul said:
Hi all,
Not sure if this should have gone into the C++ board,
apologies if it should have.
I was hoping for a really simple answer/examples to the
simple question of whether I can call .Net functions from
my legacy unmanaged C++ application. I just cannot seem
to get the answer from the MSDN!
If the .NET code is in a library assembly (DLL) then unmanaged code can call
the classes. There is a tool called tlbexp that will generate a type library
from the metadata in a library assembly, there is also a tool called regasm
that will register a library assembly as if it is a COM server (regasm can
also be used to create a type library). Once you have registered the DLL you
can use normal COM techniques to access the object and .NET will create a
special wrapper COM object to make the .NET object look like a COM object.
You can write your .NET object to make it easier to call from COM and for
details I would recommend Adam Nathan's book ".NET and COM" (the only book
you'll ever need on COM and .NET). To make life easier for you, make sure
that the .NET assembly is in the GAC. You can also call the object by
hosting the runtime in your native application, but its a pain to do because
you have to write the code to create the appdomain, and activate the object.
Be aware that COM does not understand overloading so you'll find that
overloaded methods will have names like Method, Method_2, Method_3 etc.
Richard