user142350
user142350

Reputation: 222

How to pass a C# Reference to COM Object to a C++ DLL

I am writing a Visual Studio add-in to process C++ code, and think that COM interop is slowing me down to much. I therefore want to pass a C# reference to a COM object to a small C++ DLL, have the DLL perform the necessary calculations and return back a string.

I would be passing a CodeFunction2 object to the DLL and getting an XML string with information on the method back.

While you are welcome to question whether I really need this for the performance boost, if you call a dozen member variables accross a COM interop for thousands of methods it seems to eat up way too much time changing between managed and unmanaged code.

How do I format the arguments to the C++ DLL? I have no experience with calling unmanaged code from managed code in general, but the main question I need answered is how to format the call.

Upvotes: 2

Views: 1842

Answers (3)

Tamás Szelei
Tamás Szelei

Reputation: 23971

As others have pointed out, Managed C++ would be a good idea, since it makes interop a jiffy. If you decide to use it, instead of Managed C++, I'd suggest C++/CLI, which is the same in features, but it's newer and has a much cleaner syntax (it is intended to replace Managed C++). (in Managed C++ you have thing like __gc and stuff which is ugly).

Here is a nice tutorial about interop scenarios.

Upvotes: 1

ScottTx
ScottTx

Reputation: 1483

If you honestly think the C# / COM interop is slowing you down, why not write your VS addin in C++? That would avoid having to pass from a C# addin dll into a C++ dll to handle the COM interop - do it all in the same place.

Upvotes: 1

John Fisher
John Fisher

Reputation: 22717

Maybe you should write a Managed C++ dll instead (using the /clr switch), then you can directly pass managed objects into the C++ dll and do whatever COM magic you like without worrying about passing them between dlls.

Upvotes: 2

Related Questions