dllsetup.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. //------------------------------------------------------------------------------
  2. // File: DllSetup.cpp
  3. //
  4. // Desc: DirectShow base classes.
  5. //
  6. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  7. //------------------------------------------------------------------------------
  8. #include <streams.h>
  9. #include <strsafe.h>
  10. #include "combase.h"
  11. //---------------------------------------------------------------------------
  12. // defines
  13. #define MAX_KEY_LEN 260
  14. //---------------------------------------------------------------------------
  15. // externally defined functions/variable
  16. //extern int g_cTemplates;
  17. //extern CFactoryTemplate g_Templates[];
  18. //---------------------------------------------------------------------------
  19. //
  20. // EliminateSubKey
  21. //
  22. // Try to enumerate all keys under this one.
  23. // if we find anything, delete it completely.
  24. // Otherwise just delete it.
  25. //
  26. // note - this was pinched/duplicated from
  27. // Filgraph\Mapper.cpp - so should it be in
  28. // a lib somewhere?
  29. //
  30. //---------------------------------------------------------------------------
  31. STDAPI
  32. EliminateSubKey( HKEY hkey, LPCTSTR strSubKey )
  33. {
  34. HKEY hk;
  35. if (0 == lstrlen(strSubKey) ) {
  36. // defensive approach
  37. return E_FAIL;
  38. }
  39. LONG lreturn = RegOpenKeyEx( hkey
  40. , strSubKey
  41. , 0
  42. , MAXIMUM_ALLOWED
  43. , &hk );
  44. ASSERT( lreturn == ERROR_SUCCESS
  45. || lreturn == ERROR_FILE_NOT_FOUND
  46. || lreturn == ERROR_INVALID_HANDLE );
  47. if( ERROR_SUCCESS == lreturn )
  48. {
  49. // Keep on enumerating the first (zero-th)
  50. // key and deleting that
  51. for( ; ; )
  52. {
  53. TCHAR Buffer[MAX_KEY_LEN];
  54. DWORD dw = MAX_KEY_LEN;
  55. FILETIME ft;
  56. lreturn = RegEnumKeyEx( hk
  57. , 0
  58. , Buffer
  59. , &dw
  60. , NULL
  61. , NULL
  62. , NULL
  63. , &ft);
  64. ASSERT( lreturn == ERROR_SUCCESS
  65. || lreturn == ERROR_NO_MORE_ITEMS );
  66. if( ERROR_SUCCESS == lreturn )
  67. {
  68. EliminateSubKey(hk, Buffer);
  69. }
  70. else
  71. {
  72. break;
  73. }
  74. }
  75. RegCloseKey(hk);
  76. RegDeleteKey(hkey, strSubKey);
  77. }
  78. return NOERROR;
  79. }
  80. //---------------------------------------------------------------------------
  81. //
  82. // AMovieSetupRegisterServer()
  83. //
  84. // registers specfied file "szFileName" as server for
  85. // CLSID "clsServer". A description is also required.
  86. // The ThreadingModel and ServerType are optional, as
  87. // they default to InprocServer32 (i.e. dll) and Both.
  88. //
  89. //---------------------------------------------------------------------------
  90. STDAPI
  91. AMovieSetupRegisterServer( CLSID clsServer
  92. , LPCWSTR szDescription
  93. , LPCWSTR szFileName
  94. , LPCWSTR szThreadingModel = L"Both"
  95. , LPCWSTR szServerType = L"InprocServer32" )
  96. {
  97. // temp buffer
  98. //
  99. TCHAR achTemp[MAX_PATH];
  100. // convert CLSID uuid to string and write
  101. // out subkey as string - CLSID\{}
  102. //
  103. OLECHAR szCLSID[CHARS_IN_GUID];
  104. HRESULT hr = StringFromGUID2( clsServer
  105. , szCLSID
  106. , CHARS_IN_GUID );
  107. ASSERT( SUCCEEDED(hr) );
  108. // create key
  109. //
  110. HKEY hkey;
  111. (void)StringCchPrintf( achTemp, NUMELMS(achTemp), TEXT("CLSID\\%ls"), szCLSID );
  112. LONG lreturn = RegCreateKey( HKEY_CLASSES_ROOT
  113. , (LPCTSTR)achTemp
  114. , &hkey );
  115. if( ERROR_SUCCESS != lreturn )
  116. {
  117. return AmHresultFromWin32(lreturn);
  118. }
  119. // set description string
  120. //
  121. (void)StringCchPrintf( achTemp, NUMELMS(achTemp), TEXT("%ls"), szDescription );
  122. lreturn = RegSetValue( hkey
  123. , (LPCTSTR)NULL
  124. , REG_SZ
  125. , achTemp
  126. , sizeof(achTemp) );
  127. if( ERROR_SUCCESS != lreturn )
  128. {
  129. RegCloseKey( hkey );
  130. return AmHresultFromWin32(lreturn);
  131. }
  132. // create CLSID\\{"CLSID"}\\"ServerType" key,
  133. // using key to CLSID\\{"CLSID"} passed back by
  134. // last call to RegCreateKey().
  135. //
  136. HKEY hsubkey;
  137. (void)StringCchPrintf( achTemp, NUMELMS(achTemp), TEXT("%ls"), szServerType );
  138. lreturn = RegCreateKey( hkey
  139. , achTemp
  140. , &hsubkey );
  141. if( ERROR_SUCCESS != lreturn )
  142. {
  143. RegCloseKey( hkey );
  144. return AmHresultFromWin32(lreturn);
  145. }
  146. // set Server string
  147. //
  148. (void)StringCchPrintf( achTemp, NUMELMS(achTemp), TEXT("%ls"), szFileName );
  149. lreturn = RegSetValue( hsubkey
  150. , (LPCTSTR)NULL
  151. , REG_SZ
  152. , (LPCTSTR)achTemp
  153. , sizeof(TCHAR) * (lstrlen(achTemp)+1) );
  154. if( ERROR_SUCCESS != lreturn )
  155. {
  156. RegCloseKey( hkey );
  157. RegCloseKey( hsubkey );
  158. return AmHresultFromWin32(lreturn);
  159. }
  160. (void)StringCchPrintf( achTemp, NUMELMS(achTemp), TEXT("%ls"), szThreadingModel );
  161. lreturn = RegSetValueEx( hsubkey
  162. , TEXT("ThreadingModel")
  163. , 0L
  164. , REG_SZ
  165. , (CONST BYTE *)achTemp
  166. , sizeof(TCHAR) * (lstrlen(achTemp)+1) );
  167. // close hkeys
  168. //
  169. RegCloseKey( hkey );
  170. RegCloseKey( hsubkey );
  171. // and return
  172. //
  173. return HRESULT_FROM_WIN32(lreturn);
  174. }
  175. //---------------------------------------------------------------------------
  176. //
  177. // AMovieSetupUnregisterServer()
  178. //
  179. // default ActiveMovie dll setup function
  180. // - to use must be called from an exported
  181. // function named DllRegisterServer()
  182. //
  183. //---------------------------------------------------------------------------
  184. STDAPI
  185. AMovieSetupUnregisterServer( CLSID clsServer )
  186. {
  187. // convert CLSID uuid to string and write
  188. // out subkey CLSID\{}
  189. //
  190. OLECHAR szCLSID[CHARS_IN_GUID];
  191. HRESULT hr = StringFromGUID2( clsServer
  192. , szCLSID
  193. , CHARS_IN_GUID );
  194. ASSERT( SUCCEEDED(hr) );
  195. TCHAR achBuffer[MAX_KEY_LEN];
  196. (void)StringCchPrintf( achBuffer, NUMELMS(achBuffer), TEXT("CLSID\\%ls"), szCLSID );
  197. // delete subkey
  198. //
  199. hr = EliminateSubKey( HKEY_CLASSES_ROOT, achBuffer );
  200. ASSERT( SUCCEEDED(hr) );
  201. // return
  202. //
  203. return NOERROR;
  204. }
  205. //---------------------------------------------------------------------------
  206. //
  207. // AMovieSetupRegisterFilter through IFilterMapper2
  208. //
  209. //---------------------------------------------------------------------------
  210. STDAPI
  211. AMovieSetupRegisterFilter2( const AMOVIESETUP_FILTER * const psetupdata
  212. , IFilterMapper2 * pIFM2
  213. , BOOL bRegister )
  214. {
  215. DbgLog((LOG_TRACE, 3, TEXT("= AMovieSetupRegisterFilter")));
  216. // check we've got data
  217. //
  218. if( NULL == psetupdata ) return S_FALSE;
  219. // unregister filter
  220. // (as pins are subkeys of filter's CLSID key
  221. // they do not need to be removed separately).
  222. //
  223. DbgLog((LOG_TRACE, 3, TEXT("= = unregister filter")));
  224. HRESULT hr = pIFM2->UnregisterFilter(
  225. 0, // default category
  226. 0, // default instance name
  227. *psetupdata->clsID );
  228. if( bRegister )
  229. {
  230. REGFILTER2 rf2;
  231. rf2.dwVersion = 1;
  232. rf2.dwMerit = psetupdata->dwMerit;
  233. rf2.cPins = psetupdata->nPins;
  234. rf2.rgPins = psetupdata->lpPin;
  235. // register filter
  236. //
  237. DbgLog((LOG_TRACE, 3, TEXT("= = register filter")));
  238. hr = pIFM2->RegisterFilter(*psetupdata->clsID
  239. , psetupdata->strName
  240. , 0 // moniker
  241. , 0 // category
  242. , NULL // instance
  243. , &rf2);
  244. }
  245. // handle one acceptable "error" - that
  246. // of filter not being registered!
  247. // (couldn't find a suitable #define'd
  248. // name for the error!)
  249. //
  250. if( 0x80070002 == hr)
  251. return NOERROR;
  252. else
  253. return hr;
  254. }
  255. //---------------------------------------------------------------------------
  256. //
  257. // RegisterAllServers()
  258. //
  259. //---------------------------------------------------------------------------
  260. /*
  261. STDAPI
  262. RegisterAllServers( LPCWSTR szFileName, BOOL bRegister )
  263. {
  264. HRESULT hr = NOERROR;
  265. for( int i = 0; i < g_cTemplates; i++ )
  266. {
  267. // get i'th template
  268. //
  269. const CFactoryTemplate *pT = &g_Templates[i];
  270. DbgLog((LOG_TRACE, 2, TEXT("- - register %ls"),
  271. (LPCWSTR)pT->m_Name ));
  272. // register CLSID and InprocServer32
  273. //
  274. if( bRegister )
  275. {
  276. hr = AMovieSetupRegisterServer( *(pT->m_ClsID)
  277. , (LPCWSTR)pT->m_Name
  278. , szFileName );
  279. }
  280. else
  281. {
  282. hr = AMovieSetupUnregisterServer( *(pT->m_ClsID) );
  283. }
  284. // check final error for this pass
  285. // and break loop if we failed
  286. //
  287. if( FAILED(hr) )
  288. break;
  289. }
  290. return hr;
  291. }
  292. */
  293. //---------------------------------------------------------------------------
  294. //
  295. // AMovieDllRegisterServer2()
  296. //
  297. // default ActiveMovie dll setup function
  298. // - to use must be called from an exported
  299. // function named DllRegisterServer()
  300. //
  301. // this function is table driven using the
  302. // static members of the CFactoryTemplate
  303. // class defined in the dll.
  304. //
  305. // it registers the Dll as the InprocServer32
  306. // and then calls the IAMovieSetup.Register
  307. // method.
  308. //
  309. //---------------------------------------------------------------------------
  310. //STDAPI
  311. //AMovieDllRegisterServer2( BOOL bRegister )
  312. //{
  313. // HRESULT hr = NOERROR;
  314. //
  315. // DbgLog((LOG_TRACE, 2, TEXT("AMovieDllRegisterServer2()")));
  316. //
  317. // // get file name (where g_hInst is the
  318. // // instance handle of the filter dll)
  319. // //
  320. // WCHAR achFileName[MAX_PATH];
  321. //
  322. // // WIN95 doesn't support GetModuleFileNameW
  323. // //
  324. // {
  325. // char achTemp[MAX_PATH];
  326. //
  327. // DbgLog((LOG_TRACE, 2, TEXT("- get module file name")));
  328. //
  329. // // g_hInst handle is set in our dll entry point. Make sure
  330. // // DllEntryPoint in dllentry.cpp is called
  331. // ASSERT(g_hInst != 0);
  332. //
  333. // if( 0 == GetModuleFileNameA( g_hInst
  334. // , achTemp
  335. // , sizeof(achTemp) ) )
  336. // {
  337. // // we've failed!
  338. // DWORD dwerr = GetLastError();
  339. // return AmHresultFromWin32(dwerr);
  340. // }
  341. //
  342. // MultiByteToWideChar( CP_ACP
  343. // , 0L
  344. // , achTemp
  345. // , lstrlenA(achTemp) + 1
  346. // , achFileName
  347. // , NUMELMS(achFileName) );
  348. // }
  349. //
  350. // //
  351. // // first registering, register all OLE servers
  352. // //
  353. // if( bRegister )
  354. // {
  355. // DbgLog((LOG_TRACE, 2, TEXT("- register OLE Servers")));
  356. // hr = RegisterAllServers( achFileName, TRUE );
  357. // }
  358. //
  359. // //
  360. // // next, register/unregister all filters
  361. // //
  362. //
  363. // if( SUCCEEDED(hr) )
  364. // {
  365. // // init is ref counted so call just in case
  366. // // we're being called cold.
  367. // //
  368. // DbgLog((LOG_TRACE, 2, TEXT("- CoInitialize")));
  369. // hr = CoInitialize( (LPVOID)NULL );
  370. // ASSERT( SUCCEEDED(hr) );
  371. //
  372. // // get hold of IFilterMapper2
  373. // //
  374. // DbgLog((LOG_TRACE, 2, TEXT("- obtain IFilterMapper2")));
  375. // IFilterMapper2 *pIFM2 = 0;
  376. // IFilterMapper *pIFM = 0;
  377. // hr = CoCreateInstance( CLSID_FilterMapper2
  378. // , NULL
  379. // , CLSCTX_INPROC_SERVER
  380. // , IID_IFilterMapper2
  381. // , (void **)&pIFM2 );
  382. // if(FAILED(hr))
  383. // {
  384. // DbgLog((LOG_TRACE, 2, TEXT("- trying IFilterMapper instead")));
  385. //
  386. // hr = CoCreateInstance(
  387. // CLSID_FilterMapper,
  388. // NULL,
  389. // CLSCTX_INPROC_SERVER,
  390. // IID_IFilterMapper,
  391. // (void **)&pIFM);
  392. // }
  393. // if( SUCCEEDED(hr) )
  394. // {
  395. // // scan through array of CFactoryTemplates
  396. // // registering servers and filters.
  397. // //
  398. // DbgLog((LOG_TRACE, 2, TEXT("- register Filters")));
  399. // for( int i = 0; i < g_cTemplates; i++ )
  400. // {
  401. // // get i'th template
  402. // //
  403. // const CFactoryTemplate *pT = &g_Templates[i];
  404. //
  405. // if( NULL != pT->m_pAMovieSetup_Filter )
  406. // {
  407. // DbgLog((LOG_TRACE, 2, TEXT("- - register %ls"), (LPCWSTR)pT->m_Name ));
  408. //
  409. // if(pIFM2)
  410. // {
  411. // hr = AMovieSetupRegisterFilter2( pT->m_pAMovieSetup_Filter, pIFM2, bRegister );
  412. // }
  413. // else
  414. // {
  415. // hr = AMovieSetupRegisterFilter( pT->m_pAMovieSetup_Filter, pIFM, bRegister );
  416. // }
  417. // }
  418. //
  419. // // check final error for this pass
  420. // // and break loop if we failed
  421. // //
  422. // if( FAILED(hr) )
  423. // break;
  424. // }
  425. //
  426. // // release interface
  427. // //
  428. // if(pIFM2)
  429. // pIFM2->Release();
  430. // else
  431. // pIFM->Release();
  432. //
  433. // }
  434. //
  435. // // and clear up
  436. // //
  437. // CoFreeUnusedLibraries();
  438. // CoUninitialize();
  439. // }
  440. //
  441. // //
  442. // // if unregistering, unregister all OLE servers
  443. // //
  444. // if( SUCCEEDED(hr) && !bRegister )
  445. // {
  446. // DbgLog((LOG_TRACE, 2, TEXT("- register OLE Servers")));
  447. // hr = RegisterAllServers( achFileName, FALSE );
  448. // }
  449. //
  450. // DbgLog((LOG_TRACE, 2, TEXT("- return %0x"), hr));
  451. // return hr;
  452. //}
  453. //---------------------------------------------------------------------------
  454. //
  455. // AMovieDllRegisterServer()
  456. //
  457. // default ActiveMovie dll setup function
  458. // - to use must be called from an exported
  459. // function named DllRegisterServer()
  460. //
  461. // this function is table driven using the
  462. // static members of the CFactoryTemplate
  463. // class defined in the dll.
  464. //
  465. // it registers the Dll as the InprocServer32
  466. // and then calls the IAMovieSetup.Register
  467. // method.
  468. //
  469. //---------------------------------------------------------------------------
  470. //STDAPI
  471. //AMovieDllRegisterServer( void )
  472. //{
  473. // HRESULT hr = NOERROR;
  474. //
  475. // // get file name (where g_hInst is the
  476. // // instance handle of the filter dll)
  477. // //
  478. // WCHAR achFileName[MAX_PATH];
  479. //
  480. // {
  481. // // WIN95 doesn't support GetModuleFileNameW
  482. // //
  483. // char achTemp[MAX_PATH];
  484. //
  485. // if( 0 == GetModuleFileNameA( g_hInst
  486. // , achTemp
  487. // , sizeof(achTemp) ) )
  488. // {
  489. // // we've failed!
  490. // DWORD dwerr = GetLastError();
  491. // return AmHresultFromWin32(dwerr);
  492. // }
  493. //
  494. // MultiByteToWideChar( CP_ACP
  495. // , 0L
  496. // , achTemp
  497. // , lstrlenA(achTemp) + 1
  498. // , achFileName
  499. // , NUMELMS(achFileName) );
  500. // }
  501. //
  502. // // scan through array of CFactoryTemplates
  503. // // registering servers and filters.
  504. // //
  505. // for( int i = 0; i < g_cTemplates; i++ )
  506. // {
  507. // // get i'th template
  508. // //
  509. // const CFactoryTemplate *pT = &g_Templates[i];
  510. //
  511. // // register CLSID and InprocServer32
  512. // //
  513. // hr = AMovieSetupRegisterServer( *(pT->m_ClsID)
  514. // , (LPCWSTR)pT->m_Name
  515. // , achFileName );
  516. //
  517. // // instantiate all servers and get hold of
  518. // // IAMovieSetup, if implemented, and call
  519. // // IAMovieSetup.Register() method
  520. // //
  521. // if( SUCCEEDED(hr) && (NULL != pT->m_lpfnNew) )
  522. // {
  523. // // instantiate object
  524. // //
  525. // PAMOVIESETUP psetup;
  526. // hr = CoCreateInstance( *(pT->m_ClsID)
  527. // , 0
  528. // , CLSCTX_INPROC_SERVER
  529. // , IID_IAMovieSetup
  530. // , reinterpret_cast<void**>(&psetup) );
  531. // if( SUCCEEDED(hr) )
  532. // {
  533. // hr = psetup->Unregister();
  534. // if( SUCCEEDED(hr) )
  535. // hr = psetup->Register();
  536. // psetup->Release();
  537. // }
  538. // else
  539. // {
  540. // if( (E_NOINTERFACE == hr )
  541. // || (VFW_E_NEED_OWNER == hr ) )
  542. // hr = NOERROR;
  543. // }
  544. // }
  545. //
  546. // // check final error for this pass
  547. // // and break loop if we failed
  548. // //
  549. // if( FAILED(hr) )
  550. // break;
  551. //
  552. // } // end-for
  553. //
  554. // return hr;
  555. //}
  556. //---------------------------------------------------------------------------
  557. //
  558. // AMovieDllUnregisterServer()
  559. //
  560. // default ActiveMovie dll uninstall function
  561. // - to use must be called from an exported
  562. // function named DllRegisterServer()
  563. //
  564. // this function is table driven using the
  565. // static members of the CFactoryTemplate
  566. // class defined in the dll.
  567. //
  568. // it calls the IAMovieSetup.Unregister
  569. // method and then unregisters the Dll
  570. // as the InprocServer32
  571. //
  572. //---------------------------------------------------------------------------
  573. //STDAPI
  574. //AMovieDllUnregisterServer()
  575. //{
  576. // // initialize return code
  577. // //
  578. // HRESULT hr = NOERROR;
  579. //
  580. // // scan through CFactory template and unregister
  581. // // all OLE servers and filters.
  582. // //
  583. // for( int i = g_cTemplates; i--; )
  584. // {
  585. // // get i'th template
  586. // //
  587. // const CFactoryTemplate *pT = &g_Templates[i];
  588. //
  589. // // check method exists
  590. // //
  591. // if( NULL != pT->m_lpfnNew )
  592. // {
  593. // // instantiate object
  594. // //
  595. // PAMOVIESETUP psetup;
  596. // hr = CoCreateInstance( *(pT->m_ClsID)
  597. // , 0
  598. // , CLSCTX_INPROC_SERVER
  599. // , IID_IAMovieSetup
  600. // , reinterpret_cast<void**>(&psetup) );
  601. // if( SUCCEEDED(hr) )
  602. // {
  603. // hr = psetup->Unregister();
  604. // psetup->Release();
  605. // }
  606. // else
  607. // {
  608. // if( (E_NOINTERFACE == hr )
  609. // || (VFW_E_NEED_OWNER == hr ) )
  610. // hr = NOERROR;
  611. // }
  612. // }
  613. //
  614. // // unregister CLSID and InprocServer32
  615. // //
  616. // if( SUCCEEDED(hr) )
  617. // {
  618. // hr = AMovieSetupUnregisterServer( *(pT->m_ClsID) );
  619. // }
  620. //
  621. // // check final error for this pass
  622. // // and break loop if we failed
  623. // //
  624. // if( FAILED(hr) )
  625. // break;
  626. // }
  627. //
  628. // return hr;
  629. //}