Search code examples
c++winapicomwindows-servicesatl

trouble with CoCreateInstance method


I want to test a COM Service(EXE) but i can't to retrieve correct HRESULT from CoCreateInstance.

My COM sample is 'ATLCOMService' Microsoft sample. How to fix that ?

And client code is:

    void main()
    {
        HRESULT         hr;
        ISimpleObject *IFirstATL = NULL;

        hr = CoInitialize(0);
        if (SUCCEEDED(hr))
        {
            hr = CoCreateInstance(CLSID_SimpleObject, NULL,
                CLSCTX_LOCAL_SERVER,
                LIBID_ATLCOMServiceLib, (void**)&IFirstATL);
            //hr is: E_NOINTERFACE No such interface supported.

            hr = CoCreateInstance(CLSID_SimpleObject, NULL,
                CLSCTX_INPROC_SERVER,
                LIBID_ATLCOMServiceLib, (void**)&IFirstATL);
            //hr is: REGDB_E_CLASSNOTREG Class not registered

            if (SUCCEEDED(hr))
            {
                BSTR str;
                IFirstATL->HelloWorld(&str);
                cout << str << endl;

                IFirstATL->Release();
            }
            else
            {
                cout << "CoCreateInstance Failed." << endl;
            }
        }
        CoUninitialize();
    }

And IDs in header file is:

    #ifndef CLSID_DEFINED
    #define CLSID_DEFINED
    typedef IID CLSID;
    #endif // CLSID_DEFINED

    #define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) \
            const type name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}

    #endif !_MIDL_USE_GUIDDEF_

    MIDL_DEFINE_GUID(IID, 
    IID_ISimpleObject,0x1B877090,0x76CD,0x4EDE,0x81,0x15,0xEC,
    0x4C,0xCD,0x96,0x76,0xF3);


    MIDL_DEFINE_GUID(IID, LIBID_ATLCOMServiceLib,0xCC2CA6F0,0x2220,0x4D77,0xBA,
    0x46,0x4B,0xCB,0x62,0x15,0x6A,0x28);


    MIDL_DEFINE_GUID(IID, DIID__ISimpleObjectEvents,0x7DACF5E9,0x2885,0x4E4E,
    0x83,0xDD,0xCA,0x6C,0xC3,0xA8,0x8B,0x6D);


    MIDL_DEFINE_GUID(CLSID, 
    CLSID_SimpleObject,0x388F1C82,0xED00,0x4966,0x95,0x90,
    0x02,0xF6,0xB9,0xCC,0xA4,0x1B);

    #undef MIDL_DEFINE_GUID

    #ifdef __cplusplus
    }
    #endif

And in below saw registry instance and service status:

Registry - CLSID :

Registry - CLSID

Registry - typeid :

Registry - typeid

and Service Status :

Service status


Solution

  •        hr = CoCreateInstance(CLSID_SimpleObject, 
                NULL,
                CLSCTX_LOCAL_SERVER,
                LIBID_ATLCOMServiceLib, // <<--- IID_ISimpleObject here?
                (void**)&IFirstATL);
            //hr is: E_NOINTERFACE No such interface supported.
    

    Note that the fourth argument is supposed to be IID of the interface you are requesting, presumably IID_ISimpleObject. The API's returned error code suggests that you check if you request the correct interface: E_NOINTERFACE (or if it is correct then maybe the server erroneously does not implement it, or it is a marshaling issue).