Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C++
Article

Emulating CoCreateInstance()

Rate me:
Please Sign up or sign in to vote.
4.77/5 (29 votes)
16 Apr 20071 min read 107.1K   59   16
Describes how to use COM objects without registration.

Introduction

A while ago, I wrote a small utility that converts DOT syntax into an image using the WinGraphViz.DLL COM component.

However, it is not very usual to have the user register this component before running the tool, thus I started looking around for a way to use COM components as if they were normal DLLs. The research yielded that there are many techniques to accomplish that:

  1. Registration-Free COM (for XP and above)
  2. Emulating the CoCreateInstance()

To learn more about Registration-Free COM, please check the references at the end of this article.

Emulating CoCreateInstance()

Normally, to create an instance, you would do something like:

hr = CoCreateInstance(CLSID_DOT, NULL, CLSCTX_ALL,
                      IID_IDOT, (LPVOID *)&pIDOT);

This will cause OLE to fetch the associated DLL from the registry and call its DllGetClassObject() method to get a class factory, then from the class factory an instance of your required IID will be created.

For that reason, we may emulate the CoCreateInstance() by the following code:

HRESULT __stdcall MyCoCreateInstance(
  LPCTSTR szDllName,
  IN REFCLSID rclsid,
  IUnknown* pUnkOuter,
  IN REFIID riid,
  OUT LPVOID FAR* ppv)
{
  HRESULT hr = REGDB_E_KEYMISSING;

  HMODULE hDll = ::LoadLibrary(szDllName);
  if (hDll == 0)
    return hr;

  typedef HRESULT (__stdcall *pDllGetClassObject)(IN REFCLSID rclsid, 
                   IN REFIID riid, OUT LPVOID FAR* ppv);

  pDllGetClassObject GetClassObject = 
     (pDllGetClassObject)::GetProcAddress(hDll, "DllGetClassObject");
  if (GetClassObject == 0)
  {
    ::FreeLibrary(hDll);
    return hr;
  }

  IClassFactory *pIFactory;

  hr = GetClassObject(rclsid, IID_IClassFactory, (LPVOID *)&pIFactory);

  if (!SUCCEEDED(hr))
    return hr;

  hr = pIFactory->CreateInstance(pUnkOuter, riid, ppv);
  pIFactory->Release();

  return hr;
}

Notice how this function takes a parameter holding the DLL's name.

Using the code

You need your application to run first if the COM is registered, if not you would resort to emulating the CoCreateInstance(). Your code could look like this:

hr = CoCreateInstance(CLSID_DOT, NULL, CLSCTX_ALL,
     IID_IDOT, (LPVOID *)&pIDOT);

if (hr == REGDB_E_CLASSNOTREG)
{
  hr = MyCoCreateInstance(_T("WinGraphViz.dll"), CLSID_DOT, 
       NULL, IID_IDOT, (LPVOID *)&pIDOT);
}

if (FAILED(hr))
{
  cout << "CoCreateInstance Failed: " << hr 
       << "nn";
  return -1;
}

Reference

The following links were helpful during the building of this simple snippet:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Elias (aka lallousx86, @0xeb) has always been interested in the making of things and their inner workings.

His computer interests include system programming, reverse engineering, writing libraries, tutorials and articles.

In his free time, and apart from researching, his favorite reading topics include: dreams, metaphysics, philosophy, psychology and any other human/mystical science.

Former employee of Microsoft and Hex-Rays (the creators of IDA Pro), was responsible about many debugger plugins, IDAPython project ownership and what not.

Elias currently works as an Anticheat engineer in Blizzard Entertainment.

Elias co-authored 2 books and authored one book:

- Practical Reverse Engineering
- The Antivirus Hacker's Handbook
- The Art of Batch Files Programming

Comments and Discussions

 
QuestionWhat about IDispatch methods? Pin
mertart12-Jun-10 2:50
mertart12-Jun-10 2:50 
QuestionSingle Instance of COM Servers from different clients Pin
GurmeetSingh8718-Feb-10 6:00
GurmeetSingh8718-Feb-10 6:00 
Hi,

I am creating an instance of COM Server from two different clients using call to CoCreateInstance function similar as shown below :

hr = CoCreateInstance(CLSID_DOT, NULL, CLSCTX_ALL,
IID_IDOT, (LPVOID *)&pIDOT);

To achieve single instance of the COMServer present on machine, i want to register the LPDispatch of COMServer to registry so that next time when the other client try to create the instance of COM Server , it first checks registry for the LPDispatch if it is present, it will use that Dispatch pointer to access COM Server.

For this i want to know how to get the Dipatch of a COM Server from its InitInstance() function. So that i can register that dipatch from InitInstance() that can be used by other client.

Thanks
Gurmeet Singh
GeneralCool, Great, Successfully used on Linux!!! Pin
Sharjith20-Jul-09 5:46
professionalSharjith20-Jul-09 5:46 
QuestionHow do I get clsid/iid unless the COM dll is not registered Pin
ehaerim23-Dec-07 18:50
ehaerim23-Dec-07 18:50 
AnswerRe: How do I get clsid/iid unless the COM dll is not registered Pin
Sharjith21-Jul-09 6:50
professionalSharjith21-Jul-09 6:50 
Questionun freed addtional dlls Pin
irol9-Nov-07 4:46
professionalirol9-Nov-07 4:46 
AnswerRe: un freed addtional dlls Pin
Elias Bachaalany11-Nov-07 22:19
Elias Bachaalany11-Nov-07 22:19 
GeneralRe: un freed addtional dlls Pin
irol12-Nov-07 23:57
professionalirol12-Nov-07 23:57 
QuestionOCX? Pin
sammytheblowfish10-Jul-07 20:28
sammytheblowfish10-Jul-07 20:28 
QuestionRe: OCX? Pin
sammytheblowfish14-Jul-07 12:18
sammytheblowfish14-Jul-07 12:18 
AnswerRe: OCX? Pin
charlieqin8-Nov-07 6:54
charlieqin8-Nov-07 6:54 
QuestionFreeLibrary? Pin
pestking17-Apr-07 15:03
pestking17-Apr-07 15:03 
AnswerRe: FreeLibrary? Pin
Elias Bachaalany17-Apr-07 18:49
Elias Bachaalany17-Apr-07 18:49 
GeneralRe: FreeLibrary? Pin
Phish Bulb13-Sep-07 5:27
Phish Bulb13-Sep-07 5:27 
GeneralRe: FreeLibrary? Pin
Elias Bachaalany20-Sep-07 21:01
Elias Bachaalany20-Sep-07 21:01 
GeneralGood stuff Pin
sprymak16-Apr-07 23:13
sprymak16-Apr-07 23:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.