Share via

How to create instance of Win32 COM object?

Leo Ayala 21 Reputation points
2020-12-05T02:38:48.753+00:00

I have some legacy code that need to port over to C#. There is a COM server object written in C++ that is called by other Win32/MFC apps. I'm trying to see if I can write a C# app and create an instance of the COM object just like the other Win32/MFC apps. I referenced the Type Library (tlb) in my C# project but when I create an instance of the COM server object, I get the following error:

Creating an instance of the COM component with CLSID {723440AD-AB68-11D4-91F9-00A0CCE1FA84} from the IClassFactory failed due to the following error: 80010108 The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED)).

The IDL is as follows:

import "oaidl.idl";
import "ocidl.idl";
[
    object,
    uuid(723440AC-AB68-11D4-91F9-00A0CCE1FA84),
    dual,
    helpstring("ITestServer Interface v2.2"),
    oleautomation,
    pointer_default(unique)
]
interface ITestServer : IDispatch
{
    /* many methods */
}

library MyTestServerLib
{
    importlib("stdole32.tlb");
    importlib("stdole2.tlb");

    coclass TestServer
    {
        [default] interface ITestServer;
    };
}

The legacy apps work fine on my PC so I know that the COM object is registered. It is only my C# app that fails. Any idea as to why I would be getting the error mentioned above?

Any help will be greatly appreciated.

Regards,
LA

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


1 answer

Sort by: Most helpful
  1. Castorix31 91,871 Reputation points
    2020-12-05T10:31:38.62+00:00

    I don't reference the .tlb

    For example, with a COM interface IImgCtx (used to display pictures) =>

    [ComImport]
    [Guid("3050F3D7-98B5-11CF-BB82-00AA00BDCE0B")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IImgCtx
    {
             // declare all methods...
    }
    

    then :

    Guid CLSID_IImgCtx = new Guid("3050F3D6-98B5-11CF-BB82-00AA00BDCE0B");
    Type ImgCtxType = Type.GetTypeFromCLSID(CLSID_IImgCtx, true);
    IImgCtx pImgCtx = (IImgCtx)Activator.CreateInstance(ImgCtxType);
    

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.