Serialization IntPtr as out parameter

Hi

I have a problem with serializing IntPtr as an out parameter during remote call - If I have a remote object with method that returns IntPtr, then BinaryFormatter can't serialize the return message. I've spent some time to research the problem (thanks .NET reflector) and find out that the BinaryFormatter serializes objects that implement the IMethodReturnMessage in a special way, and if this message has IntPtr in the list of out arguments, then BinarySerializer throws the following exception SerializationException (Invalid type code in stream 'Invalid'.).
You can easily reproduce this case with the following code:

ReturnMessage message = new ReturnMessage(null, new Object[] { IntPtr.Zero }, 1, null, null);
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, message); // Here the exception is thrown

So the question is how to return the IntPtr from the remote method


Answer this question

Serialization IntPtr as out parameter

  • EddieMu

    It would help if you could provide the following information:

    1. The method signature of the remote method.

    2. The framework version (1.1 or 2.0)

    Thanks

    Sowmy



  • 2c

    • The Framework is 2.0.
    • The method signature is: void ExecuteSearch(String searchFilter, String[] attributeNames, Int32 numberAttributes, out IntPtr searchHandle);


  • KitGreen

    IntPtr is a value type and you can have value types as out parameter with BinaryFormatter. I tired the same thing with SoapFormatter and it works fine with SoapParameter. I will find out if the binary formatter shortcoming is by design or a bug

  • Serialization IntPtr as out parameter