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.

280 lines
5.9 KiB

  1. // dll.cpp
  2. //
  3. // Dll entry points and IDirectSoundWaveFactory implementation
  4. //
  5. // READ THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!
  6. //
  7. // 4530: C++ exception handler used, but unwind semantics are not enabled. Specify -GX
  8. //
  9. // We disable this because we use exceptions and do *not* specify -GX (USE_NATIVE_EH in
  10. // sources).
  11. //
  12. // The one place we use exceptions is around construction of objects that call
  13. // InitializeCriticalSection. We guarantee that it is safe to use in this case with
  14. // the restriction given by not using -GX (automatic objects in the call chain between
  15. // throw and handler are not destructed). Turning on -GX buys us nothing but +10% to code
  16. // size because of the unwind code.
  17. //
  18. // Any other use of exceptions must follow these restrictions or -GX must be turned on.
  19. //
  20. // READ THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!
  21. //
  22. #pragma warning(disable:4530)
  23. #include <objbase.h>
  24. #include <mmsystem.h>
  25. #include <dsoundp.h>
  26. #include "debug.h"
  27. #include "oledll.h"
  28. #include "debug.h"
  29. #include "dmusicc.h"
  30. #include "dmusici.h"
  31. #include "riff.h"
  32. #include "dswave.h"
  33. #include <regstr.h>
  34. // Globals
  35. //
  36. // Version information for our class
  37. //
  38. char g_szFriendlyName[] = "Microsoft DirectSound Wave";
  39. char g_szVerIndProgID[] = "Microsoft.DirectSoundWave";
  40. char g_szProgID[] = "Microsoft.DirectSoundWave.1";
  41. // Dll's hModule
  42. //
  43. HMODULE g_hModule = NULL;
  44. // Track whether running on Unicode machine.
  45. BOOL g_fIsUnicode = FALSE;
  46. // Count of active components and class factory server locks
  47. //
  48. long g_cComponent = 0;
  49. long g_cLock = 0;
  50. static char const g_szDoEmulation[] = "DoEmulation";
  51. // CDirectSoundWaveFactory::QueryInterface
  52. //
  53. HRESULT __stdcall
  54. CDirectSoundWaveFactory::QueryInterface(const IID &iid,
  55. void **ppv)
  56. {
  57. if (iid == IID_IUnknown || iid == IID_IClassFactory) {
  58. *ppv = static_cast<IClassFactory*>(this);
  59. } else {
  60. *ppv = NULL;
  61. return E_NOINTERFACE;
  62. }
  63. reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  64. return S_OK;
  65. }
  66. CDirectSoundWaveFactory::CDirectSoundWaveFactory()
  67. {
  68. m_cRef = 1;
  69. InterlockedIncrement(&g_cLock);
  70. }
  71. CDirectSoundWaveFactory::~CDirectSoundWaveFactory()
  72. {
  73. InterlockedDecrement(&g_cLock);
  74. }
  75. // CDirectSoundWaveFactory::AddRef
  76. //
  77. ULONG __stdcall
  78. CDirectSoundWaveFactory::AddRef()
  79. {
  80. return InterlockedIncrement(&m_cRef);
  81. }
  82. // CDirectSoundWaveFactory::Release
  83. //
  84. ULONG __stdcall
  85. CDirectSoundWaveFactory::Release()
  86. {
  87. if (!InterlockedDecrement(&m_cRef)) {
  88. delete this;
  89. return 0;
  90. }
  91. return m_cRef;
  92. }
  93. // CDirectSoundWaveFactory::CreateInstance
  94. //
  95. //
  96. HRESULT __stdcall
  97. CDirectSoundWaveFactory::CreateInstance(IUnknown* pUnknownOuter,
  98. const IID& iid,
  99. void** ppv)
  100. {
  101. HRESULT hr;
  102. if (pUnknownOuter) {
  103. return CLASS_E_NOAGGREGATION;
  104. }
  105. CWave *pWave;
  106. try
  107. {
  108. pWave = new CWave;
  109. }
  110. catch( ... )
  111. {
  112. return E_OUTOFMEMORY;
  113. }
  114. if (pWave == NULL) {
  115. return E_OUTOFMEMORY;
  116. }
  117. hr = pWave->QueryInterface(iid, ppv);
  118. pWave->Release();
  119. return hr;
  120. }
  121. // CDirectSoundWaveFactory::LockServer
  122. //
  123. HRESULT __stdcall
  124. CDirectSoundWaveFactory::LockServer(BOOL bLock)
  125. {
  126. if (bLock) {
  127. InterlockedIncrement(&g_cLock);
  128. } else {
  129. InterlockedDecrement(&g_cLock);
  130. }
  131. return S_OK;
  132. }
  133. // Standard calls needed to be an inproc server
  134. //
  135. STDAPI DllCanUnloadNow()
  136. {
  137. if (g_cComponent || g_cLock) {
  138. return S_FALSE;
  139. }
  140. return S_OK;
  141. }
  142. STDAPI DllGetClassObject(const CLSID& clsid,
  143. const IID& iid,
  144. void** ppv)
  145. {
  146. IUnknown* pIUnknown = NULL;
  147. if(clsid == CLSID_DirectSoundWave)
  148. {
  149. pIUnknown = static_cast<IUnknown*> (new CDirectSoundWaveFactory);
  150. if(!pIUnknown)
  151. {
  152. return E_OUTOFMEMORY;
  153. }
  154. }
  155. else
  156. {
  157. return CLASS_E_CLASSNOTAVAILABLE;
  158. }
  159. HRESULT hr = pIUnknown->QueryInterface(iid, ppv);
  160. pIUnknown->Release();
  161. return hr;
  162. }
  163. STDAPI DllUnregisterServer()
  164. {
  165. UnregisterServer(CLSID_DirectSoundWave,
  166. g_szFriendlyName,
  167. g_szVerIndProgID,
  168. g_szProgID);
  169. return S_OK;
  170. }
  171. STDAPI DllRegisterServer()
  172. {
  173. RegisterServer(g_hModule,
  174. CLSID_DirectSoundWave,
  175. g_szFriendlyName,
  176. g_szVerIndProgID,
  177. g_szProgID);
  178. return S_OK;
  179. }
  180. extern void DebugInit();
  181. // Standard Win32 DllMain
  182. //
  183. #ifdef DBG
  184. static char* aszReasons[] =
  185. {
  186. "DLL_PROCESS_DETACH",
  187. "DLL_PROCESS_ATTACH",
  188. "DLL_THREAD_ATTACH",
  189. "DLL_THREAD_DETACH"
  190. };
  191. const DWORD nReasons = (sizeof(aszReasons) / sizeof(char*));
  192. #endif
  193. BOOL APIENTRY DllMain(HINSTANCE hModule,
  194. DWORD dwReason,
  195. void *lpReserved)
  196. {
  197. OSVERSIONINFO osvi;
  198. static int nReferenceCount = 0;
  199. #ifdef DBG
  200. if (dwReason < nReasons)
  201. {
  202. DebugTrace(0, "DllMain: %s\n", (LPSTR)aszReasons[dwReason]);
  203. }
  204. else
  205. {
  206. DebugTrace(0, "DllMain: Unknown dwReason <%u>\n", dwReason);
  207. }
  208. #endif
  209. if (dwReason == DLL_PROCESS_ATTACH) {
  210. if (++nReferenceCount == 1)
  211. {
  212. DisableThreadLibraryCalls(hModule);
  213. g_hModule = hModule;
  214. osvi.dwOSVersionInfoSize = sizeof(osvi);
  215. GetVersionEx(&osvi);
  216. g_fIsUnicode =
  217. (osvi.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS);
  218. #ifdef DBG
  219. DebugInit();
  220. #endif
  221. }
  222. }
  223. #ifdef DBG
  224. else if (dwReason == DLL_PROCESS_DETACH) {
  225. if (--nReferenceCount == 0)
  226. {
  227. TraceI(-1, "Unloading DSWave : g_cLock = %d, g_cComponent = %d", g_cLock, g_cComponent);
  228. // Assert if we still have some objects hanging around
  229. assert(g_cComponent == 0);
  230. assert(g_cLock == 0);
  231. }
  232. }
  233. #endif
  234. return TRUE;
  235. }