Calling C# method from C/C++ DLL

  • Thread starter Thread starter Ann Huxtable
  • Start date Start date
A

Ann Huxtable

I have a simple Log method, signature : void(int,string), that I want to
be able to call from my C/C++ (unmanaged) code. Is there any reference
available to show how this may be done?

Thanks
 
The only way to do this is to use COM interop.

See the article in MSDN entitled "Exposing .NET Framework Components to COM"

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Ann said:
I have a simple Log method, signature : void(int,string), that I want
to be able to call from my C/C++ (unmanaged) code. Is there any
reference available to show how this may be done?

As Bob says, you have to use COM interop to do this. Specifically, you
have to host the .NET runtime in the C++ application and then access the
..NET class and method through OLE Automation. If you have done any work
calling automation interfaces (IDispatch) in C++ then you will know how
tedious this is.

If it is "a simple Log method" then why not implement it in C++ in a DLL
and use this DLL in your C# code through Platform Invoke, and in your
C++ code through normal export (static) libraries?

Richard
 
Back
Top