471,120 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,120 software developers and data experts.

.Net COM Server Interop

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:
[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:
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,
Apr 3 '08 #1
0 1061

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Kevin | last post: by
15 posts views Thread by randyr | last post: by
5 posts views Thread by Dave A | last post: by
3 posts views Thread by Hari Menon | last post: by
3 posts views Thread by =?Utf-8?B?c3BkMzAwMQ==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.