Is it possible to call C# code from C++ ?

  • Thread starter Thread starter Ricky
  • Start date Start date
R

Ricky

Hi

I know that we can call C++ code from C# using COM interop, how about do
the reverse?

Ricky
 
Ricky,

If you want to call a COM component from .NET, you just have to add a
reference to it in your VS.NET project (go to "Add References").

If you were working from the command line, you would run the TLBIMP
utility to generate a Runtime Callable Wrapper, and then set a reference to
that.

You can also call C++ code through the P/Invoke layer, if the C++ code
exposes functions through a DLL.

Or, finally, you can wrap the C++ code in a managed wrapper (using
Managed Extensions for C++) and then call it like a normal assembly.

Hope this helps.
 
Yes, you can expose C# classes as a COM classes.
However, you have to respect some basic rules and preferably set some
attributes for this to work correctly, you also have to explicitly "register
the assembly for COM interop" [1].
For details, check the "COM interop tutorials" in the C# programmers
reference" on MSDN.

Willy.

[1] Windows XP SP2 offers a "registration free COM interop" facility.
 
Ricky,

Yes, it is. You can export your .NET types to COM using the TLBEXP
tool, which will generate a type library for your .NET types.

However, going from .NET to COM is a little tricker than the other way
around. Before doing this, you should read the section of the .NET
framework documentation titled "", located at (watch for line wrap):
 
Back
Top