home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / dllsetup.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-25  |  10.1 KB  |  386 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (c) 1992 - 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11.  
  12. #include <streams.h>
  13.  
  14. //---------------------------------------------------------------------------
  15. // defines
  16.  
  17. #define MAX_KEY_LEN  260
  18.  
  19.  
  20. //---------------------------------------------------------------------------
  21. // externally defined functions/variable
  22.  
  23. extern int g_cTemplates;
  24. extern CFactoryTemplate g_Templates[];
  25.  
  26. //---------------------------------------------------------------------------
  27. //
  28. // EliminateSubKey
  29. //
  30. // Try to enumerate all keys under this one.
  31. // if we find anything, delete it completely.
  32. // Otherwise just delete it.
  33. //
  34. // note - this was pinched/duplicated from
  35. // Filgraph\Mapper.cpp - so should it be in
  36. // a lib somewhere?
  37. //
  38. //---------------------------------------------------------------------------
  39.  
  40. HRESULT EliminateSubKey( HKEY hkey, LPTSTR strSubKey )
  41. {
  42.   HKEY hk;
  43.   if (0 == lstrlen(strSubKey) ) {
  44.       // defensive approach
  45.       return E_FAIL;
  46.   }
  47.  
  48.   LONG lreturn = RegOpenKeyEx( hkey
  49.                              , strSubKey
  50.                              , 0
  51.                              , MAXIMUM_ALLOWED
  52.                              , &hk );
  53.  
  54.   ASSERT(    lreturn == ERROR_SUCCESS
  55.           || lreturn == ERROR_FILE_NOT_FOUND
  56.           || lreturn == ERROR_INVALID_HANDLE );
  57.  
  58.   if( ERROR_SUCCESS == lreturn )
  59.   {
  60.     // Keep on enumerating the first (zero-th)
  61.     // key and deleting that
  62.  
  63.     for( ; ; )
  64.     {
  65.       TCHAR Buffer[MAX_KEY_LEN];
  66.       DWORD dw = MAX_KEY_LEN;
  67.       FILETIME ft;
  68.  
  69.       lreturn = RegEnumKeyEx( hk
  70.                             , 0
  71.                             , Buffer
  72.                             , &dw
  73.                             , NULL
  74.                             , NULL
  75.                             , NULL
  76.                             , &ft);
  77.  
  78.       ASSERT(    lreturn == ERROR_SUCCESS
  79.               || lreturn == ERROR_NO_MORE_ITEMS );
  80.  
  81.       if( ERROR_SUCCESS == lreturn )
  82.       {
  83.         EliminateSubKey(hk, Buffer);
  84.       }
  85.       else
  86.       {
  87.         break;
  88.       }
  89.     }
  90.  
  91.     RegCloseKey(hk);
  92.     RegDeleteKey(hkey, strSubKey);
  93.   }
  94.  
  95.   return NOERROR;
  96. }
  97.  
  98.  
  99. //---------------------------------------------------------------------------
  100. //
  101. // AMovieSetupRegisterServer()
  102. //
  103. // registers specfied file "szFileName" as server for
  104. // CLSID "clsServer".  A description is also required.
  105. // The ThreadingModel and ServerType are optional, as
  106. // they default to InprocServer32 (i.e. dll) and Both.
  107. //
  108. //---------------------------------------------------------------------------
  109.  
  110. HRESULT
  111. AMovieSetupRegisterServer( CLSID   clsServer
  112.                          , LPCWSTR szDescription
  113.                          , LPCWSTR szFileName
  114.                          , LPCWSTR szThreadingModel = L"Both"
  115.                          , LPCWSTR szServerType     = L"InprocServer32" )
  116. {
  117.   // convert CLSID uuid to string and write
  118.   // out subkey as string - CLSID\{}
  119.   //
  120.   OLECHAR szCLSID[CHARS_IN_GUID];
  121.   HRESULT hr = StringFromGUID2( clsServer
  122.                                 , szCLSID
  123.                                 , CHARS_IN_GUID );
  124.   ASSERT( SUCCEEDED(hr) );
  125.  
  126.   WCHAR achsubkey[MAX_PATH];
  127.   wsprintfW( achsubkey, L"CLSID\\%ls", szCLSID );
  128.  
  129.   // create key
  130.   //
  131.   HKEY hkey;
  132.   LONG lreturn = RegCreateKeyW( HKEY_CLASSES_ROOT
  133.                               , (LPCWSTR)achsubkey
  134.                               , &hkey              );
  135.   if( ERROR_SUCCESS != lreturn )
  136.   {
  137.     return HRESULT_FROM_WIN32(lreturn);
  138.   }
  139.  
  140.   // set description string
  141.   //
  142.   lreturn = RegSetValueW( hkey
  143.                         , (LPCWSTR)NULL
  144.                         , REG_SZ
  145.                         , szDescription
  146.                         , sizeof(szDescription) );
  147.   if( ERROR_SUCCESS != lreturn )
  148.   {
  149.     RegCloseKey( hkey );
  150.     return HRESULT_FROM_WIN32(lreturn);
  151.   }
  152.  
  153.   // create CLSID\\{"CLSID"}\\"ServerType" key,
  154.   // using key to CLSID\\{"CLSID"} passed back by
  155.   // last call to RegCreateKey().
  156.   //
  157.   HKEY hsubkey;
  158.  
  159.   lreturn = RegCreateKeyW( hkey
  160.                          , szServerType
  161.                          , &hsubkey     );
  162.   if( ERROR_SUCCESS != lreturn )
  163.   {
  164.     RegCloseKey( hkey );
  165.     return HRESULT_FROM_WIN32(lreturn);
  166.   }
  167.  
  168.   // set Server string
  169.   //
  170.   lreturn = RegSetValueW( hsubkey
  171.                         , (LPCWSTR)NULL
  172.                         , REG_SZ
  173.                         , (LPCWSTR)szFileName
  174.                         , sizeof(WCHAR) * (lstrlenW(szFileName)+1) );
  175.   if( ERROR_SUCCESS != lreturn )
  176.   {
  177.     RegCloseKey( hkey );
  178.     RegCloseKey( hsubkey );
  179.     return HRESULT_FROM_WIN32(lreturn);
  180.   }
  181.  
  182.   lreturn = RegSetValueExW( hsubkey
  183.                           , L"ThreadingModel"
  184.                           , 0L
  185.                           , REG_SZ
  186.                           , (CONST BYTE *)szThreadingModel
  187.                           , sizeof(WCHAR) * (lstrlenW(szThreadingModel)+1) );
  188.  
  189.   // close hkeys
  190.   //
  191.   RegCloseKey( hkey );
  192.   RegCloseKey( hsubkey );
  193.  
  194.   // and return
  195.   //
  196.   return HRESULT_FROM_WIN32(lreturn);
  197.  
  198. }
  199.  
  200.  
  201. //---------------------------------------------------------------------------
  202. //
  203. // AMovieSetupUnregisterServer()
  204. //
  205. // default ActiveMovie dll setup function
  206. // - to use must be called from an exported
  207. //   function named DllRegisterServer()
  208. //
  209. //---------------------------------------------------------------------------
  210.  
  211. HRESULT
  212. AMovieSetupUnregisterServer( CLSID clsServer )
  213. {
  214.   // convert CLSID uuid to string and write
  215.   // out subkey CLSID\{}
  216.   //
  217.   OLECHAR szCLSID[CHARS_IN_GUID];
  218.   HRESULT hr = StringFromGUID2( clsServer
  219.                                 , szCLSID
  220.                                 , CHARS_IN_GUID );
  221.   ASSERT( SUCCEEDED(hr) );
  222.  
  223.   TCHAR achBuffer[MAX_KEY_LEN];
  224.   wsprintf( achBuffer, TEXT("CLSID\\%ls"), szCLSID );
  225.  
  226.   // delete subkey
  227.   //
  228.  
  229.   hr = EliminateSubKey( HKEY_CLASSES_ROOT, achBuffer );
  230.   ASSERT( SUCCEEDED(hr) );
  231.  
  232.   // return
  233.   //
  234.   return NOERROR;
  235. }
  236.  
  237.  
  238. //---------------------------------------------------------------------------
  239. //
  240. // AMovieDllRegisterServer()
  241. //
  242. // default ActiveMovie dll setup function
  243. // - to use must be called from an exported
  244. //   function named DllRegisterServer()
  245. //
  246. // this function is table driven using the
  247. // static members of the CFactoryTemplate
  248. // class defined in the dll.
  249. //
  250. // it registers the Dll as the InprocServer32
  251. // and then calls the IAMovieSetup.Register
  252. // method.
  253. //
  254. //---------------------------------------------------------------------------
  255.  
  256.  
  257. HRESULT
  258. AMovieDllRegisterServer( void )
  259. {
  260.   HRESULT hr = NOERROR;
  261.  
  262.   // get file name (where g_hInst is the
  263.   // instance handle of the filter dll)
  264.   //
  265.   WCHAR achFileName[MAX_PATH];
  266.  
  267.   {
  268.     // WIN95 doesn't support GetModuleFileNameW
  269.     //
  270.     char  achTemp[MAX_PATH];
  271.  
  272.     if( 0 == GetModuleFileName( g_hInst
  273.                               , achTemp
  274.                               , sizeof(achTemp) ) )
  275.     {
  276.       // we've failed!
  277.       DWORD dwerr = GetLastError();
  278.       return HRESULT_FROM_WIN32(dwerr);
  279.     }
  280.  
  281.     MultiByteToWideChar( CP_ACP
  282.                        , 0L
  283.                        , achTemp
  284.                        , lstrlen(achTemp) + 1
  285.                        , achFileName
  286.                        , sizeof(achFileName) );
  287.   }
  288.  
  289.   // scan through array of CFactoryTemplates
  290.   // registering servers and filters.
  291.   //
  292.   for( int i = 0; i < g_cTemplates; i++ )
  293.   {
  294.     // get i'th template
  295.     //
  296.     const CFactoryTemplate *pT = &g_Templates[i];
  297.  
  298.     // register CLSID and InprocServer32
  299.     //
  300.     hr = AMovieSetupRegisterServer( *(pT->m_ClsID)
  301.                                   , (LPCWSTR)pT->m_Name
  302.                                   , achFileName );
  303.     if( FAILED(hr) ) break;
  304.  
  305.     // instantiate all servers and get hold of
  306.     // IAMovieSetup, if implemented, and call
  307.     // IAMovieSetup.Register() method
  308.     //
  309.     if( NULL != pT->m_lpfnNew )
  310.     {
  311.       // instantiate object
  312.       //
  313.       PAMOVIESETUP psetup;
  314.       hr = CoCreateInstance( *(pT->m_ClsID), 0, CLSCTX_INPROC_SERVER, IID_IAMovieSetup
  315.                            , reinterpret_cast<void**>(&psetup) );
  316.       if( SUCCEEDED(hr) )
  317.       {
  318.         hr = psetup->Register();
  319.         psetup->Release();
  320.       }
  321.       else hr = NOERROR;
  322.     }
  323.   } // end-for
  324.  
  325.   return hr;
  326. }
  327.  
  328.  
  329. //---------------------------------------------------------------------------
  330. //
  331. // AMovieDllUnregisterServer()
  332. //
  333. // default ActiveMovie dll uninstall function
  334. // - to use must be called from an exported
  335. //   function named DllRegisterServer()
  336. //
  337. // this function is table driven using the
  338. // static members of the CFactoryTemplate
  339. // class defined in the dll.
  340. //
  341. // it calls the IAMovieSetup.Unregister
  342. // method and then unregisters the Dll
  343. // as the InprocServer32
  344. //
  345. //---------------------------------------------------------------------------
  346.  
  347. HRESULT AMovieDllUnregisterServer()
  348. {
  349.   // initialize return code
  350.   //
  351.   HRESULT hr = NOERROR;
  352.  
  353.   // scan through CFactory template and unregister
  354.   // all OLE servers and filters.
  355.   //
  356.   for( int i = g_cTemplates; i--; )
  357.   {
  358.     // get i'th template
  359.     //
  360.     const CFactoryTemplate *pT = &g_Templates[i];
  361.  
  362.     // check method exists
  363.     //
  364.     if( NULL != pT->m_lpfnNew )
  365.     {
  366.       // instantiate object
  367.       //
  368.       PAMOVIESETUP psetup;
  369.       hr = CoCreateInstance( *(pT->m_ClsID), 0, CLSCTX_INPROC_SERVER, IID_IAMovieSetup
  370.                            , reinterpret_cast<void**>(&psetup) );
  371.       if( SUCCEEDED(hr) )
  372.       {
  373.         hr = psetup->Unregister();
  374.         psetup->Release();
  375.       }
  376.       else hr = NOERROR;
  377.     }
  378.  
  379.     // unregister CLSID and InprocServer32
  380.     //
  381.     hr = AMovieSetupUnregisterServer( *(pT->m_ClsID) );
  382.   }
  383.  
  384.   return hr;
  385. }
  386.