home *** CD-ROM | disk | FTP | other *** search
- //==========================================================================;
- //
- // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
- // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
- // PURPOSE.
- //
- // Copyright (c) 1992 - 1996 Microsoft Corporation. All Rights Reserved.
- //
- //--------------------------------------------------------------------------;
-
- #include <streams.h>
-
- //---------------------------------------------------------------------------
- // defines
-
- #define MAX_KEY_LEN 260
-
-
- //---------------------------------------------------------------------------
- // externally defined functions/variable
-
- extern int g_cTemplates;
- extern CFactoryTemplate g_Templates[];
-
- //---------------------------------------------------------------------------
- //
- // EliminateSubKey
- //
- // Try to enumerate all keys under this one.
- // if we find anything, delete it completely.
- // Otherwise just delete it.
- //
- // note - this was pinched/duplicated from
- // Filgraph\Mapper.cpp - so should it be in
- // a lib somewhere?
- //
- //---------------------------------------------------------------------------
-
- HRESULT EliminateSubKey( HKEY hkey, LPTSTR strSubKey )
- {
- HKEY hk;
- if (0 == lstrlen(strSubKey) ) {
- // defensive approach
- return E_FAIL;
- }
-
- LONG lreturn = RegOpenKeyEx( hkey
- , strSubKey
- , 0
- , MAXIMUM_ALLOWED
- , &hk );
-
- ASSERT( lreturn == ERROR_SUCCESS
- || lreturn == ERROR_FILE_NOT_FOUND
- || lreturn == ERROR_INVALID_HANDLE );
-
- if( ERROR_SUCCESS == lreturn )
- {
- // Keep on enumerating the first (zero-th)
- // key and deleting that
-
- for( ; ; )
- {
- TCHAR Buffer[MAX_KEY_LEN];
- DWORD dw = MAX_KEY_LEN;
- FILETIME ft;
-
- lreturn = RegEnumKeyEx( hk
- , 0
- , Buffer
- , &dw
- , NULL
- , NULL
- , NULL
- , &ft);
-
- ASSERT( lreturn == ERROR_SUCCESS
- || lreturn == ERROR_NO_MORE_ITEMS );
-
- if( ERROR_SUCCESS == lreturn )
- {
- EliminateSubKey(hk, Buffer);
- }
- else
- {
- break;
- }
- }
-
- RegCloseKey(hk);
- RegDeleteKey(hkey, strSubKey);
- }
-
- return NOERROR;
- }
-
-
- //---------------------------------------------------------------------------
- //
- // AMovieSetupRegisterServer()
- //
- // registers specfied file "szFileName" as server for
- // CLSID "clsServer". A description is also required.
- // The ThreadingModel and ServerType are optional, as
- // they default to InprocServer32 (i.e. dll) and Both.
- //
- //---------------------------------------------------------------------------
-
- HRESULT
- AMovieSetupRegisterServer( CLSID clsServer
- , LPCWSTR szDescription
- , LPCWSTR szFileName
- , LPCWSTR szThreadingModel = L"Both"
- , LPCWSTR szServerType = L"InprocServer32" )
- {
- // convert CLSID uuid to string and write
- // out subkey as string - CLSID\{}
- //
- OLECHAR szCLSID[CHARS_IN_GUID];
- HRESULT hr = StringFromGUID2( clsServer
- , szCLSID
- , CHARS_IN_GUID );
- ASSERT( SUCCEEDED(hr) );
-
- WCHAR achsubkey[MAX_PATH];
- wsprintfW( achsubkey, L"CLSID\\%ls", szCLSID );
-
- // create key
- //
- HKEY hkey;
- LONG lreturn = RegCreateKeyW( HKEY_CLASSES_ROOT
- , (LPCWSTR)achsubkey
- , &hkey );
- if( ERROR_SUCCESS != lreturn )
- {
- return HRESULT_FROM_WIN32(lreturn);
- }
-
- // set description string
- //
- lreturn = RegSetValueW( hkey
- , (LPCWSTR)NULL
- , REG_SZ
- , szDescription
- , sizeof(szDescription) );
- if( ERROR_SUCCESS != lreturn )
- {
- RegCloseKey( hkey );
- return HRESULT_FROM_WIN32(lreturn);
- }
-
- // create CLSID\\{"CLSID"}\\"ServerType" key,
- // using key to CLSID\\{"CLSID"} passed back by
- // last call to RegCreateKey().
- //
- HKEY hsubkey;
-
- lreturn = RegCreateKeyW( hkey
- , szServerType
- , &hsubkey );
- if( ERROR_SUCCESS != lreturn )
- {
- RegCloseKey( hkey );
- return HRESULT_FROM_WIN32(lreturn);
- }
-
- // set Server string
- //
- lreturn = RegSetValueW( hsubkey
- , (LPCWSTR)NULL
- , REG_SZ
- , (LPCWSTR)szFileName
- , sizeof(WCHAR) * (lstrlenW(szFileName)+1) );
- if( ERROR_SUCCESS != lreturn )
- {
- RegCloseKey( hkey );
- RegCloseKey( hsubkey );
- return HRESULT_FROM_WIN32(lreturn);
- }
-
- lreturn = RegSetValueExW( hsubkey
- , L"ThreadingModel"
- , 0L
- , REG_SZ
- , (CONST BYTE *)szThreadingModel
- , sizeof(WCHAR) * (lstrlenW(szThreadingModel)+1) );
-
- // close hkeys
- //
- RegCloseKey( hkey );
- RegCloseKey( hsubkey );
-
- // and return
- //
- return HRESULT_FROM_WIN32(lreturn);
-
- }
-
-
- //---------------------------------------------------------------------------
- //
- // AMovieSetupUnregisterServer()
- //
- // default ActiveMovie dll setup function
- // - to use must be called from an exported
- // function named DllRegisterServer()
- //
- //---------------------------------------------------------------------------
-
- HRESULT
- AMovieSetupUnregisterServer( CLSID clsServer )
- {
- // convert CLSID uuid to string and write
- // out subkey CLSID\{}
- //
- OLECHAR szCLSID[CHARS_IN_GUID];
- HRESULT hr = StringFromGUID2( clsServer
- , szCLSID
- , CHARS_IN_GUID );
- ASSERT( SUCCEEDED(hr) );
-
- TCHAR achBuffer[MAX_KEY_LEN];
- wsprintf( achBuffer, TEXT("CLSID\\%ls"), szCLSID );
-
- // delete subkey
- //
-
- hr = EliminateSubKey( HKEY_CLASSES_ROOT, achBuffer );
- ASSERT( SUCCEEDED(hr) );
-
- // return
- //
- return NOERROR;
- }
-
-
- //---------------------------------------------------------------------------
- //
- // AMovieDllRegisterServer()
- //
- // default ActiveMovie dll setup function
- // - to use must be called from an exported
- // function named DllRegisterServer()
- //
- // this function is table driven using the
- // static members of the CFactoryTemplate
- // class defined in the dll.
- //
- // it registers the Dll as the InprocServer32
- // and then calls the IAMovieSetup.Register
- // method.
- //
- //---------------------------------------------------------------------------
-
-
- HRESULT
- AMovieDllRegisterServer( void )
- {
- HRESULT hr = NOERROR;
-
- // get file name (where g_hInst is the
- // instance handle of the filter dll)
- //
- WCHAR achFileName[MAX_PATH];
-
- {
- // WIN95 doesn't support GetModuleFileNameW
- //
- char achTemp[MAX_PATH];
-
- if( 0 == GetModuleFileName( g_hInst
- , achTemp
- , sizeof(achTemp) ) )
- {
- // we've failed!
- DWORD dwerr = GetLastError();
- return HRESULT_FROM_WIN32(dwerr);
- }
-
- MultiByteToWideChar( CP_ACP
- , 0L
- , achTemp
- , lstrlen(achTemp) + 1
- , achFileName
- , sizeof(achFileName) );
- }
-
- // scan through array of CFactoryTemplates
- // registering servers and filters.
- //
- for( int i = 0; i < g_cTemplates; i++ )
- {
- // get i'th template
- //
- const CFactoryTemplate *pT = &g_Templates[i];
-
- // register CLSID and InprocServer32
- //
- hr = AMovieSetupRegisterServer( *(pT->m_ClsID)
- , (LPCWSTR)pT->m_Name
- , achFileName );
- if( FAILED(hr) ) break;
-
- // instantiate all servers and get hold of
- // IAMovieSetup, if implemented, and call
- // IAMovieSetup.Register() method
- //
- if( NULL != pT->m_lpfnNew )
- {
- // instantiate object
- //
- PAMOVIESETUP psetup;
- hr = CoCreateInstance( *(pT->m_ClsID), 0, CLSCTX_INPROC_SERVER, IID_IAMovieSetup
- , reinterpret_cast<void**>(&psetup) );
- if( SUCCEEDED(hr) )
- {
- hr = psetup->Register();
- psetup->Release();
- }
- else hr = NOERROR;
- }
- } // end-for
-
- return hr;
- }
-
-
- //---------------------------------------------------------------------------
- //
- // AMovieDllUnregisterServer()
- //
- // default ActiveMovie dll uninstall function
- // - to use must be called from an exported
- // function named DllRegisterServer()
- //
- // this function is table driven using the
- // static members of the CFactoryTemplate
- // class defined in the dll.
- //
- // it calls the IAMovieSetup.Unregister
- // method and then unregisters the Dll
- // as the InprocServer32
- //
- //---------------------------------------------------------------------------
-
- HRESULT AMovieDllUnregisterServer()
- {
- // initialize return code
- //
- HRESULT hr = NOERROR;
-
- // scan through CFactory template and unregister
- // all OLE servers and filters.
- //
- for( int i = g_cTemplates; i--; )
- {
- // get i'th template
- //
- const CFactoryTemplate *pT = &g_Templates[i];
-
- // check method exists
- //
- if( NULL != pT->m_lpfnNew )
- {
- // instantiate object
- //
- PAMOVIESETUP psetup;
- hr = CoCreateInstance( *(pT->m_ClsID), 0, CLSCTX_INPROC_SERVER, IID_IAMovieSetup
- , reinterpret_cast<void**>(&psetup) );
- if( SUCCEEDED(hr) )
- {
- hr = psetup->Unregister();
- psetup->Release();
- }
- else hr = NOERROR;
- }
-
- // unregister CLSID and InprocServer32
- //
- hr = AMovieSetupUnregisterServer( *(pT->m_ClsID) );
- }
-
- return hr;
- }
-