使用C ++ API注册COM DLL的所有接口

问题描述 投票:0回答:2

我知道,我们可以使用regsvr32.exe注册COM DLL的接口

是任何Windows C ++ API可用,哪个API将注册COM DLL的接口。

谢谢! Vijay Kumbhani

c++ visual-c++ com
2个回答
2
投票

所有regsvr32都会加载一个DLL并调用DLL暴露的DllRegisterServer函数。

如果你想编写自己的代码来执行此操作,那么你需要使用LoadLibraryGetProcAddress来获取指向该函数的指针,然后调用它。


0
投票

你知道CLSID和COM dll文件路径吗?如果是,您可以使用API​​ RegCreateKeyEx / SetKeyValue / RegCreateKeyEx / ...将它们写入注册表,这是一个示例:

[HKEY_CLASSES_ROOT\CLSID\{CLSID}\InprocServer32]
@="C:\\Windows\\System32\\oleaut32.dll"    
"ThreadingModel"="Both"

LONG lreturn = RegCreateKeyEx(
        HKEY_CLASSES_ROOT,
        _T("CLSID\${COMCLSID}"),  // subkey
        0,                        // reserved
        NULL,                     // class string (can be NULL)
        REG_OPTION_NON_VOLATILE,
        KEY_ALL_ACCESS,
        NULL,                     // security attributes
        &hKey,
        NULL                      // receives the "disposition" (is it a new or existing key)
        );
hr = __HRESULT_FROM_WIN32(lreturn);
// The default key value is a description of the object.
if (SUCCEEDED(hr))
{
    hr = SetKeyValue(hKey, NULL, _T("your description"));
}

// Create the "InprocServer32" subkey
if (SUCCEEDED(hr))
{
    const TCHAR *sServer = TEXT("InprocServer32");

    LONG lreturn = RegCreateKeyEx(hKey, sServer, 0, NULL,
        REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hSubkey, NULL);

    hr = __HRESULT_FROM_WIN32(lreturn);
}

if (SUCCEEDED(hr))
{
    hr = SetKeyValue(hSubkey, NULL, _T("${COM DLL file Path}"));
}

// Add a new value to the subkey, for "ThreadingModel" = <threading model>
if (SUCCEEDED(hr))
{
    hr = SetKeyValue(hSubkey, TEXT("ThreadingModel"), sThreadingModel);
}
© www.soinside.com 2019 - 2024. All rights reserved.