0

代码如下。如何避免TYPE_E_LIBNOTREGISTERED(hr=0x8002801D, Library not registered) error(DO NOT register the ActiveX to Windows)disp->Invoke()
注册 ActiveX 控件时,一切正常,但我不想注册到 WINDOWS。
谢谢!

#define WIN32_LEAN_AND_MEAN
#include <tchar.h>
#include <Windows.h>
#include <assert.h>
#include <stdio.h>
#include <ole2.h>

int APIENTRY _tWinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    OleInitialize(NULL);
    LPCTSTR lpFile = _T("D:\\Temp\\Flash32_11_4_402_287.ocx");
    HMODULE hDll = LoadLibrary(lpFile);
    if (hDll)
    {
        typedef HRESULT (WINAPI *DllGetClassObject_t)(REFCLSID, REFIID, LPVOID*);

        DllGetClassObject_t DllGetClassObject = NULL;
        DllGetClassObject = (DllGetClassObject_t)GetProcAddress(hDll, "DllGetClassObject");
        if (DllGetClassObject)
        {
            CLSID clsid = GUID_NULL;
            IClassFactory *pCF = NULL;
            if (SUCCEEDED(CLSIDFromString(L"{D27CDB6E-AE6D-11CF-96B8-444553540000}", &clsid))
                && SUCCEEDED(DllGetClassObject(clsid, IID_IClassFactory, (void**)&pCF)))
            {
                IOleObject *obj = NULL;
                IDispatch *disp = NULL;
                if (SUCCEEDED(pCF->CreateInstance(NULL, IID_IOleObject, (void**)&obj))
                    && SUCCEEDED(obj->QueryInterface(IID_IDispatch, (void**)&disp)))
                {
                    DISPPARAMS params = { NULL, NULL, 0, 0 };
                    HRESULT hr = disp->Invoke(0x70, IID_NULL,
                        LOCALE_USER_DEFAULT, DISPATCH_METHOD,
                        &params, NULL, NULL, NULL);

                    assert(SUCCEEDED(hr));
                    //ERROR: hr=0x8002801D,TYPE_E_LIBNOTREGISTERED, Library not registered
                }

                if (disp) disp->Release();
                if (obj) obj->Release();
                pCF->Release();
            }
        }
    }

    OleUninitialize();
    return 0;
}
4

1 回答 1

0

致 Hans Passant:非常感谢。英语不是我的母语,但是在你详细的回答下,我已经完美解决了这个问题,这是我在stackoverflow上的第一个问题,再次感谢。正如你所说,我的解决方案如下:

//ITypeInfo *m_ti = NULL;
//pCF->CreateInstance(...);

ITypeLib *tl = NULL;
if (SUCCEEDED(LoadTypeLib(wsOcxFile, &tl)))
{
    if (SUCCEEDED(tl->GetTypeInfoOfGuid(guid, &m_ti)))
    {
        hr = m_ti->Invoke(disp,
            dispid, wFlags, &m_dispParams,
            pvResult, &ei, &nArgErr);

    }
    tl->Release();
}

//m_ti->Release();
于 2012-11-28T06:59:00.103 回答