Hi everybody,
I'm working on exposing a .Net (C#) COM Server. Manage Code is exposting an interface with two methods. These methods take an IN/OUT Struct as parameter. Requirement is that the unmanaged client (C++) will initialize the struct fields with some values and these struct values can be modified or reallocated by Managed COM Server.
Struct look something like this on the managed side:
[ComVisible(true)]
[Guid("<guid>")]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct MyStruct
{
string s1; //tlb maps to LPWSTR s1]
string[] arrayS2 //tlb maps to SAFEARRAY* arrayS2
byte[] arrayByte //tlb maps to SAFEARRAY* arrayByte
}
Interface is defined as:
[ComVisible(true)]
[Guid("<someguid>")]
public interface MyInterface
{
int MyMethod1([In, Out] ref MyStruct myStruct);
....
}
Unamanged C++ client will be allocating the MyStruct on heap, give some heap allocation using CoTaskMemAlloc and values to s1, arrayS2 and arrayByte. Manged Server will read these values and can potentially reallocated all the fields of the myStruct.
My understanding is that the Interop Marshall (in the above scenario) will fcopy the struct and all its fields to new Struct and free the fields (which are allocated by CoMemTaskAlloc) and pass it to ManagedCode -- ManagedCode then play with the struct and its field as needed and reallocated if required. Now while returning the Interop marshall will (using CoMemTaskAlloc) copy all the fields of the Struct back to myStruct and pass to the unmanaged client. Its unmanaged client's responsibility to free the fields of myStruct once it is done with the structure.
1> Is my understanding correct?
2> Is there a better way to achieve the above [again re-emphasising the requirement: umanaged client need to pass a struct to managed COM server with fields initialized AND managed server need to return the Struct with its fields reallocated/reinitialized.
Thanks for any help/suggestion,
user2008.