Pass C # object to COM by value

I am trying to use the COM interface from C #, which provides the following interface created by tlbimp:

[Guid("7DDCEDF4-3B78-460E-BB34-C7496FD3CD56")] [TypeLibType(TypeLibTypeFlags.FDual | TypeLibTypeFlags.FNonExtensible | TypeLibTypeFlags.FDispatchable)] public interface IFred { [DispId(1)] IBarney Pall { get; set; } } [Guid("E390230E-EE9C-4819-BC19-08DAB808AEA9")] [TypeLibType(TypeLibTypeFlags.FDual | TypeLibTypeFlags.FNonExtensible | TypeLibTypeFlags.FDispatchable)] public interface IBarney { [DispId(1)] double Wilma { get; set; } } 

The generated assembly does not contain an implementation for the IBarney interface. I created a C # structure that implements the IBarney interface as follows:

 [Guid("2C61BA37-7047-43DB-84B1-83B4268CF77B")] [ComVisible(true)] public struct Barney : IBarney { public double Wilma { get; set; } } 

What โ€œworksโ€, now the question is, will the Barney instance be ordered by value or by reference? This is important due to network overhead. Perfectly does something like:

 fredInstance.Pall = new Barney { Wilma = 0.37 }; 

will result in a single trip to the same network. How can I check this or how can I tell you that the COM interaction of my Barney structure should always be sorted by value?

Update:

Given the comment by Hans Passan. What would be the โ€œrightโ€ way to develop it? What will be the IDL to allow a simple structure to be used as the value for the "property" of the COM interface? Looking at the IDL from which the interfaces I'm currently using are being created, adding a coclass with an IBarney interface by default will be enough, right?

+7
c # marshalling com com-interop
source share
1 answer

A couple of comments ... I don't know if this will be the answer or not ...

1) I would think you should make your interfaces [ComVisible (true)]

2) Why do you define Barney as a structure? It must be a class. Unlike C ++, where the only difference between a class and a structure is, by default, private vs public members, C # structures and classes are fundamentally different.

+1
source share

All Articles