代码改变世界

无注册表的COM调用

2012-08-21 11:19  Clingingboy  阅读(653)  评论(0编辑  收藏  举报

 

跳过注册表部分

http://blog.csdn.net/problc/article/details/7428440

HRESULT WINAPI CreateCOMObject( LPCWSTR pstrDll, REFCLSID rclsid, REFIID riid, LPVOID* ppObject, IUnknown* pUnkOuter )
{
    if (ppObject == NULL)
    {
        return E_POINTER;
    }
    *ppObject = NULL;

    typedef HRESULT (__stdcall *GETCLASS_PROC)(REFCLSID, REFIID, LPVOID*);

    HINSTANCE hInst = ::GetModuleHandle(pstrDll);
    if (!hInst)
    {
        hInst = CoLoadLibrary((LPOLESTR)pstrDll, TRUE);
    }

    GETCLASS_PROC proc = (GETCLASS_PROC)GetProcAddress(hInst, "DllGetClassObject");
    if (proc)
    {
        IClassFactory* pfact = NULL;
        HRESULT hr = proc(rclsid, IID_IClassFactory, (void**)&pfact);
        if (pfact)
        {
            hr = pfact->CreateInstance(pUnkOuter, riid, ppObject);
            pfact->Release();
        }
        return hr;
    }

    return CO_E_DLLNOTFOUND;
}

 

1.CoLoadLibrary

Loads a specific DLL into the caller's process. CoLoadLibrary is equivalent to LoadLibraryEx. CoLoadLibrary does not do anything about the lifetime of the library.

http://technet.microsoft.com/zh-cn/sqlserver/ms863920(en-us).aspx

2.CoFreeLibrary

Frees a library that, when loaded, was specified to be freed explicitly.

3.DllGetClassObject

Retrieves the class object from a DLL object handler or object application. DllGetClassObject is called from within the CoGetClassObject function when the class context is a DLL.

4.IDL参考

http://www.cnblogs.com/weiqubo/archive/2011/03/21/1989845.html