Hello I wrote a managed COM server and COM client in C#. When I’m trying to get a reference to a COM server in a COM client by using this code.
Type comType = Type.GetTypeFromProgID("My.ComServer", true);
object comObj = Activator.CreateInstance(comType);
var comServer = (ComServer)comObj;
I get a reference to the new object instead of a reference to a registered COM server in ROT. To fix this problem, I use pinvoke CoGetClassObject it works but I have another problem.
private void InitComServer(object sender, RoutedEventArgs e)
{
object comServerInstance = null;
var factory = Ole32NativeMethods.CoGetClassObject(typeof(TestCoClass).GUID, RegistrationClassContext.LocalServer, IntPtr.Zero, typeof(IClassFactory).GUID) as IClassFactory;
if (factory != null)
{
var iid = typeof(ITestInterface).GUID;
factory.CreateInstance(null, iid, out comServerInstance);
}
this.comServer = (ITestInterface)comServerInstance;
}
I don’t know how to subscribe to events of COM server.
P.S. I attached a small demo application to illustrate this problem.
https://drive.google.com/file/d/0B_irffxq3qZqWEJMTGJHZWhZWTA/view?usp=sharing