TYPE_E_LIBNOTREGISTERED(Library not registered) while call Invoke() of unregistered ActiveX control?

0

Code as below. How to avoid the TYPE_E_LIBNOTREGISTERED(hr=0x8002801D, Library not registered) error(DO NOT register the ActiveX to Windows) of disp->Invoke()?
When register the ActiveX control, all is OK, but I want not register to WINDOWS.
Thanks!

#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;
}
activex
invoke
asked on Stack Overflow Nov 27, 2012 by kevin.wang • edited Nov 27, 2012 by KeyNone

1 Answer

0

To Hans Passant: Thanks very very much. English is not my mother tongue, but under you detailed answers, I have the perfect solution to this problem, and this is my first question on stackoverflow, thanks again. As you said, my solution is bellow:

//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();
answered on Stack Overflow Nov 28, 2012 by kevin.wang

User contributions licensed under CC BY-SA 3.0