0

我在regsvr32 的 WOW64 版本中注册了我的 32 位进程内组件(仅CLSID\{clsid},MyComponent.1MyComponent键及其子键) 。HKEY_CURRENT_USER\Software\Classes当我尝试使用 32 位进程中的 CoCreateInstance() 创建它时,它会因 REGDB_E_CLASSNOTREG 而失败。

但是HKEY_CLASSES_ROOT\CLSID\{clsid}\InprocServer32当我从我的 32 位进程中手动检索并手动加载 DLL 以调用 DllGetClassObject() 时,它成功了!

我不明白。当我的代码执行它们应该执行的操作时,CoCreateInstance() 和 CoGetClassObject() 在做什么使它们因 REGDB_E_CLASSNOTREG 而失败?

//This code fails with REGDB_E_CLASSNOTREG
STDMETHODIMP CreateTestCOM1(ITestCOM ** ppTestCOM, void **ppContext)
{
    assert(ppContext!=NULL);
    if(ppContext==NULL)
        return E_INVALIDARG;

    *ppContext = NULL;
    HRESULT hr = E_NOTIMPL;
//I tried both CoCreateInstance and CoGetClassObject, they both fail.
#if 0
    hr = CoCreateInstance(CLSID_TestCOM, NULL, CLSCTX_INPROC_SERVER, IID_ITestCOM, reinterpret_cast< void ** >(ppTestCOM));
#else
    IClassFactory *pFactory = NULL;
    hr = CoGetClassObject(CLSID_TestCOM, CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory, reinterpret_cast< void ** >(&pFactory));
    if(SUCCEEDED(hr))
    {
        hr = pFactory->CreateInstance(NULL, IID_ITestCOM, reinterpret_cast< void ** >(ppTestCOM));
        RELEASE_INTERFACE(pFactory);
    }
#endif
    return hr;
}
//This code works perfectly.
STDMETHODIMP CreateTestCOM3(ITestCOM ** ppTestCOM, void **ppContext)
{
    HRESULT hrRet = E_FAIL;
    LPTSTR libFileName = NULL;
    HKEY hMyClsidKey = NULL;
    DWORD err = RegOpenKeyEx(HKEY_CLASSES_ROOT, _T("CLSID\\{4883259C-527B-4327-B9BF-6C22079045B5}"), 0, KEY_READ, &hMyClsidKey);
    if(err != ERROR_SUCCESS)
    {
        _tprintf(_T("CreateTestCOM3: RegOpenKeyEx({CLSID}) failed with error %lu.\n"), err);
        hrRet = (err==ERROR_FILE_NOT_FOUND ? REGDB_E_CLASSNOTREG : E_UNEXPECTED);
    }
    else
    {
        HKEY hInprocKey = NULL;
        err = RegOpenKeyEx(hMyClsidKey, _T("InprocServer32"), 0, KEY_READ, &hInprocKey);
        if(err != ERROR_SUCCESS)
        {
            _tprintf(_T("CreateTestCOM3: RegOpenKeyEx(InprocServer32) failed with error %lu.\n"), err);
            hrRet = (err==ERROR_FILE_NOT_FOUND ? REGDB_E_KEYMISSING : E_UNEXPECTED);
        }
        else
        {
            TCHAR buf[MAX_PATH] = _T("");
            DWORD cbData = sizeof buf;
            DWORD type = 0;
            err = RegQueryValueEx(hInprocKey, NULL, NULL, &type, reinterpret_cast<LPBYTE>(buf), &cbData);
            if(err != ERROR_SUCCESS)
            {
                _tprintf(_T("CreateTestCOM3: RegQueryValueEx() failed with error %lu.\n"), err);
            }
            else if(type != REG_SZ)
            {
                _putts(_T("CreateTestCOM3: Inproc key's default value is not a REG_SZ."));
            }
            else if((cbData & 1) != 0)
            {
                _putts(_T("CreateTestCOM3: Inproc key's default value has odd size."));
            }
            else if(buf[MAX_PATH-1] != _T('\0'))
            {
                _putts(_T("CreateTestCOM3: Inproc key's default value is too long."));
            }
            else
            {
                libFileName = _tcsdup(buf);
                _tprintf(_T("CreateTestCOM3: Successfully retrieved DLL path: %s\n"), libFileName);
            }

            RegCloseKey(hInprocKey);
        }
        RegCloseKey(hMyClsidKey);
    }

    if(libFileName != NULL)
    {
        //LoadLibrary(), GetProcAddress(), DllGetClassObject(), IClassFactory::CreateInstance().
        hrRet = CreateTestCOMFromDLL(libFileName, ppTestCOM, ppContext);

        free(libFileName);
    }
    return hrRet;
}

我还使用 WOW64 版本的 regedit 检查了注册表,从那里看起来都HKEY_CLASSES_ROOT很好HKEY_CURRENT_USER\Software\Classes

编辑添加:我使用的是 Windows 7,而不是 XP/Vista。据此,是一个重要的区别。

4

0 回答 0