tags:

views:

71

answers:

1

Hi,

I want to create COM server which should be lanunched as a process using C#. I tried console application with COM interop support, client can able to create COM object of my server and call COM objects's method. But my server not launched as process (like out process server), also my server application's main() function not called.

I need to create COM server as separate process to avoid .net framework compatibility issue.

My code below,

[Guid("321003E2-51E5-4acd-BC0E-3C9F42235748"),    InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ITestInterface
{
    void PrintHi([In, MarshalAs(UnmanagedType.BStr)] string name);
}

[Guid("259689FE-58E4-4175-A82B-4E3827861767"), ProgId("MyTestApp.TestCom")]
public class TestCom : ITestInterface
{
    public TestCom()
    {
    }

    [ComRegisterFunction]
    public static void RegisterClass(string key)
    {
        StringBuilder strReg = new StringBuilder(key);
        strReg.Replace(@"HKEY_CLASSES_ROOT\", "");
        RegistryKey classReg = Registry.ClassesRoot.OpenSubKey(strReg.ToString(), true);
        classReg.Close();
    }

    [ComUnregisterFunction]
    public static void UnregisterClass(string key)
    {
        StringBuilder strReg = new StringBuilder(key);
        strReg.Replace(@"HKEY_CLASSES_ROOT\", "");
        RegistryKey classReg = Registry.ClassesRoot.OpenSubKey(strReg.ToString(), true);
        classReg.Close();
    }

    public void PrintHi(string name)
    {
        MessageBox.Show(name);
    }

}

And Server main function,

public class Program
    {
        static void Main(string[] args)
        {
            MessageBox.Show("Server main called");
        }
    }

My vc++ client,

   ITestInterface *cpi = NULL;
   HRESULT hr = CoInitialize(NULL);
   hr = CoCreateInstance(CLSID_TestCom, NULL, CLSCTX_ALL, IID_ITestInterface, (LPVOID *)&cpi);
   if (SUCCEEDED(hr))
   {
      cpi->PrintHi(_bstr_t("Test param"));
      cpi->Release();
      cpi = NULL;
   }
   CoUninitialize();

Thanks,

+1  A: 

You either need to register the COM component in COM+, or you need to launch your server via a separate task. On its own, COM is not going to create an object in an out of process server.

Mike Burton