none
Checking NULL parameters in COM Interop RRS feed

  • Question

  • Hi,

    I have developed a managed (C#) COM server and we have unmanaged (C/C++) client applications. In the server we have methods like:

    public int GetInfo(out int p1, out int p2) {}

    From the unmanaged client, this can be called as:

    int p1;
    int p2;
    server->GetInfo(&p1, &p2);

    This works fine and whatever value is assigned to these variables in server (C#) is available to client.

    The problem arises in case the client calls the method like this:

    server->GetInfo(NULL, NULL);

    How can I check in C# code, that the parameters passed are not initialized properly (I want to return back some error to client in this case). I tried comparing like:

    if (p1 == nulll) // this does not work

    In watch window the variable is displayed as "Cannot dereference expression. The pointer is not valid".


    Regards,
    Tuesday, January 20, 2009 5:45 AM

Answers

  • You'd have to patch the interop assembly by hand.  Decompile it first with Ildasm.exe, then alter the arguments from out int to IntPtr.  Compile again with Ilasm.exe.  On the C# side you can now check for IntPtr.Zero, use Marshal.WriteInt32() if it isn't.
    Hans Passant.
    • Marked as answer by Jayanta Dey Wednesday, January 21, 2009 3:42 AM
    Tuesday, January 20, 2009 7:07 AM
    Moderator

All replies

  • You'd have to patch the interop assembly by hand.  Decompile it first with Ildasm.exe, then alter the arguments from out int to IntPtr.  Compile again with Ilasm.exe.  On the C# side you can now check for IntPtr.Zero, use Marshal.WriteInt32() if it isn't.
    Hans Passant.
    • Marked as answer by Jayanta Dey Wednesday, January 21, 2009 3:42 AM
    Tuesday, January 20, 2009 7:07 AM
    Moderator
  • Thnaks, that solved the issue.
    Wednesday, January 21, 2009 3:42 AM