Search code examples
c#.netcomremoting

COM+ invoking method in remote instance Unknown name error


I have a COM dll which I was using in my .net project through interop referance. Now I got a requirement to move this COM component to another remote machine and create instance there.(kind of out of machine similar to out of process, probably this is remoting, i don't know :-))

What I have done was created a new COM+ applciation in server machine. Added this component inside this app. This is how it is listed in COM+ interface on server. enter image description here

Exported this app as proxy insatller and installed in my client machine. Used following code to access this

Type type;
type = Type.GetTypeFromProgID("DllName.COMName", "serverName", false);
var COMObject = Activator.CreateInstance(type);
var returnValue = COMObject.GetType().InvokeMember("Method_1",   BindingFlags.Public |  BindingFlags.InvokeMethod, null, COMObject, new object[1] { "1" });

But I am getting UNKNOWN NAME (0x80020006) error when invoking Method_1? Have any one face similar issue before, please help me.


Solution

  • What I would do is this:

    1) create an equivalent interface in C#, something like this:

    [Guid("<the interface IID goes here>")]
    public interface ISomeInterfaceName
    {
       int Method1(...);
       int Method2(...);
       int Method3(...);
       ...
    }
    

    2) and then this, instead of your initial late-binding code:

    Type type = Type.GetTypeFromProgID("DllName.COMName", "serverName", false);
    var COMObject = Activator.CreateInstance(type);
    var if = (ISomeInterfaceName)COMObject;
    if.Method1(...);
    

    NOTES: the IID must the IID from the interface, not to be confused with the CLSID. The methods should be laid out to correspond exactly to the COM ones (the methods order is important, how parameters and return type are defined is also important)

    You could also import the TLB from the COM component, if you have a lot of classes and intefaces like this.