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.

537 lines
16 KiB

  1. // Copyright (c) 1998-1999 Microsoft Corporation
  2. // loader dll.cpp
  3. //
  4. // Dll entry points and CToolFactory, CContainerFactory implementation
  5. //
  6. // READ THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!
  7. //
  8. // 4530: C++ exception handler used, but unwind semantics are not enabled. Specify -GX
  9. //
  10. // We disable this because we use exceptions and do *not* specify -GX (USE_NATIVE_EH in
  11. // sources).
  12. //
  13. // The one place we use exceptions is around construction of objects that call
  14. // InitializeCriticalSection. We guarantee that it is safe to use in this case with
  15. // the restriction given by not using -GX (automatic objects in the call chain between
  16. // throw and handler are not destructed). Turning on -GX buys us nothing but +10% to code
  17. // size because of the unwind code.
  18. //
  19. // Any other use of exceptions must follow these restrictions or -GX must be turned on.
  20. //
  21. // READ THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!
  22. //
  23. #pragma warning(disable:4530)
  24. #include <objbase.h>
  25. #include "debug.h"
  26. #include "oledll.h"
  27. #include "debug.h"
  28. #include "dmusicc.h"
  29. #include "dmusici.h"
  30. #include "BaseTool.h"
  31. #include "Echo.h"
  32. #include "Transpose.h"
  33. #include "Duration.h"
  34. #include "Quantize.h"
  35. #include "TimeShift.h"
  36. #include "Swing.h"
  37. #include "Velocity.h"
  38. #ifndef UNDER_CE
  39. #include <regstr.h>
  40. #endif
  41. // Globals
  42. //
  43. // Version information for our class
  44. //
  45. TCHAR g_szEchoFriendlyName[] = TEXT("Microsoft Echo Tool");
  46. TCHAR g_szEchoShortName[] = TEXT("Echo");
  47. TCHAR g_szEchoDescription[] = TEXT("Echoes notes");
  48. TCHAR g_szEchoVerIndProgID[] = TEXT("Microsoft.DirectMusicEchoTool");
  49. TCHAR g_szEchoProgID[] = TEXT("Microsoft.DirectMusicEchoTool.1");
  50. TCHAR g_szTransposeFriendlyName[] = TEXT("Microsoft Transpose Tool");
  51. TCHAR g_szTransposeShortName[] = TEXT("Transpose");
  52. TCHAR g_szTransposeDescription[] = TEXT("Transposes notes");
  53. TCHAR g_szTransposeVerIndProgID[] = TEXT("Microsoft.DirectMusicTransposeTool");
  54. TCHAR g_szTransposeProgID[] = TEXT("Microsoft.DirectMusicTransposeTool.1");
  55. TCHAR g_szDurationFriendlyName[] = TEXT("Microsoft Duration Modifier Tool");
  56. TCHAR g_szDurationShortName[] = TEXT("Duration");
  57. TCHAR g_szDurationDescription[] = TEXT("Scales note durations");
  58. TCHAR g_szDurationVerIndProgID[] = TEXT("Microsoft.DirectMusicDurationTool");
  59. TCHAR g_szDurationProgID[] = TEXT("Microsoft.DirectMusicDurationTool.1");
  60. TCHAR g_szQuantizeFriendlyName[] = TEXT("Microsoft Quantize Tool");
  61. TCHAR g_szQuantizeShortName[] = TEXT("Quantize");
  62. TCHAR g_szQuantizeDescription[] = TEXT("Quantizes note starts and durations");
  63. TCHAR g_szQuantizeVerIndProgID[] = TEXT("Microsoft.DirectMusicQuantizeTool");
  64. TCHAR g_szQuantizeProgID[] = TEXT("Microsoft.DirectMusicQuantizeTool.1");
  65. TCHAR g_szTimeShiftFriendlyName[] = TEXT("Microsoft Time Shift Tool");
  66. TCHAR g_szTimeShiftShortName[] = TEXT("Time Shift");
  67. TCHAR g_szTimeShiftDescription[] = TEXT("Shifts and randomizes note starts");
  68. TCHAR g_szTimeShiftVerIndProgID[] = TEXT("Microsoft.DirectMusicTimeShiftTool");
  69. TCHAR g_szTimeShiftProgID[] = TEXT("Microsoft.DirectMusicTimeShiftTool.1");
  70. TCHAR g_szSwingFriendlyName[] = TEXT("Microsoft Swing Tool");
  71. TCHAR g_szSwingShortName[] = TEXT("Swing");
  72. TCHAR g_szSwingDescription[] = TEXT("Changes the timing to a adopt a triplet rhythm");
  73. TCHAR g_szSwingVerIndProgID[] = TEXT("Microsoft.DirectMusicSwingTool");
  74. TCHAR g_szSwingProgID[] = TEXT("Microsoft.DirectMusicSwingTool.1");
  75. TCHAR g_szVelocityFriendlyName[] = TEXT("Microsoft Velocity Transform Tool");
  76. TCHAR g_szVelocityShortName[] = TEXT("Velocity Transform");
  77. TCHAR g_szVelocityDescription[] = TEXT("Modifies note velocities");
  78. TCHAR g_szVelocityVerIndProgID[] = TEXT("Microsoft.DirectMusicVelocityTool");
  79. TCHAR g_szVelocityProgID[] = TEXT("Microsoft.DirectMusicVelocityTool.1");
  80. // Dll's hModule
  81. //
  82. HMODULE g_hModule = NULL;
  83. #ifndef UNDER_CE
  84. // Track whether running on Unicode machine.
  85. BOOL g_fIsUnicode = FALSE;
  86. #endif
  87. // Count of active components and class factory server locks
  88. //
  89. long g_cComponent = 0;
  90. long g_cLock = 0;
  91. // CToolFactory::QueryInterface
  92. //
  93. HRESULT __stdcall
  94. CToolFactory::QueryInterface(const IID &iid,
  95. void **ppv)
  96. {
  97. if (iid == IID_IUnknown || iid == IID_IClassFactory) {
  98. *ppv = static_cast<IClassFactory*>(this);
  99. } else {
  100. *ppv = NULL;
  101. return E_NOINTERFACE;
  102. }
  103. reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  104. return S_OK;
  105. }
  106. CToolFactory::CToolFactory(DWORD dwToolType)
  107. {
  108. m_cRef = 1;
  109. m_dwToolType = dwToolType;
  110. InterlockedIncrement(&g_cLock);
  111. }
  112. CToolFactory::~CToolFactory()
  113. {
  114. InterlockedDecrement(&g_cLock);
  115. }
  116. // CToolFactory::AddRef
  117. //
  118. ULONG __stdcall
  119. CToolFactory::AddRef()
  120. {
  121. return InterlockedIncrement(&m_cRef);
  122. }
  123. // CToolFactory::Release
  124. //
  125. ULONG __stdcall
  126. CToolFactory::Release()
  127. {
  128. if (!InterlockedDecrement(&m_cRef)) {
  129. delete this;
  130. return 0;
  131. }
  132. return m_cRef;
  133. }
  134. // CToolFactory::CreateInstance
  135. //
  136. //
  137. HRESULT __stdcall
  138. CToolFactory::CreateInstance(IUnknown* pUnknownOuter,
  139. const IID& iid,
  140. void** ppv)
  141. {
  142. HRESULT hr;
  143. if (pUnknownOuter) {
  144. return CLASS_E_NOAGGREGATION;
  145. }
  146. CBaseTool *pTool;
  147. switch (m_dwToolType)
  148. {
  149. case TOOL_ECHO:
  150. pTool = new CEchoTool;
  151. break;
  152. case TOOL_TRANSPOSE:
  153. pTool = new CTransposeTool;
  154. break;
  155. case TOOL_SWING:
  156. pTool = new CSwingTool;
  157. break;
  158. case TOOL_DURATION:
  159. pTool = new CDurationTool;
  160. break;
  161. case TOOL_QUANTIZE:
  162. pTool = new CQuantizeTool;
  163. break;
  164. case TOOL_TIMESHIFT:
  165. pTool = new CTimeShiftTool;
  166. break;
  167. case TOOL_VELOCITY:
  168. pTool = new CVelocityTool;
  169. break;
  170. }
  171. if (pTool == NULL) {
  172. return E_OUTOFMEMORY;
  173. }
  174. hr = pTool->QueryInterface(iid, ppv);
  175. pTool->Release();
  176. return hr;
  177. }
  178. // CToolFactory::LockServer
  179. //
  180. HRESULT __stdcall
  181. CToolFactory::LockServer(BOOL bLock)
  182. {
  183. if (bLock) {
  184. InterlockedIncrement(&g_cLock);
  185. } else {
  186. InterlockedDecrement(&g_cLock);
  187. }
  188. return S_OK;
  189. }
  190. // Standard calls needed to be an inproc server
  191. //
  192. STDAPI DllCanUnloadNow()
  193. {
  194. if (g_cComponent || g_cLock) {
  195. return S_FALSE;
  196. }
  197. return S_OK;
  198. }
  199. STDAPI DllGetClassObject(const CLSID& clsid,
  200. const IID& iid,
  201. void** ppv)
  202. {
  203. IUnknown* pIUnknown = NULL;
  204. DWORD dwTypeID = 0;
  205. if(clsid == CLSID_DirectMusicEchoTool)
  206. {
  207. dwTypeID = TOOL_ECHO;
  208. }
  209. else if(clsid == CLSID_DirectMusicTransposeTool)
  210. {
  211. dwTypeID = TOOL_TRANSPOSE;
  212. }
  213. else if(clsid == CLSID_DirectMusicDurationTool)
  214. {
  215. dwTypeID = TOOL_DURATION;
  216. }
  217. else if(clsid == CLSID_DirectMusicQuantizeTool)
  218. {
  219. dwTypeID = TOOL_QUANTIZE;
  220. }
  221. else if(clsid == CLSID_DirectMusicTimeShiftTool)
  222. {
  223. dwTypeID = TOOL_TIMESHIFT;
  224. }
  225. else if(clsid == CLSID_DirectMusicSwingTool)
  226. {
  227. dwTypeID = TOOL_SWING;
  228. }
  229. else if(clsid == CLSID_DirectMusicVelocityTool)
  230. {
  231. dwTypeID = TOOL_VELOCITY;
  232. }
  233. else
  234. {
  235. return CLASS_E_CLASSNOTAVAILABLE;
  236. }
  237. pIUnknown = static_cast<IUnknown*> (new CToolFactory(dwTypeID));
  238. if(pIUnknown)
  239. {
  240. HRESULT hr = pIUnknown->QueryInterface(iid, ppv);
  241. pIUnknown->Release();
  242. return hr;
  243. }
  244. return E_OUTOFMEMORY;
  245. }
  246. const TCHAR cszToolRegRoot[] = TEXT(DMUS_REGSTR_PATH_TOOLS) TEXT("\\");
  247. const TCHAR cszDescriptionKey[] = TEXT("Description");
  248. const TCHAR cszNameKey[] = TEXT("Name");
  249. const TCHAR cszShortNameKey[] = TEXT("ShortName");
  250. const int CLSID_STRING_SIZE = 39;
  251. HRESULT CLSIDToStr(const CLSID &clsid, TCHAR *szStr, int cbStr);
  252. HRESULT RegisterTool(REFGUID guid,
  253. const TCHAR szDescription[],
  254. const TCHAR szShortName[],
  255. const TCHAR szName[])
  256. {
  257. HKEY hk;
  258. TCHAR szCLSID[CLSID_STRING_SIZE];
  259. TCHAR szRegKey[256];
  260. HRESULT hr = CLSIDToStr(guid, szCLSID, sizeof(szCLSID));
  261. if (!SUCCEEDED(hr))
  262. {
  263. return hr;
  264. }
  265. lstrcpy(szRegKey, cszToolRegRoot);
  266. lstrcat(szRegKey, szCLSID);
  267. if (RegCreateKey(HKEY_LOCAL_MACHINE,
  268. szRegKey,
  269. &hk))
  270. {
  271. return E_FAIL;
  272. }
  273. hr = S_OK;
  274. if (RegSetValueEx(hk,
  275. cszDescriptionKey,
  276. 0L,
  277. REG_SZ,
  278. (CONST BYTE*)szDescription,
  279. lstrlen(szDescription) + 1))
  280. {
  281. hr = E_FAIL;
  282. }
  283. if (RegSetValueEx(hk,
  284. cszNameKey,
  285. 0L,
  286. REG_SZ,
  287. (CONST BYTE*)szName,
  288. lstrlen(szName) + 1))
  289. {
  290. hr = E_FAIL;
  291. }
  292. if (RegSetValueEx(hk,
  293. cszShortNameKey,
  294. 0L,
  295. REG_SZ,
  296. (CONST BYTE*)szShortName,
  297. lstrlen(szName) + 1))
  298. {
  299. hr = E_FAIL;
  300. }
  301. RegCloseKey(hk);
  302. return hr;
  303. }
  304. HRESULT UnregisterTool(REFGUID guid)
  305. {
  306. HKEY hk;
  307. TCHAR szCLSID[CLSID_STRING_SIZE];
  308. TCHAR szRegKey[256];
  309. HRESULT hr = CLSIDToStr(guid, szCLSID, sizeof(szCLSID));
  310. if (!SUCCEEDED(hr))
  311. {
  312. return hr;
  313. }
  314. lstrcpy(szRegKey, cszToolRegRoot);
  315. lstrcat(szRegKey, szCLSID);
  316. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,szRegKey,0,KEY_ALL_ACCESS | KEY_WRITE, &hk))
  317. {
  318. return E_FAIL;
  319. }
  320. hr = S_OK;
  321. if (RegDeleteValue(hk,cszDescriptionKey))
  322. {
  323. hr = E_FAIL;
  324. }
  325. if (RegDeleteValue(hk,cszNameKey))
  326. {
  327. hr = E_FAIL;
  328. }
  329. if (RegDeleteValue(hk,cszShortNameKey))
  330. {
  331. hr = E_FAIL;
  332. }
  333. RegCloseKey(hk);
  334. if (RegDeleteKey(HKEY_LOCAL_MACHINE,szRegKey))
  335. {
  336. hr = E_FAIL;
  337. }
  338. return hr;
  339. }
  340. STDAPI DllUnregisterServer()
  341. {
  342. UnregisterServer(CLSID_DirectMusicEchoTool,
  343. g_szEchoFriendlyName,
  344. g_szEchoVerIndProgID,
  345. g_szEchoProgID);
  346. UnregisterTool(CLSID_DirectMusicEchoTool);
  347. UnregisterServer(CLSID_DirectMusicTransposeTool,
  348. g_szTransposeFriendlyName,
  349. g_szTransposeVerIndProgID,
  350. g_szTransposeProgID);
  351. UnregisterTool(CLSID_DirectMusicTransposeTool);
  352. UnregisterServer(CLSID_DirectMusicDurationTool,
  353. g_szDurationFriendlyName,
  354. g_szDurationVerIndProgID,
  355. g_szDurationProgID);
  356. UnregisterTool(CLSID_DirectMusicDurationTool);
  357. UnregisterServer(CLSID_DirectMusicQuantizeTool,
  358. g_szQuantizeFriendlyName,
  359. g_szQuantizeVerIndProgID,
  360. g_szQuantizeProgID);
  361. UnregisterTool(CLSID_DirectMusicQuantizeTool);
  362. UnregisterServer(CLSID_DirectMusicSwingTool,
  363. g_szSwingFriendlyName,
  364. g_szSwingVerIndProgID,
  365. g_szSwingProgID);
  366. UnregisterTool(CLSID_DirectMusicSwingTool);
  367. UnregisterServer(CLSID_DirectMusicTimeShiftTool,
  368. g_szTimeShiftFriendlyName,
  369. g_szTimeShiftVerIndProgID,
  370. g_szTimeShiftProgID);
  371. UnregisterTool(CLSID_DirectMusicTimeShiftTool);
  372. UnregisterServer(CLSID_DirectMusicVelocityTool,
  373. g_szVelocityFriendlyName,
  374. g_szVelocityVerIndProgID,
  375. g_szVelocityProgID);
  376. UnregisterTool(CLSID_DirectMusicVelocityTool);
  377. return S_OK;
  378. }
  379. STDAPI DllRegisterServer()
  380. {
  381. RegisterServer(g_hModule,
  382. CLSID_DirectMusicEchoTool,
  383. g_szEchoFriendlyName,
  384. g_szEchoVerIndProgID,
  385. g_szEchoProgID);
  386. RegisterTool(CLSID_DirectMusicEchoTool, g_szEchoDescription, g_szEchoShortName, g_szEchoFriendlyName);
  387. RegisterServer(g_hModule,
  388. CLSID_DirectMusicTransposeTool,
  389. g_szTransposeFriendlyName,
  390. g_szTransposeVerIndProgID,
  391. g_szTransposeProgID);
  392. RegisterTool(CLSID_DirectMusicTransposeTool, g_szTransposeDescription, g_szTransposeShortName, g_szTransposeFriendlyName);
  393. RegisterServer(g_hModule,
  394. CLSID_DirectMusicDurationTool,
  395. g_szDurationFriendlyName,
  396. g_szDurationVerIndProgID,
  397. g_szDurationProgID);
  398. RegisterTool(CLSID_DirectMusicDurationTool, g_szDurationDescription, g_szDurationShortName, g_szDurationFriendlyName);
  399. RegisterServer(g_hModule,
  400. CLSID_DirectMusicQuantizeTool,
  401. g_szQuantizeFriendlyName,
  402. g_szQuantizeVerIndProgID,
  403. g_szQuantizeProgID);
  404. RegisterTool(CLSID_DirectMusicQuantizeTool, g_szQuantizeDescription, g_szQuantizeShortName, g_szQuantizeFriendlyName);
  405. RegisterServer(g_hModule,
  406. CLSID_DirectMusicSwingTool,
  407. g_szSwingFriendlyName,
  408. g_szSwingVerIndProgID,
  409. g_szSwingProgID);
  410. RegisterTool(CLSID_DirectMusicSwingTool, g_szSwingDescription, g_szSwingShortName, g_szSwingFriendlyName);
  411. RegisterServer(g_hModule,
  412. CLSID_DirectMusicTimeShiftTool,
  413. g_szTimeShiftFriendlyName,
  414. g_szTimeShiftVerIndProgID,
  415. g_szTimeShiftProgID);
  416. RegisterTool(CLSID_DirectMusicTimeShiftTool, g_szTimeShiftDescription, g_szTimeShiftShortName, g_szTimeShiftFriendlyName);
  417. RegisterServer(g_hModule,
  418. CLSID_DirectMusicVelocityTool,
  419. g_szVelocityFriendlyName,
  420. g_szVelocityVerIndProgID,
  421. g_szVelocityProgID);
  422. RegisterTool(CLSID_DirectMusicVelocityTool, g_szVelocityDescription, g_szVelocityShortName, g_szVelocityFriendlyName);
  423. return S_OK;
  424. }
  425. extern void DebugInit();
  426. // Standard Win32 DllMain
  427. //
  428. #ifdef DBG
  429. static char* aszReasons[] =
  430. {
  431. "DLL_PROCESS_DETACH",
  432. "DLL_PROCESS_ATTACH",
  433. "DLL_THREAD_ATTACH",
  434. "DLL_THREAD_DETACH"
  435. };
  436. const DWORD nReasons = (sizeof(aszReasons) / sizeof(char*));
  437. #endif
  438. BOOL APIENTRY DllMain(HINSTANCE hModule,
  439. DWORD dwReason,
  440. void *lpReserved)
  441. {
  442. static int nReferenceCount = 0;
  443. #ifdef DBG
  444. if (dwReason < nReasons)
  445. {
  446. DebugTrace(0, "DllMain: %s\n", (LPSTR)aszReasons[dwReason]);
  447. }
  448. else
  449. {
  450. DebugTrace(0, "DllMain: Unknown dwReason <%u>\n", dwReason);
  451. }
  452. #endif
  453. if (dwReason == DLL_PROCESS_ATTACH) {
  454. if (++nReferenceCount == 1)
  455. {
  456. g_hModule = (HMODULE)hModule;
  457. #ifndef UNDER_CE
  458. OSVERSIONINFO osvi;
  459. DisableThreadLibraryCalls(hModule);
  460. osvi.dwOSVersionInfoSize = sizeof(osvi);
  461. GetVersionEx(&osvi);
  462. g_fIsUnicode =
  463. (osvi.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS);
  464. #endif
  465. #ifdef DBG
  466. DebugInit();
  467. #endif
  468. }
  469. }
  470. return TRUE;
  471. }