Thursday, May 5, 2011

When and why use CoLoadLibrary?

The description of CoLoadLibrary() says it does pretty much the same as LoadLibraryEx() - loads a DLL into the process. COM classes creation functions - CoCreateInstance() and CoGetClassObject() - both do load the necessary DLL into the process too.

Then why is CoLoadLibrary() needed in the first place and how should it be used?

From stackoverflow
  • Have a look at the code:

    mov     edi,edi
    push    ebp
    mov     ebp,esp
    push    8
    push    0
    push    dword ptr [ebp+8]
    call    dword ptr [ole32!_imp__LoadLibraryExW (71eb1214)]
    pop     ebp
    ret     8

    So it just calls:


    Presumably, the routine merely exists for backwards compatibility -- it probably has its roots in Win16.

    sharptooth : Are there any scenarios when I would need to call it?
    Johannes Passing : None that I can think of -- the functionality CoLoadLibrary once implemented seems to have been removed, so this routine really is just compatibilty baggage.
  • Perhaps if you were writing your own regsvr32.exe? But JP's disassembly doesn't really support my guess, because you could just use LoadLibraryEx instead. Maybe in the olden days, Microsoft planned on COM DLLs someday being loaded in a different way than regular DLLs (D-COM?), so this was a way of ensuring future compatibility.


Post a Comment