Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
5.3 KiB

  1. //+---------------------------------------------------------------------
  2. //
  3. // File: srfact.cxx
  4. //
  5. // Contents: Class Factory
  6. //
  7. // Classes: SRFactory
  8. //
  9. //------------------------------------------------------------------------
  10. //#pragma warning(disable:4103)
  11. #include <stdlib.h>
  12. #include <windows.h>
  13. #include <windowsx.h>
  14. #include <commdlg.h> // common dialog boxes
  15. #include <ole2.h>
  16. #include <o2base.hxx> // the base classes and utilities
  17. #include <initguid.h>
  18. #include "srs.hxx"
  19. SRFactory * gpSRFactory = NULL;
  20. DWORD gdwRegisterClass = 0;
  21. extern "C" BOOL
  22. CreateSRClassFactory(HINSTANCE hinst,BOOL fEmbedded)
  23. {
  24. BOOL fRet = FALSE;
  25. if(SRFactory::Create(hinst))
  26. {
  27. gpSRFactory->AddRef();
  28. if(fEmbedded)
  29. {
  30. DOUT(TEXT("SoundRec: Registering SRFactory\r\n"));
  31. HRESULT hr = CoRegisterClassObject(CLSID_SOUNDREC,
  32. (LPUNKNOWN)(LPCLASSFACTORY)gpSRFactory,
  33. CLSCTX_LOCAL_SERVER,
  34. //REGCLS_MULTI_SEPARATE,
  35. REGCLS_SINGLEUSE,
  36. &gdwRegisterClass);
  37. if(OK(hr))
  38. {
  39. CoLockObjectExternal((LPUNKNOWN)(LPCLASSFACTORY)gpSRFactory,
  40. TRUE, TRUE);
  41. fRet = TRUE;
  42. }
  43. #if DBG
  44. else
  45. {
  46. TCHAR achBuffer[256];
  47. wsprintf(achBuffer,
  48. TEXT("SoundRec: CoRegisterClassObject (%lx)\r\n"),
  49. (long)hr);
  50. OutputDebugString(achBuffer);
  51. }
  52. #endif
  53. }
  54. else
  55. {
  56. fRet = TRUE;
  57. }
  58. }
  59. return fRet;
  60. }
  61. extern "C" HRESULT
  62. ReleaseSRClassFactory(void)
  63. {
  64. HRESULT hr = NOERROR;
  65. if(gdwRegisterClass)
  66. {
  67. #if DBG
  68. OutputDebugString(TEXT("SoundRec: Revoking SRFactory\r\n"));
  69. #endif
  70. CoLockObjectExternal((LPUNKNOWN)(LPCLASSFACTORY)gpSRFactory,
  71. FALSE, TRUE);
  72. hr = CoRevokeClassObject(gdwRegisterClass);
  73. gdwRegisterClass = 0;
  74. gpSRFactory->Release();
  75. gpSRFactory = NULL;
  76. }
  77. return hr;
  78. }
  79. BOOL
  80. SRFactory::Create(HINSTANCE hinst)
  81. {
  82. gpSRFactory = new SRFactory;
  83. //
  84. // initialize our classes
  85. //
  86. if (gpSRFactory == NULL
  87. || !gpSRFactory->Init(hinst)
  88. || !SRCtrl::ClassInit(gpSRFactory->_pClass)
  89. || !SRDV::ClassInit(gpSRFactory->_pClass)
  90. || !SRInPlace::ClassInit(gpSRFactory->_pClass))
  91. {
  92. return FALSE;
  93. }
  94. return TRUE;
  95. }
  96. //+---------------------------------------------------------------
  97. //
  98. // Member: SRFactory:::Init
  99. //
  100. // Synopsis: Initializes the class factory
  101. //
  102. // Arguments: [hinst] -- instance handle of the module with
  103. // class descriptor resources
  104. //
  105. // Returns: TRUE iff the factory was successfully initialized
  106. //
  107. //----------------------------------------------------------------
  108. BOOL
  109. SRFactory::Init(HINSTANCE hinst)
  110. {
  111. //
  112. // Register the standard OLE clipboard formats.
  113. // These are available in the OleClipFormat array.
  114. //
  115. RegisterOleClipFormats();
  116. if((_pClass = new ClassDescriptor) == NULL)
  117. return FALSE;
  118. BOOL fRet = _pClass->Init(hinst, 0); //IDS_CLASSID);
  119. //
  120. // Patch _pClass->_haccel with a resource reload
  121. // (for InPlace) due to mismatch between the base class
  122. // resource scheme and legacy code...
  123. //
  124. if(fRet)
  125. {
  126. if((_pClass->_haccel = LoadAccelerators(hinst, TEXT("SOUNDREC"))) == NULL)
  127. fRet = FALSE;
  128. }
  129. return fRet;
  130. }
  131. STDMETHODIMP
  132. SRFactory::LockServer(BOOL fLock)
  133. {
  134. return CoLockObjectExternal((LPUNKNOWN)gpSRFactory, fLock, TRUE);
  135. }
  136. //+---------------------------------------------------------------
  137. //
  138. // Member: SRFactory:::CreateInstance
  139. //
  140. // Synopsis: Member of IClassFactory interface
  141. //
  142. //----------------------------------------------------------------
  143. STDMETHODIMP
  144. SRFactory::CreateInstance(LPUNKNOWN pUnkOuter, REFIID iid, LPVOID FAR* ppv)
  145. {
  146. #if DBG
  147. OutputDebugString(TEXT("SRFactory::CreateInstance\r\n"));
  148. #endif
  149. *ppv = NULL; //NULL output param
  150. //
  151. // create an object, then query for the appropriate interface
  152. //
  153. LPUNKNOWN pUnk;
  154. LPSRCTRL pTemp;
  155. HRESULT hr;
  156. if (OK(hr = SRCtrl::Create(pUnkOuter, _pClass, &pUnk, &pTemp)))
  157. {
  158. hr = pUnk->QueryInterface(iid, ppv);
  159. pUnk->Release(); // on failure this will release obj, otherwise
  160. // it will ensure an object ref count of 1
  161. }
  162. return hr;
  163. }
  164. /*
  165. * for the creation of a moniker
  166. */
  167. BOOL
  168. CreateStandaloneObject(void)
  169. {
  170. DOUT(TEXT("Soundrec CreateStandaloneObject\r\n"));
  171. //
  172. //Ensure a unique filename in gachLinkFilename so we can create valid
  173. //FileMonikers...
  174. //
  175. if(gachLinkFilename[0] == 0)
  176. BuildUniqueLinkName();
  177. BOOL fSuccess = FALSE;
  178. LPVOID pvUnk;
  179. HRESULT hr = gpSRFactory->CreateInstance(NULL, IID_IUnknown, (LPVOID FAR*)&pvUnk);
  180. if(hr == NOERROR)
  181. {
  182. //CoLockObjectExternal((LPUNKNOWN)(LPOLEOBJECT)gpCtrlThis, TRUE, TRUE);
  183. extern LPSRCTRL gpCtrlThis;
  184. gpCtrlThis->Lock();
  185. fSuccess = TRUE;
  186. }
  187. else
  188. {
  189. DOUT(TEXT("Soundrec CreateStandaloneObject FAILED!\r\n"));
  190. fSuccess = FALSE;
  191. }
  192. return fSuccess;
  193. }