title: How to register a COM class in memory?
body: <p>I have a COM class (<em>e.g.</em>, <code>CLSID_WidgetFactory</code>) that I want to make available to anyone in my process who calls:</p>
  
  <pre><code>CoCreateInstance(CLSID_Widget, ...);
  </code></pre>
  
  <p>But i want to make it available without having to register it, or include it in a registration-free COM assembly manifest.</p>
  
  <h2>Core question</h2>
  
  <p>I have:</p>
  
  <ul>
  <li>a <code>clsid</code> (<code>CLSID_Widget</code>)</li>
  <li>an <code>IClassFactory</code> implementation</li>
  </ul>
  
  <p>How can i register the these with the COM infrastructure so that anyone in my process can create the COM object?</p>
  
  <p>Obviously I want to accomplish this:</p>
  
  <ul>
  <li>without registering the COM object in the <code>HKLM\Software\Classes</code> registry hive</li>
  <li>without registering the COM object in the <code>HKCU\Software\Classes</code> hive</li>
  <li>without create a <a href="https://stackoverflow.com/questions/10739845/how-to-deploy-sql-server-compact-edition-4-0">registration-free COM assembly manifest</a></li>
  </ul>
  
  <p>Conceptually i would have my classID, and my factory object:</p>
  
  <pre><code>class WidgetFactory : ComObject, IClassFactory
  {
     //IClassFactory methods
     HRESULT CreateInstance(IUnknown unkOuter, Guid iid, out obj)
     {
        Widget widget = new Widget();
  
        HRESULT hr = widget.QueryInterface(iid, out obj);
        return hr;
     }
  }
  </code></pre>
  
  <p>Then i want to register my class factory:</p>
  
  <pre><code>Guid CLSID_Widget = "{0677445E-EA9B-447A-AF2E-CCD86E49CED0}";
  IClassFactory factory = new WidgetFactory();
  
  RegisterClassFactory(CLSID_Widget, factory);
  </code></pre>
  
  <p>All that's left is to figure out how to register a COM Class in-memory:</p>
  
  <pre><code>void RegisterClassFactory(Guid clsid, IClassFactory classfactory)
  {
     //...
  }
  </code></pre>
  
  <h2>Search terms</h2>
  
  <ul>
  <li>How to register a COM object in memory</li>
  <li>How to register a COM class in my process</li>
  <li>How to register a CLSID in memory</li>
  <li>How to register IClassFactory in my application</li>
  </ul>
  
  <p><strong>Note</strong>: I know the answer. But i can't figure out the correct set of search terms that would trigger a hit. </p>
  
  <p><strong>Bonus Reading</strong></p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/36955791/can-i-create-and-use-a-com-class-without-registering-it-in-the-registry">Can I create and use a COM class without registering it in the registry?</a></li>
  <li>Old New Thing: <a href="https://blogs.msdn.microsoft.com/oldnewthing/20120406-00/?p=7913" rel="nofollow noreferrer">Registration-free COM the old-fashioned way: The car mp3 player</a></li>
  <li>Old New Thing: <a href="https://blogs.msdn.microsoft.com/oldnewthing/20100503-00/?p=14183" rel="nofollow noreferrer">How do I accept files to be opened via IDropTarget instead of on the command line?</a></li>
  </ul>