1 //------------------------------------------------------------------------------
4 // Desc: DirectShow base classes.
6 // Copyright (c) 1992-2002 Microsoft Corporation. All rights reserved.
7 //------------------------------------------------------------------------------
12 //---------------------------------------------------------------------------
15 #define MAX_KEY_LEN 260
18 //---------------------------------------------------------------------------
19 // externally defined functions/variable
21 extern int g_cTemplates
;
22 extern CFactoryTemplate g_Templates
[];
24 //---------------------------------------------------------------------------
28 // Try to enumerate all keys under this one.
29 // if we find anything, delete it completely.
30 // Otherwise just delete it.
32 // note - this was pinched/duplicated from
33 // Filgraph\Mapper.cpp - so should it be in
36 //---------------------------------------------------------------------------
39 EliminateSubKey( HKEY hkey
, LPTSTR strSubKey
)
42 if (0 == lstrlen(strSubKey
) ) {
47 LONG lreturn
= RegOpenKeyEx( hkey
53 ASSERT( lreturn
== ERROR_SUCCESS
54 || lreturn
== ERROR_FILE_NOT_FOUND
55 || lreturn
== ERROR_INVALID_HANDLE
);
57 if( ERROR_SUCCESS
== lreturn
)
59 // Keep on enumerating the first (zero-th)
60 // key and deleting that
64 TCHAR Buffer
[MAX_KEY_LEN
];
65 DWORD dw
= MAX_KEY_LEN
;
68 lreturn
= RegEnumKeyEx( hk
77 ASSERT( lreturn
== ERROR_SUCCESS
78 || lreturn
== ERROR_NO_MORE_ITEMS
);
80 if( ERROR_SUCCESS
== lreturn
)
82 EliminateSubKey(hk
, Buffer
);
91 RegDeleteKey(hkey
, strSubKey
);
98 //---------------------------------------------------------------------------
100 // AMovieSetupRegisterServer()
102 // registers specfied file "szFileName" as server for
103 // CLSID "clsServer". A description is also required.
104 // The ThreadingModel and ServerType are optional, as
105 // they default to InprocServer32 (i.e. dll) and Both.
107 //---------------------------------------------------------------------------
110 AMovieSetupRegisterServer( CLSID clsServer
111 , LPCWSTR szDescription
113 , LPCWSTR szThreadingModel
= L
"Both"
114 , LPCWSTR szServerType
= L
"InprocServer32" )
118 TCHAR achTemp
[MAX_PATH
];
120 // convert CLSID uuid to string and write
121 // out subkey as string - CLSID\{}
123 OLECHAR szCLSID
[CHARS_IN_GUID
];
124 HRESULT hr
= StringFromGUID2( clsServer
127 ASSERT( SUCCEEDED(hr
) );
132 wsprintf( achTemp
, TEXT("CLSID\\%ls"), szCLSID
);
133 LONG lreturn
= RegCreateKey( HKEY_CLASSES_ROOT
136 if( ERROR_SUCCESS
!= lreturn
)
138 return AmHresultFromWin32(lreturn
);
141 // set description string
144 wsprintf( achTemp
, TEXT("%ls"), szDescription
);
145 lreturn
= RegSetValue( hkey
150 if( ERROR_SUCCESS
!= lreturn
)
153 return AmHresultFromWin32(lreturn
);
156 // create CLSID\\{"CLSID"}\\"ServerType" key,
157 // using key to CLSID\\{"CLSID"} passed back by
158 // last call to RegCreateKey().
162 wsprintf( achTemp
, TEXT("%ls"), szServerType
);
163 lreturn
= RegCreateKey( hkey
166 if( ERROR_SUCCESS
!= lreturn
)
169 return AmHresultFromWin32(lreturn
);
174 wsprintf( achTemp
, TEXT("%ls"), szFileName
);
175 lreturn
= RegSetValue( hsubkey
179 , sizeof(TCHAR
) * (lstrlen(achTemp
)+1) );
180 if( ERROR_SUCCESS
!= lreturn
)
183 RegCloseKey( hsubkey
);
184 return AmHresultFromWin32(lreturn
);
187 wsprintf( achTemp
, TEXT("%ls"), szThreadingModel
);
188 lreturn
= RegSetValueEx( hsubkey
189 , TEXT("ThreadingModel")
192 , (CONST BYTE
*)achTemp
193 , sizeof(TCHAR
) * (lstrlen(achTemp
)+1) );
198 RegCloseKey( hsubkey
);
202 return HRESULT_FROM_WIN32(lreturn
);
207 //---------------------------------------------------------------------------
209 // AMovieSetupUnregisterServer()
211 // default ActiveMovie dll setup function
212 // - to use must be called from an exported
213 // function named DllRegisterServer()
215 //---------------------------------------------------------------------------
218 AMovieSetupUnregisterServer( CLSID clsServer
)
220 // convert CLSID uuid to string and write
221 // out subkey CLSID\{}
223 OLECHAR szCLSID
[CHARS_IN_GUID
];
224 HRESULT hr
= StringFromGUID2( clsServer
227 ASSERT( SUCCEEDED(hr
) );
229 TCHAR achBuffer
[MAX_KEY_LEN
];
230 wsprintf( achBuffer
, TEXT("CLSID\\%ls"), szCLSID
);
235 hr
= EliminateSubKey( HKEY_CLASSES_ROOT
, achBuffer
);
236 ASSERT( SUCCEEDED(hr
) );
244 //---------------------------------------------------------------------------
246 // AMovieSetupRegisterFilter through IFilterMapper2
248 //---------------------------------------------------------------------------
251 AMovieSetupRegisterFilter2( const AMOVIESETUP_FILTER
* const psetupdata
252 , IFilterMapper2
* pIFM2
255 DbgLog((LOG_TRACE
, 3, TEXT("= AMovieSetupRegisterFilter")));
257 // check we've got data
259 if( NULL
== psetupdata
) return S_FALSE
;
263 // (as pins are subkeys of filter's CLSID key
264 // they do not need to be removed separately).
266 DbgLog((LOG_TRACE
, 3, TEXT("= = unregister filter")));
267 HRESULT hr
= pIFM2
->UnregisterFilter(
268 0, // default category
269 0, // default instance name
270 *psetupdata
->clsID
);
277 rf2
.dwMerit
= psetupdata
->dwMerit
;
278 rf2
.cPins
= psetupdata
->nPins
;
279 rf2
.rgPins
= psetupdata
->lpPin
;
283 DbgLog((LOG_TRACE
, 3, TEXT("= = register filter")));
284 hr
= pIFM2
->RegisterFilter(*psetupdata
->clsID
285 , psetupdata
->strName
292 // handle one acceptable "error" - that
293 // of filter not being registered!
294 // (couldn't find a suitable #define'd
295 // name for the error!)
297 if( 0x80070002 == hr
)
304 //---------------------------------------------------------------------------
306 // RegisterAllServers()
308 //---------------------------------------------------------------------------
311 RegisterAllServers( LPCWSTR szFileName
, BOOL bRegister
)
313 HRESULT hr
= NOERROR
;
315 for( int i
= 0; i
< g_cTemplates
; i
++ )
319 const CFactoryTemplate
*pT
= &g_Templates
[i
];
321 DbgLog((LOG_TRACE
, 2, TEXT("- - register %ls"),
322 (LPCWSTR
)pT
->m_Name
));
324 // register CLSID and InprocServer32
328 hr
= AMovieSetupRegisterServer( *(pT
->m_ClsID
)
329 , (LPCWSTR
)pT
->m_Name
334 hr
= AMovieSetupUnregisterServer( *(pT
->m_ClsID
) );
337 // check final error for this pass
338 // and break loop if we failed
348 //---------------------------------------------------------------------------
350 // AMovieDllRegisterServer2()
352 // default ActiveMovie dll setup function
353 // - to use must be called from an exported
354 // function named DllRegisterServer()
356 // this function is table driven using the
357 // static members of the CFactoryTemplate
358 // class defined in the dll.
360 // it registers the Dll as the InprocServer32
361 // and then calls the IAMovieSetup.Register
364 //---------------------------------------------------------------------------
367 AMovieDllRegisterServer2( BOOL bRegister
)
369 HRESULT hr
= NOERROR
;
371 DbgLog((LOG_TRACE
, 2, TEXT("AMovieDllRegisterServer2()")));
373 // get file name (where g_hInst is the
374 // instance handle of the filter dll)
376 WCHAR achFileName
[MAX_PATH
];
378 // WIN95 doesn't support GetModuleFileNameW
381 char achTemp
[MAX_PATH
];
383 DbgLog((LOG_TRACE
, 2, TEXT("- get module file name")));
385 // g_hInst handle is set in our dll entry point. Make sure
386 // DllEntryPoint in dllentry.cpp is called
387 ASSERT(g_hInst
!= 0);
389 if( 0 == GetModuleFileNameA( g_hInst
391 , sizeof(achTemp
) ) )
394 DWORD dwerr
= GetLastError();
395 return AmHresultFromWin32(dwerr
);
398 MultiByteToWideChar( CP_ACP
401 , lstrlenA(achTemp
) + 1
403 , NUMELMS(achFileName
) );
407 // first registering, register all OLE servers
411 DbgLog((LOG_TRACE
, 2, TEXT("- register OLE Servers")));
412 hr
= RegisterAllServers( achFileName
, TRUE
);
416 // next, register/unregister all filters
421 // init is ref counted so call just in case
422 // we're being called cold.
424 DbgLog((LOG_TRACE
, 2, TEXT("- CoInitialize")));
425 hr
= CoInitialize( (LPVOID
)NULL
);
426 ASSERT( SUCCEEDED(hr
) );
428 // get hold of IFilterMapper2
430 DbgLog((LOG_TRACE
, 2, TEXT("- obtain IFilterMapper2")));
431 IFilterMapper2
*pIFM2
= 0;
432 IFilterMapper
*pIFM
= 0;
433 hr
= CoCreateInstance( CLSID_FilterMapper2
435 , CLSCTX_INPROC_SERVER
440 DbgLog((LOG_TRACE
, 2, TEXT("- trying IFilterMapper instead")));
442 hr
= CoCreateInstance(
445 CLSCTX_INPROC_SERVER
,
451 // scan through array of CFactoryTemplates
452 // registering servers and filters.
454 DbgLog((LOG_TRACE
, 2, TEXT("- register Filters")));
455 for( int i
= 0; i
< g_cTemplates
; i
++ )
459 const CFactoryTemplate
*pT
= &g_Templates
[i
];
461 if( NULL
!= pT
->m_pAMovieSetup_Filter
)
463 DbgLog((LOG_TRACE
, 2, TEXT("- - register %ls"), (LPCWSTR
)pT
->m_Name
));
467 hr
= AMovieSetupRegisterFilter2( pT
->m_pAMovieSetup_Filter
, pIFM2
, bRegister
);
471 hr
= AMovieSetupRegisterFilter( pT
->m_pAMovieSetup_Filter
, pIFM
, bRegister
);
475 // check final error for this pass
476 // and break loop if we failed
493 CoFreeUnusedLibraries();
498 // if unregistering, unregister all OLE servers
500 if( SUCCEEDED(hr
) && !bRegister
)
502 DbgLog((LOG_TRACE
, 2, TEXT("- register OLE Servers")));
503 hr
= RegisterAllServers( achFileName
, FALSE
);
506 DbgLog((LOG_TRACE
, 2, TEXT("- return %0x"), hr
));
511 //---------------------------------------------------------------------------
513 // AMovieDllRegisterServer()
515 // default ActiveMovie dll setup function
516 // - to use must be called from an exported
517 // function named DllRegisterServer()
519 // this function is table driven using the
520 // static members of the CFactoryTemplate
521 // class defined in the dll.
523 // it registers the Dll as the InprocServer32
524 // and then calls the IAMovieSetup.Register
527 //---------------------------------------------------------------------------
531 AMovieDllRegisterServer( void )
533 HRESULT hr
= NOERROR
;
535 // get file name (where g_hInst is the
536 // instance handle of the filter dll)
538 WCHAR achFileName
[MAX_PATH
];
541 // WIN95 doesn't support GetModuleFileNameW
543 char achTemp
[MAX_PATH
];
545 if( 0 == GetModuleFileNameA( g_hInst
547 , sizeof(achTemp
) ) )
550 DWORD dwerr
= GetLastError();
551 return AmHresultFromWin32(dwerr
);
554 MultiByteToWideChar( CP_ACP
557 , lstrlenA(achTemp
) + 1
559 , NUMELMS(achFileName
) );
562 // scan through array of CFactoryTemplates
563 // registering servers and filters.
565 for( int i
= 0; i
< g_cTemplates
; i
++ )
569 const CFactoryTemplate
*pT
= &g_Templates
[i
];
571 // register CLSID and InprocServer32
573 hr
= AMovieSetupRegisterServer( *(pT
->m_ClsID
)
574 , (LPCWSTR
)pT
->m_Name
577 // instantiate all servers and get hold of
578 // IAMovieSetup, if implemented, and call
579 // IAMovieSetup.Register() method
581 if( SUCCEEDED(hr
) && (NULL
!= pT
->m_lpfnNew
) )
583 // instantiate object
586 hr
= CoCreateInstance( *(pT
->m_ClsID
)
588 , CLSCTX_INPROC_SERVER
590 , reinterpret_cast<void**>(&psetup
) );
593 hr
= psetup
->Unregister();
595 hr
= psetup
->Register();
600 if( (E_NOINTERFACE
== hr
)
601 || (VFW_E_NEED_OWNER
== hr
) )
606 // check final error for this pass
607 // and break loop if we failed
618 //---------------------------------------------------------------------------
620 // AMovieDllUnregisterServer()
622 // default ActiveMovie dll uninstall function
623 // - to use must be called from an exported
624 // function named DllRegisterServer()
626 // this function is table driven using the
627 // static members of the CFactoryTemplate
628 // class defined in the dll.
630 // it calls the IAMovieSetup.Unregister
631 // method and then unregisters the Dll
632 // as the InprocServer32
634 //---------------------------------------------------------------------------
637 AMovieDllUnregisterServer()
639 // initialize return code
641 HRESULT hr
= NOERROR
;
643 // scan through CFactory template and unregister
644 // all OLE servers and filters.
646 for( int i
= g_cTemplates
; i
--; )
650 const CFactoryTemplate
*pT
= &g_Templates
[i
];
652 // check method exists
654 if( NULL
!= pT
->m_lpfnNew
)
656 // instantiate object
659 hr
= CoCreateInstance( *(pT
->m_ClsID
)
661 , CLSCTX_INPROC_SERVER
663 , reinterpret_cast<void**>(&psetup
) );
666 hr
= psetup
->Unregister();
671 if( (E_NOINTERFACE
== hr
)
672 || (VFW_E_NEED_OWNER
== hr
) )
677 // unregister CLSID and InprocServer32
681 hr
= AMovieSetupUnregisterServer( *(pT
->m_ClsID
) );
684 // check final error for this pass
685 // and break loop if we failed