Reputation: 623
I implemented several COM objects in C# as local COM Server. My client is written in C++. This time I'm using COM objects (C# classes) for passing data from C++ to C# and it's working fine. I'm just thinking, if these COM objects are just "data holders", would be more efficient to use C# structures to hold data?
Upvotes: 0
Views: 82
Reputation: 942348
Generally no, particularly from C#. It depends on whether you write correct C++ code. Structs are an interop problem, packing and alignment rules are an implementation detail that can easily get you into trouble. Multiply that by the vagaries of exactly how the struct member is marshaled, the bool type has 3 common ways to get marshaled. Strings and arrays are never not a problem.
Writing correct C++ code requires using the IRecordInfo interface, it uses the struct definition in the type library to know how the marshal a struct member properly. You have to do this for each field in the struct.
This will not be faster than calling the property getter for a field when you use an in-process COM server. The only kind that's easy to create in C#.
You can ignore this requirement and hope that your C++ declaration matches the C# declaration. That might work. But typically only something you should do when the cost of marshaling the info is too high. That is only the case when the round-trip to the property getter is too high, usually for a call that transitions an apartment boundary. Like it does for an out-of-process server.
Upvotes: 1