组件注册表注册的相关函数

//
// SetRegKeyValue - Private function that updates the registry
// 设置注册表 键和键值
//
static BOOL SetRegKeyValue(
       LPTSTR pszKey,  //主键
       LPTSTR pszSubkey, //子键
       LPTSTR pszValue ) //键值
{
  BOOL bOk = FALSE;
  LONG ec;
  HKEY hKey;
  TCHAR szKey[128];

  lstrcpy(szKey, pszKey);

  if (NULL != pszSubkey) //子键不为空则用"\\"和主键连接在一起
  {
    lstrcat( szKey, "\\" );
    lstrcat( szKey, pszSubkey );
  }

  ec = RegCreateKeyEx(  //创建主键,如果主键下不为空,则相当于在某主键下创建子键
         HKEY_CLASSES_ROOT,
         szKey,
         0,
         NULL,
         REG_OPTION_NON_VOLATILE,
         KEY_ALL_ACCESS,
         NULL,
         &hKey,
         NULL);

  if (ERROR_SUCCESS == ec)
  {
    if (NULL != pszValue)//如键值不为空,则设置键值
    {
      ec = RegSetValueEx(
             hKey,
             NULL,
             0,
             REG_SZ,
             (BYTE *)pszValue,
             (lstrlen(pszValue)+1)*sizeof(TCHAR));
    }
    if (ERROR_SUCCESS == ec)
      bOk = TRUE;
    RegCloseKey(hKey);
  }

  return bOk;
}

//
// CreateComponentCategory - Uses the component categories manager
// to register a specific component category on the local machine.
// 创建组件类别
//
HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription)
{
   ICatRegister* pcr = 0;
   HRESULT hr;
   hr = CoCreateInstance( CLSID_StdComponentCategoriesMgr,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_ICatRegister,
                          (void**)&pcr );
   if (FAILED(hr))
      return hr;

   CATEGORYINFO catinfo;
   catinfo.catid = catid;  //设置组件类别的CLSID
   catinfo.lcid = 0x0409;
   int len = wcslen( catDescription );
   wcsncpy( catinfo.szDescription, catDescription, wcslen( catDescription )); //设置组件类别描述
   catinfo.szDescription[len] = '\0';

   hr = pcr->RegisterCategories( 1, &catinfo );  //注册组件类别
   pcr->Release();

   return hr;
}

//
// RegisterCLSIDInCategory - Uses the component categories
// manager to specify that the given component implements
// the specified component category
// 将组件注册到组件类别下
//
HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
{
   // Register your component categories information.
   ICatRegister* pcr = 0;
   HRESULT hr;
   hr = CoCreateInstance( CLSID_StdComponentCategoriesMgr,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_ICatRegister,
                          (void**)&pcr );
   if (SUCCEEDED(hr))
   {
      CATID rgcatid[1] ;
      rgcatid[0] = catid;
      hr = pcr->RegisterClassImplCategories( clsid, 1, rgcatid );

      pcr->Release();
   }

   return hr;
}

//
// DllRegisterServer - Entry point called by utilities such as
// REGSVR32.EXE to update the registry with the appropriate
// values for each component type in this DLL housing.
//
STDAPI DllRegisterServer(void)
{
    HRESULT  hr = NOERROR;
    CHAR    szModulePath[MAX_PATH];
    CHAR    szID[128];    //GUID值
    CHAR    szCLSID[128];   //"CLSID\\+GUID"

    WCHAR   wszID[128];
    WCHAR   wszCLSID[128];

    GetModuleFileName(
      g_hinstDLL,
      szModulePath,
      sizeof( szModulePath ) / sizeof( CHAR ));

    StringFromGUID2(CLSID_Math, wszID, sizeof( wszID ));
    wcscpy( wszCLSID, L"CLSID\\" );
    wcscat( wszCLSID, wszID ); //设置主键CLSID下的子键
    wcstombs( szID, wszID, sizeof( szID ));
    wcstombs( szCLSID, wszCLSID, sizeof( szID ));

 //
    // Create the ProgID keys.
 // 创建ProgID
 //
    SetRegKeyValue(    //创建主键"Chapter2.Math.1",并设置键值"Chapter2 Math Component"
      "Chapter2.Math.1",
      NULL,
      "Chapter2 Math Component" );
    SetRegKeyValue(    //在主键"Chapter2.Math.1"下创建"CLSID"子键,并设置键值
      "Chapter2.Math.1",
      "CLSID",
      szID);

 //
    // Create version independent ProgID keys.
 // 创建版本无关的ProgID
 //
    SetRegKeyValue(
      "Chapter2.Math",
      NULL,
      "Chapter2 Math Component");
    SetRegKeyValue(    //设置"Chapter2.Math"的现在版本为"Chapter2.Math.1"。
      "Chapter2.Math",
      "CurVer",
      "Chapter2.Math.1");
    SetRegKeyValue(    //设置"Chapter2.Math"的CLSID.
      "Chapter2.Math",
      "CLSID",
      szID);

 //
    // Create entries under CLSID.
 // 创建CLSID主键
 //
    SetRegKeyValue(     //设置CLSID键值
      szCLSID,
      NULL,
      "Chapter 2 Math Component");
    SetRegKeyValue(     //设置子键ProgID的键值
      szCLSID,
      "ProgID",
      "Chapter2.Math.1");
    SetRegKeyValue(     //设置CLSID的版本无关ProgID.
      szCLSID,
      "VersionIndependentProgID",
      "Chapter2.Math");
    SetRegKeyValue(     //设置组件的Dll文件的路径
      szCLSID,
      "InprocServer32",
      szModulePath);

    // Register our component category
    CreateComponentCategory( CATID_ATLDevGuide, L"ATL Developer's Guide Examples" );
    RegisterCLSIDInCategory( CLSID_Math, CATID_ATLDevGuide );

    return S_OK;
}

posted @ 2010-09-05 16:01  大树的种子  阅读(647)  评论(0编辑  收藏  举报