Thursday, December 20, 2007

Calling a COM object on a remote machine with no type library

I'd always assumed to connect to a remote COM object required the client code to reference the type library for the COM object and for the type library to be registered on the client machine as well, but somebody showed me a way of doing this today that appears to avoid the need for either of these.

      Type type = Type.GetTypeFromCLSID(new Guid("{12345678-1234-1234-1234-123456789012}"), server, true);
      
      Object obj = Activator.CreateInstance(type);
      
      return (string)obj.GetType().InvokeMember("Method", 
        BindingFlags.InvokeMethod, null, objTest, new object[1]{param});

Admittedly it uses reflection, which will probably be slower than a direct call to the interface, but given it's a call over the wire, this probably won't be the slowest part of the call anyway. And it sure makes deployment easier.

No comments: