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

visual Register all interfaces of COM DLL using C++ API

本文关键字:接口 DLL COM 使用 C++ API 注册 可视化      更新时间:2023-10-16

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

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

谢谢!维贾伊·昆巴尼

regsvr32所做的只是加载一个dll并调用DLL公开的DllRegisterServer函数。

如果你想编写自己的代码来做到这一点,那么你需要使用 LoadLibrary 和 GetProcAddress 来获取指向函数的指针,然后调用它。

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

[HKEY_CLASSES_ROOTCLSID{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);
}