1

I have client-server application with WCF services, and i need to send ComObject, presented as COM interface, in some state from client to server. ComObject isn't serializable, therefore i need create a new instance on server side and restore the correct state.

How can i get this state of ComObject on client-side and create the interface implementation instance on server-side?

definition of ComObject:

public class SyncSessionContext
{
  ...
  private CoreInterop.ISyncSessionState rawState;
  ...
}

definition of COM interface

internal static class CoreInterop
{
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]           
    [Guid("b8a940fe-9f01-483b-9434-c37d361225d9")]
    [ComImport]
    public interface ISyncSessionState
    {
        [MethodImpl(MethodImplOptions.InternalCall | MethodImplOptions.PreserveSig, MethodCodeType = MethodCodeType.Runtime)]
        int GetInfoForChangeApplication([MarshalAs(UnmanagedType.LPArray), In, Out] byte[] ppbChangeApplierInfo, [In, Out] ref uint pcbChangeApplierInfo);

        ...other methods
    }
}

My client-side code:

public override void BeginSession(SyncProviderPosition position, SyncSessionContext syncSessionContext)
{
    var field = typeof(SyncSessionContext).GetField("rawState", BindingFlags.Instance | BindingFlags.NonPublic);

    // Nonserializable correct instance
    var rawState = field.GetValue(syncSessionContext);

    //extract state...
    var state = ?????

    //calling wcf service       
    proxy.BeginSession(position, state);
}

My server-side code:

public void BeginSession(SyncProviderPosition position, object state)
{
    //initializing and restoring state        
    var rawState = ?????

    syncSessionContext = new SyncSessionContext(IdFormats(), null);
    var field = typeof(SyncSessionContext).GetField("rawState", BindingFlags.Instance | BindingFlags.NonPublic);
    field.SetValue(syncSessionContext, rawState);

    KnowledgeSyncProvider.BeginSession(position, syncSessionContext);
}

1 Answers1

1

In general, you can't, unless:

  • the COM coclass of the object exposes a means of serializing and deserializing its state, such as the COM interfaces IPersistMemory or IPersistPropertyBag; or
  • you have intimate knowledge of the internal implementation of every COM coclass with which your code will deal, allowing you to write code to serialize the essential state of the object "by hand".

This is because holding a COM interface pointer tells you precisely nothing about the internal state of the COM object which exposes it. The state may not even be in memory. See also.

Community
  • 1
  • 1
Chris Dickson
  • 11,964
  • 1
  • 39
  • 60