Can I create and use a COM class without registering it in the registry?

I need to create a custom COM object that looks something like this:

static const GUID IID_ClientCommunicator = { 0x5219b44a, 0x874, 0x449e,{ 0x86, 0x11, 0xb7, 0x8, 0xd, 0xbf, 0xa6, 0xab } };
static const GUID CLSID_ClientCommunicator = { 0x5219b44b, 0x874, 0x449e,{ 0x86, 0x11, 0xb7, 0x8, 0xd, 0xbf, 0xa6, 0xab } };

class ATL_NO_VTABLE CClientCommunicator:
    public CComObjectRootEx<CComSingleThreadModel>,
    public CComCoClass<CClientCommunicator, &CLSID_ClientCommunicator>,
    public IDispatchImpl<CClientCommunicator, &IID_ClientCommunicator, 0, /*wMajor =*/ 1, /*wMinor =*/ 0>
{
public:
    //Custom functions

};

      

This object will be passed as a property to the window javascript object in Internet Explorer so that I can call the functions I define in this class from JavaScript.

My question is, if it's all done in a single executable, do I need to register the COM object in the registry? If so, how do I do this if my COM object is in the executable?

0


source to share


2 answers


As @RaymondChen pointed out in the comments, there is a function CoRegisterClassObject

here that can be used to register COM classes without having to add them to the registry.



This, of course, allows you to use that COM class within the scope of the executable that registered it.

+1


source


It is the object's task that implements IClassFactory

to create the class.

You already have one CLSID_ClientCommunicator

. Now you just need to provide an object IClassFactory

that can build it:

private class ClientCommunicatorFactory : ComObject, IClassFactory
{
   //IClassFactory methods
   HRESULT CreateInstance(IUnknown unkOuter, Guid iid, out obj)
   {
      ClientCommunicator cc = new CClientCommunicator();

      HRESULT hr = cc.QueryInterface(iid, out obj);
      return hr;
   }
}

      

Now you have:

  • your CLSID_ClientCommunicator

  • yours IClassFactory

    who can build it

You register them with COM with CoRegisterClassObject

:



IClassFactory factory = new ClientCommunicatorFactory();
DWORD dwCookie; //cookie to keep track of our registration

CoRegisterClassObject(
   CLSID_ClientCommunicator,  // the CLSID to register
   factory,                   // the factory that can construct the object
   CLSCTX_INPROC_SERVER,      // can only be used inside our process
   REGCLS_MULTIPLEUSE,        // it can be created multiple times
   out dwCookie               // cookie we can later use to delete the registration
);

      

So, if someone in your process is trying to create your class:

IUnknown cc = CreateComObject(CLSID_ClientCommunicator);

      

it just works; even if the class is not registered in the registry.

Bonus

IUnknown CreateComObject(Guid clsid)
{
   IUnknown unk;
   HRESULT hr = CoCreateInstance(clsid, null, CLSCTX_INPROC_SERVER, IID_IUnknown, out unk);
   if (Failed(hr))
      throw new EComException(hr);

   return unk;
}

      

0


source







All Articles