Leaked source code of windows server 2003
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.

771 lines
19 KiB

  1. //
  2. // dmcmpdll.cpp
  3. //
  4. // Copyright (c) 1997-1999 Microsoft Corporation
  5. //
  6. // Note: Dll entry points as well as Class Factory implementations.
  7. //
  8. // READ THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9. //
  10. // 4530: C++ exception handler used, but unwind semantics are not enabled. Specify -GX
  11. //
  12. // We disable this because we use exceptions and do *not* specify -GX (USE_NATIVE_EH in
  13. // sources).
  14. //
  15. // The one place we use exceptions is around construction of objects that call
  16. // InitializeCriticalSection. We guarantee that it is safe to use in this case with
  17. // the restriction given by not using -GX (automatic objects in the call chain between
  18. // throw and handler are not destructed). Turning on -GX buys us nothing but +10% to code
  19. // size because of the unwind code.
  20. //
  21. // Any other use of exceptions must follow these restrictions or -GX must be turned on.
  22. //
  23. // READ THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!
  24. //
  25. #pragma warning(disable:4530)
  26. #include <objbase.h>
  27. #include "debug.h"
  28. #include "debug.h"
  29. #include "..\shared\oledll.h"
  30. #include <initguid.h>
  31. #include "dmusici.h"
  32. #include "DMCompP.h"
  33. #include "dmpers.h"
  34. #include "dmcompos.h"
  35. #include "dmtempl.h"
  36. #include "spsttrk.h"
  37. #include "perstrk.h"
  38. #include "..\shared\Validate.h"
  39. #include "..\dmstyle\dmstyleP.h"
  40. #include "..\dmime\dmgraph.h"
  41. //////////////////////////////////////////////////////////////////////
  42. // Globals
  43. // Version information
  44. //
  45. TCHAR g_szComposerFriendlyName[] = TEXT("DirectMusicComposer");
  46. TCHAR g_szComposerVerIndProgID[] = TEXT("Microsoft.DirectMusicComposer");
  47. TCHAR g_szComposerProgID[] = TEXT("Microsoft.DirectMusicComposer.1");
  48. TCHAR g_szChordMapFriendlyName[] = TEXT("DirectMusicChordMap");
  49. TCHAR g_szChordMapVerIndProgID[] = TEXT("Microsoft.DirectMusicChordMap");
  50. TCHAR g_szChordMapProgID[] = TEXT("Microsoft.DirectMusicChordMap.1");
  51. TCHAR g_szChordMapTrackFriendlyName[] = TEXT("DirectMusicChordMapTrack");
  52. TCHAR g_szChordMapTrackVerIndProgID[] = TEXT("Microsoft.DirectMusicChordMapTrack");
  53. TCHAR g_szChordMapTrackProgID[] = TEXT("Microsoft.DirectMusicChordMapTrack.1");
  54. TCHAR g_szSignPostTrackFriendlyName[] = TEXT("DirectMusicSignPostTrack");
  55. TCHAR g_szSignPostTrackVerIndProgID[] = TEXT("Microsoft.DirectMusicSignPostTrack");
  56. TCHAR g_szSignPostTrackProgID[] = TEXT("Microsoft.DirectMusicSignPostTrack.1");
  57. TCHAR g_szTemplateFriendlyName[] = TEXT("DirectMusicTemplate");
  58. TCHAR g_szTemplateVerIndProgID[] = TEXT("Microsoft.DirectMusicTemplate");
  59. TCHAR g_szTemplateProgID[] = TEXT("Microsoft.DirectMusicTemplate.1");
  60. // Dll's hModule
  61. //
  62. HMODULE g_hModule = NULL;
  63. // Count of active components and class factory server locks
  64. //
  65. long g_cComponent = 0;
  66. long g_cLock = 0;
  67. //////////////////////////////////////////////////////////////////////
  68. // CDirectMusicPersonalityFactory::QueryInterface
  69. HRESULT __stdcall
  70. CDirectMusicPersonalityFactory::QueryInterface(const IID &iid,
  71. void **ppv)
  72. {
  73. V_INAME(CDirectMusicPersonalityFactory::QueryInterface);
  74. V_PTRPTR_WRITE(ppv);
  75. V_REFGUID(iid);
  76. if (iid == IID_IUnknown || iid == IID_IClassFactory) {
  77. *ppv = static_cast<IClassFactory*>(this);
  78. } else {
  79. *ppv = NULL;
  80. return E_NOINTERFACE;
  81. }
  82. reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  83. return S_OK;
  84. }
  85. //////////////////////////////////////////////////////////////////////
  86. // CDirectMusicPersonalityFactory::AddRef
  87. ULONG __stdcall
  88. CDirectMusicPersonalityFactory::AddRef()
  89. {
  90. return InterlockedIncrement(&m_cRef);
  91. }
  92. //////////////////////////////////////////////////////////////////////
  93. // CDirectMusicPersonalityFactory::Release
  94. ULONG __stdcall
  95. CDirectMusicPersonalityFactory::Release()
  96. {
  97. if (!InterlockedDecrement(&m_cRef)) {
  98. delete this;
  99. return 0;
  100. }
  101. return m_cRef;
  102. }
  103. //////////////////////////////////////////////////////////////////////
  104. // CDirectMusicPersonalityFactory::CreateInstance
  105. HRESULT __stdcall
  106. CDirectMusicPersonalityFactory::CreateInstance(IUnknown* pUnknownOuter,
  107. const IID& iid,
  108. void** ppv)
  109. {
  110. HRESULT hr;
  111. if (pUnknownOuter) {
  112. return CLASS_E_NOAGGREGATION;
  113. }
  114. CDMPers *pDM;
  115. try
  116. {
  117. pDM = new CDMPers;
  118. }
  119. catch( ... )
  120. {
  121. return E_OUTOFMEMORY;
  122. }
  123. if (pDM == NULL) {
  124. return E_OUTOFMEMORY;
  125. }
  126. hr = pDM->QueryInterface(iid, ppv);
  127. pDM->Release();
  128. return hr;
  129. }
  130. //////////////////////////////////////////////////////////////////////
  131. // CDirectMusicPersonalityFactory::LockServer
  132. HRESULT __stdcall
  133. CDirectMusicPersonalityFactory::LockServer(BOOL bLock)
  134. {
  135. if (bLock) {
  136. InterlockedIncrement(&g_cLock);
  137. } else {
  138. InterlockedDecrement(&g_cLock);
  139. }
  140. return S_OK;
  141. }
  142. //////////////////////////////////////////////////////////////////////
  143. // CDirectMusicComposerFactory::QueryInterface
  144. HRESULT __stdcall
  145. CDirectMusicComposerFactory::QueryInterface(const IID &iid,
  146. void **ppv)
  147. {
  148. V_INAME(CDirectMusicComposerFactory::QueryInterface);
  149. V_PTRPTR_WRITE(ppv);
  150. V_REFGUID(iid);
  151. if (iid == IID_IUnknown || iid == IID_IClassFactory) {
  152. *ppv = static_cast<IClassFactory*>(this);
  153. } else {
  154. *ppv = NULL;
  155. return E_NOINTERFACE;
  156. }
  157. reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  158. return S_OK;
  159. }
  160. //////////////////////////////////////////////////////////////////////
  161. // CDirectMusicComposerFactory::AddRef
  162. ULONG __stdcall
  163. CDirectMusicComposerFactory::AddRef()
  164. {
  165. return InterlockedIncrement(&m_cRef);
  166. }
  167. //////////////////////////////////////////////////////////////////////
  168. // CDirectMusicComposerFactory::Release
  169. ULONG __stdcall
  170. CDirectMusicComposerFactory::Release()
  171. {
  172. if (!InterlockedDecrement(&m_cRef)) {
  173. delete this;
  174. return 0;
  175. }
  176. return m_cRef;
  177. }
  178. //////////////////////////////////////////////////////////////////////
  179. // CDirectMusicComposerFactory::CreateInstance
  180. HRESULT __stdcall
  181. CDirectMusicComposerFactory::CreateInstance(IUnknown* pUnknownOuter,
  182. const IID& iid,
  183. void** ppv)
  184. {
  185. HRESULT hr;
  186. // DebugBreak();
  187. if (pUnknownOuter) {
  188. return CLASS_E_NOAGGREGATION;
  189. }
  190. CDMCompos *pDM = new CDMCompos;
  191. if (pDM == NULL) {
  192. return E_OUTOFMEMORY;
  193. }
  194. hr = pDM->QueryInterface(iid, ppv);
  195. pDM->Release();
  196. return hr;
  197. }
  198. //////////////////////////////////////////////////////////////////////
  199. // CDirectMusicComposerFactory::LockServer
  200. HRESULT __stdcall
  201. CDirectMusicComposerFactory::LockServer(BOOL bLock)
  202. {
  203. if (bLock) {
  204. InterlockedIncrement(&g_cLock);
  205. } else {
  206. InterlockedDecrement(&g_cLock);
  207. }
  208. return S_OK;
  209. }
  210. //////////////////////////////////////////////////////////////////////
  211. // CDirectMusicTemplateFactory::QueryInterface
  212. HRESULT __stdcall
  213. CDirectMusicTemplateFactory::QueryInterface(const IID &iid,
  214. void **ppv)
  215. {
  216. V_INAME(CDirectMusicTemplateFactory::QueryInterface);
  217. V_PTRPTR_WRITE(ppv);
  218. V_REFGUID(iid);
  219. if (iid == IID_IUnknown || iid == IID_IClassFactory) {
  220. *ppv = static_cast<IClassFactory*>(this);
  221. } else {
  222. *ppv = NULL;
  223. return E_NOINTERFACE;
  224. }
  225. reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  226. return S_OK;
  227. }
  228. //////////////////////////////////////////////////////////////////////
  229. // CDirectMusicTemplateFactory::AddRef
  230. ULONG __stdcall
  231. CDirectMusicTemplateFactory::AddRef()
  232. {
  233. return InterlockedIncrement(&m_cRef);
  234. }
  235. //////////////////////////////////////////////////////////////////////
  236. // CDirectMusicTemplateFactory::Release
  237. ULONG __stdcall
  238. CDirectMusicTemplateFactory::Release()
  239. {
  240. if (!InterlockedDecrement(&m_cRef)) {
  241. delete this;
  242. return 0;
  243. }
  244. return m_cRef;
  245. }
  246. //////////////////////////////////////////////////////////////////////
  247. // CDirectMusicTemplateFactory::CreateInstance
  248. HRESULT __stdcall
  249. CDirectMusicTemplateFactory::CreateInstance(IUnknown* pUnknownOuter,
  250. const IID& iid,
  251. void** ppv)
  252. {
  253. HRESULT hr;
  254. // DebugBreak();
  255. if (pUnknownOuter) {
  256. return CLASS_E_NOAGGREGATION;
  257. }
  258. CDMTempl *pDM;
  259. try
  260. {
  261. pDM = new CDMTempl;
  262. }
  263. catch( ... )
  264. {
  265. return E_OUTOFMEMORY;
  266. }
  267. if (pDM == NULL) {
  268. return E_OUTOFMEMORY;
  269. }
  270. hr = pDM->QueryInterface(iid, ppv);
  271. pDM->Release();
  272. return hr;
  273. }
  274. //////////////////////////////////////////////////////////////////////
  275. // CDirectMusicTemplateFactory::LockServer
  276. HRESULT __stdcall
  277. CDirectMusicTemplateFactory::LockServer(BOOL bLock)
  278. {
  279. if (bLock) {
  280. InterlockedIncrement(&g_cLock);
  281. } else {
  282. InterlockedDecrement(&g_cLock);
  283. }
  284. return S_OK;
  285. }
  286. //////////////////////////////////////////////////////////////////////
  287. // CDirectMusicSignPostTrackFactory::QueryInterface
  288. HRESULT __stdcall
  289. CDirectMusicSignPostTrackFactory::QueryInterface(const IID &iid,
  290. void **ppv)
  291. {
  292. V_INAME(CDirectMusicSignPostTrackFactory::QueryInterface);
  293. V_PTRPTR_WRITE(ppv);
  294. V_REFGUID(iid);
  295. if (iid == IID_IUnknown || iid == IID_IClassFactory) {
  296. *ppv = static_cast<IClassFactory*>(this);
  297. } else {
  298. *ppv = NULL;
  299. return E_NOINTERFACE;
  300. }
  301. reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  302. return S_OK;
  303. }
  304. //////////////////////////////////////////////////////////////////////
  305. // CDirectMusicSignPostTrackFactory::AddRef
  306. ULONG __stdcall
  307. CDirectMusicSignPostTrackFactory::AddRef()
  308. {
  309. return InterlockedIncrement(&m_cRef);
  310. }
  311. //////////////////////////////////////////////////////////////////////
  312. // CDirectMusicSignPostTrackFactory::Release
  313. ULONG __stdcall
  314. CDirectMusicSignPostTrackFactory::Release()
  315. {
  316. if (!InterlockedDecrement(&m_cRef)) {
  317. delete this;
  318. return 0;
  319. }
  320. return m_cRef;
  321. }
  322. //////////////////////////////////////////////////////////////////////
  323. // CDirectMusicSignPostTrackFactory::CreateInstance
  324. HRESULT __stdcall
  325. CDirectMusicSignPostTrackFactory::CreateInstance(IUnknown* pUnknownOuter,
  326. const IID& iid,
  327. void** ppv)
  328. {
  329. HRESULT hr;
  330. // DebugBreak();
  331. if (pUnknownOuter) {
  332. return CLASS_E_NOAGGREGATION;
  333. }
  334. CSPstTrk *pDM;
  335. try
  336. {
  337. pDM = new CSPstTrk;
  338. }
  339. catch( ... )
  340. {
  341. return E_OUTOFMEMORY;
  342. }
  343. if (pDM == NULL) {
  344. return E_OUTOFMEMORY;
  345. }
  346. hr = pDM->QueryInterface(iid, ppv);
  347. pDM->Release();
  348. return hr;
  349. }
  350. //////////////////////////////////////////////////////////////////////
  351. // CDirectMusicSignPostTrackFactory::LockServer
  352. HRESULT __stdcall
  353. CDirectMusicSignPostTrackFactory::LockServer(BOOL bLock)
  354. {
  355. if (bLock) {
  356. InterlockedIncrement(&g_cLock);
  357. } else {
  358. InterlockedDecrement(&g_cLock);
  359. }
  360. return S_OK;
  361. }
  362. //////////////////////////////////////////////////////////////////////
  363. // CDirectMusicPersonalityTrackFactory::QueryInterface
  364. HRESULT __stdcall
  365. CDirectMusicPersonalityTrackFactory::QueryInterface(const IID &iid,
  366. void **ppv)
  367. {
  368. V_INAME(CDirectMusicPersonalityTrackFactory::QueryInterface);
  369. V_PTRPTR_WRITE(ppv);
  370. V_REFGUID(iid);
  371. if (iid == IID_IUnknown || iid == IID_IClassFactory) {
  372. *ppv = static_cast<IClassFactory*>(this);
  373. } else {
  374. *ppv = NULL;
  375. return E_NOINTERFACE;
  376. }
  377. reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  378. return S_OK;
  379. }
  380. //////////////////////////////////////////////////////////////////////
  381. // CDirectMusicPersonalityTrackFactory::AddRef
  382. ULONG __stdcall
  383. CDirectMusicPersonalityTrackFactory::AddRef()
  384. {
  385. return InterlockedIncrement(&m_cRef);
  386. }
  387. //////////////////////////////////////////////////////////////////////
  388. // CDirectMusicPersonalityTrackFactory::Release
  389. ULONG __stdcall
  390. CDirectMusicPersonalityTrackFactory::Release()
  391. {
  392. if (!InterlockedDecrement(&m_cRef)) {
  393. delete this;
  394. return 0;
  395. }
  396. return m_cRef;
  397. }
  398. //////////////////////////////////////////////////////////////////////
  399. // CDirectMusicPersonalityFactory::CreateInstance
  400. HRESULT __stdcall
  401. CDirectMusicPersonalityTrackFactory::CreateInstance(IUnknown* pUnknownOuter,
  402. const IID& iid,
  403. void** ppv)
  404. {
  405. HRESULT hr;
  406. if (pUnknownOuter) {
  407. return CLASS_E_NOAGGREGATION;
  408. }
  409. CPersonalityTrack *pDM;
  410. try
  411. {
  412. pDM = new CPersonalityTrack;
  413. }
  414. catch( ... )
  415. {
  416. return E_OUTOFMEMORY;
  417. }
  418. if (pDM == NULL) {
  419. return E_OUTOFMEMORY;
  420. }
  421. hr = pDM->QueryInterface(iid, ppv);
  422. pDM->Release();
  423. return hr;
  424. }
  425. //////////////////////////////////////////////////////////////////////
  426. // CDirectMusicPersonalityTrackFactory::LockServer
  427. HRESULT __stdcall
  428. CDirectMusicPersonalityTrackFactory::LockServer(BOOL bLock)
  429. {
  430. if (bLock) {
  431. InterlockedIncrement(&g_cLock);
  432. } else {
  433. InterlockedDecrement(&g_cLock);
  434. }
  435. return S_OK;
  436. }
  437. //////////////////////////////////////////////////////////////////////
  438. // Standard calls needed to be an inproc server
  439. //////////////////////////////////////////////////////////////////////
  440. // DllCanUnloadNow
  441. STDAPI DllCanUnloadNow()
  442. {
  443. if (g_cComponent || g_cLock) {
  444. return S_FALSE;
  445. }
  446. return S_OK;
  447. }
  448. //////////////////////////////////////////////////////////////////////
  449. // DllGetClassObject
  450. STDAPI DllGetClassObject(const CLSID& clsid,
  451. const IID& iid,
  452. void** ppv)
  453. {
  454. IUnknown* pIUnknown = NULL;
  455. if(clsid == CLSID_DirectMusicChordMap)
  456. {
  457. pIUnknown = static_cast<IUnknown*> (new CDirectMusicPersonalityFactory);
  458. if(!pIUnknown)
  459. {
  460. return E_OUTOFMEMORY;
  461. }
  462. }
  463. else if(clsid == CLSID_DirectMusicComposer)
  464. {
  465. pIUnknown = static_cast<IUnknown*> (new CDirectMusicComposerFactory);
  466. if(!pIUnknown)
  467. {
  468. return E_OUTOFMEMORY;
  469. }
  470. }
  471. else if(clsid == CLSID_DMTempl)
  472. {
  473. pIUnknown = static_cast<IUnknown*> (new CDirectMusicTemplateFactory);
  474. if(!pIUnknown)
  475. {
  476. return E_OUTOFMEMORY;
  477. }
  478. }
  479. else if(clsid == CLSID_DirectMusicSignPostTrack)
  480. {
  481. pIUnknown = static_cast<IUnknown*> (new CDirectMusicSignPostTrackFactory);
  482. if(!pIUnknown)
  483. {
  484. return E_OUTOFMEMORY;
  485. }
  486. }
  487. else if(clsid == CLSID_DirectMusicChordMapTrack)
  488. {
  489. pIUnknown = static_cast<IUnknown*> (new CDirectMusicPersonalityTrackFactory);
  490. if(!pIUnknown)
  491. {
  492. return E_OUTOFMEMORY;
  493. }
  494. }
  495. else
  496. {
  497. return CLASS_E_CLASSNOTAVAILABLE;
  498. }
  499. HRESULT hr = pIUnknown->QueryInterface(iid, ppv);
  500. pIUnknown->Release();
  501. return hr;
  502. }
  503. //////////////////////////////////////////////////////////////////////
  504. // DllUnregisterServer
  505. STDAPI DllUnregisterServer()
  506. {
  507. UnregisterServer(CLSID_DirectMusicChordMap,
  508. g_szChordMapFriendlyName,
  509. g_szChordMapVerIndProgID,
  510. g_szChordMapProgID);
  511. UnregisterServer(CLSID_DirectMusicComposer,
  512. g_szComposerFriendlyName,
  513. g_szComposerVerIndProgID,
  514. g_szComposerProgID);
  515. UnregisterServer(CLSID_DMTempl,
  516. g_szTemplateFriendlyName,
  517. g_szTemplateVerIndProgID,
  518. g_szTemplateProgID);
  519. UnregisterServer(CLSID_DirectMusicSignPostTrack,
  520. g_szSignPostTrackFriendlyName,
  521. g_szSignPostTrackVerIndProgID,
  522. g_szSignPostTrackProgID);
  523. UnregisterServer(CLSID_DirectMusicChordMapTrack,
  524. g_szChordMapTrackFriendlyName,
  525. g_szChordMapTrackVerIndProgID,
  526. g_szChordMapTrackProgID);
  527. return S_OK;
  528. }
  529. //////////////////////////////////////////////////////////////////////
  530. // DllRegisterServer
  531. STDAPI DllRegisterServer()
  532. {
  533. RegisterServer(g_hModule,
  534. CLSID_DirectMusicChordMap,
  535. g_szChordMapFriendlyName,
  536. g_szChordMapVerIndProgID,
  537. g_szChordMapProgID);
  538. RegisterServer(g_hModule,
  539. CLSID_DirectMusicComposer,
  540. g_szComposerFriendlyName,
  541. g_szComposerVerIndProgID,
  542. g_szComposerProgID);
  543. RegisterServer(g_hModule,
  544. CLSID_DMTempl,
  545. g_szTemplateFriendlyName,
  546. g_szTemplateVerIndProgID,
  547. g_szTemplateProgID);
  548. RegisterServer(g_hModule,
  549. CLSID_DirectMusicSignPostTrack,
  550. g_szSignPostTrackFriendlyName,
  551. g_szSignPostTrackVerIndProgID,
  552. g_szSignPostTrackProgID);
  553. RegisterServer(g_hModule,
  554. CLSID_DirectMusicChordMapTrack,
  555. g_szChordMapTrackFriendlyName,
  556. g_szChordMapTrackVerIndProgID,
  557. g_szChordMapTrackProgID);
  558. return S_OK;
  559. }
  560. //////////////////////////////////////////////////////////////////////
  561. // Standard Win32 DllMain
  562. //////////////////////////////////////////////////////////////////////
  563. // DllMain
  564. #ifdef DBG
  565. static char* aszReasons[] =
  566. {
  567. "DLL_PROCESS_DETACH",
  568. "DLL_PROCESS_ATTACH",
  569. "DLL_THREAD_ATTACH",
  570. "DLL_THREAD_DETACH"
  571. };
  572. const DWORD nReasons = (sizeof(aszReasons) / sizeof(char*));
  573. #endif
  574. BOOL APIENTRY DllMain(HINSTANCE hModule,
  575. DWORD dwReason,
  576. void *lpReserved)
  577. {
  578. static int nReferenceCount = 0;
  579. #ifdef DBG
  580. if (dwReason < nReasons)
  581. {
  582. TraceI(0, "DllMain: %s\n", (LPSTR)aszReasons[dwReason]);
  583. }
  584. else
  585. {
  586. TraceI(0, "DllMain: Unknown dwReason <%u>\n", dwReason);
  587. }
  588. #endif
  589. switch(dwReason)
  590. {
  591. case DLL_PROCESS_ATTACH:
  592. if (++nReferenceCount == 1)
  593. {
  594. #ifdef DBG
  595. DebugInit();
  596. #endif
  597. if (!DisableThreadLibraryCalls(hModule))
  598. {
  599. TraceI(0, "DisableThreadLibraryCalls failed.\n");
  600. }
  601. g_hModule = hModule;
  602. }
  603. break;
  604. #ifdef DBG
  605. case DLL_PROCESS_DETACH:
  606. if (--nReferenceCount == 0)
  607. {
  608. TraceI(-1, "Unloading g_cLock %d g_cComponent %d\n", g_cLock, g_cComponent);
  609. // Assert if we still have some objects hanging around
  610. assert(g_cComponent == 0);
  611. assert(g_cLock == 0);
  612. }
  613. break;
  614. #endif
  615. }
  616. return TRUE;
  617. }