2

A while ago, I had a C# program that needed to call some functions in a C++ dll. I used pInvoke for that purpose. Some of the data I needed to pass in were structs. So, I would use the pInvoke Signature Toolkit to find out how to create a C# struct that was equivalent to the struct that the C++ function took by reference. Then, I was able to pass these structs to the C++ code.

Now, I have sort of the opposite situation. I am in C++ code, and I am being handed _com_ptr_t object. This is essentially a pointer to a C# object. The C# object implements a COM interface with properties like get_Foo, set_Foo, etc., that I can use to pull values (generally, they are just basic data types like float, bool, int, etc.) from the C# object and stuff them into my C++ counterpart.

This works well, but it's a little bit slow. When I profile my code, it spends a lot of time in "clr". So, I'm hoping to find a faster way, where I can perhaps add a function to the COM interface that will copy the entire C# object into the C++ counterpart struct at once -- sort of like how I did it with pInvoke above, except I'm pulling the data from C++ code through a COM interface, rather than passing data TO C++ code through a pInvoke interface.

Does anybody know of a way to do that?

2
  • each CLR -> COM requires copying managed buffers to unmanaged buffers. This will always be slow. Citing Alan Gordon (.NET and COM interoperability handbook): Avoid using chatty interfaces. Commented Jun 9, 2014 at 20:49
  • Hi Matt. It is only taking about a millisecond and a half, however I only have about 16.7ms to work with for my entire program, and so it ends up being significant. Thanks, Andro47. This interface is indeed very chatty. Picture a C# object with a few dozen properties (each of a basic data type like float or int). On the C++ side, each property gets converted into separate get_X and set_X methods. To copy the object into C++, I am having to call the get_ methods for each property. And then do this for hundreds of objects. Yeesh! Commented Jun 9, 2014 at 21:01

1 Answer 1

1

You can pass structs in COM using IDL, which may or may not accelerate the data transfer. There is a good chance it'll increase the speed of the data copy because you have fewer round trips, but if the reason you're spending a lot of time on the CLR side of things is because it's doing additional work over and above just copying data, optimizing the data transfer itself will not really speed up matters.

This is one of those situations where you have to have profiling results for both sides (native and CLR) because you're almost guaranteed to optimize the wrong thing otherwise.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much! I am not very familiar with IDL, but I will do some research. For the most part, these calls into the CLR are just C# properties of basic data types (e.g., a float property called PositionX that shows up on the C++ interface as separate get_PositionX and set_PositionX methods), and all they are doing is copying data. Some of these objects have dozens of these sorts of calls, just to copy one object, so I'm hoping this will provide a bit of a speed boost. Thanks again.
If they're really only copying data then yes, putting them into a structure should improve performance considerably.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.