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.

504 lines
12 KiB

  1. /* (C) Copyright Microsoft Corporation 1991-1994. All Rights Reserved */
  2. #include <windows.h>
  3. #include <windowsx.h>
  4. #include <mmsystem.h>
  5. #include <regstr.h>
  6. #include "reg.h"
  7. const TCHAR szRegPath[] = REGSTR_PATH_WINDOWSAPPLETS TEXT("\\Sound Recorder");
  8. /* ReadRegistryData
  9. *
  10. * Reads information from the registry
  11. *
  12. * Parameters:
  13. *
  14. * pEntryNode - The node under Media Player which should be opened
  15. * for this data. If this is NULL, the value is
  16. * written directly under szRegPath.
  17. *
  18. * pEntryName - The name of the value under pEntryNode to be retrieved.
  19. *
  20. * pType - Pointer to a buffer to receive type of data read. May be NULL.
  21. *
  22. * pData - Pointer to a buffer to receive the value data.
  23. *
  24. * Size - Size, in bytes, of the buffer pointed to by pData.
  25. *
  26. * Return:
  27. *
  28. * Registry status return (NO_ERROR is good)
  29. *
  30. *
  31. * Andrew Bell (andrewbe) wrote it, 10 September 1992
  32. *
  33. */
  34. DWORD ReadRegistryData( LPTSTR pEntryNode,
  35. LPTSTR pEntryName,
  36. PDWORD pType,
  37. LPBYTE pData,
  38. DWORD DataSize )
  39. {
  40. DWORD Status;
  41. HKEY hkeyRegPath;
  42. HKEY hkeyEntryNode;
  43. DWORD Size;
  44. Status = RegOpenKeyEx( HKEY_CURRENT_USER, szRegPath, 0,
  45. KEY_READ, &hkeyRegPath );
  46. if( Status == NO_ERROR )
  47. {
  48. /* Open the sub-node:
  49. */
  50. if( pEntryNode )
  51. Status = RegOpenKeyEx( hkeyRegPath, pEntryNode, 0,
  52. KEY_READ, &hkeyEntryNode );
  53. else
  54. hkeyEntryNode = hkeyRegPath;
  55. if( Status == NO_ERROR )
  56. {
  57. Size = DataSize;
  58. /* Read the entry from the registry:
  59. */
  60. Status = RegQueryValueEx( hkeyEntryNode,
  61. pEntryName,
  62. 0,
  63. pType,
  64. pData,
  65. &Size );
  66. if( pEntryNode )
  67. RegCloseKey( hkeyEntryNode );
  68. }
  69. RegCloseKey( hkeyRegPath );
  70. }
  71. return Status;
  72. }
  73. DWORD QueryRegistryDataSize(
  74. LPTSTR pEntryNode,
  75. LPTSTR pEntryName,
  76. DWORD *pDataSize )
  77. {
  78. DWORD Status;
  79. HKEY hkeyRegPath;
  80. HKEY hkeyEntryNode;
  81. DWORD Size;
  82. /* Open the top-level node. For Media Player this is:
  83. * "Software\\Microsoft\\Windows NT\\CurrentVersion\\Sound Recorder"
  84. */
  85. Status = RegOpenKeyEx( HKEY_CURRENT_USER, szRegPath, 0,
  86. KEY_READ, &hkeyRegPath );
  87. if( Status == NO_ERROR )
  88. {
  89. /* Open the sub-node:
  90. */
  91. if( pEntryNode )
  92. Status = RegOpenKeyEx( hkeyRegPath, pEntryNode, 0,
  93. KEY_READ, &hkeyEntryNode );
  94. else
  95. hkeyEntryNode = hkeyRegPath;
  96. if( Status == NO_ERROR )
  97. {
  98. /* Read the entry from the registry:
  99. */
  100. Status = RegQueryValueEx( hkeyEntryNode,
  101. pEntryName,
  102. 0,
  103. NULL,
  104. NULL,
  105. &Size );
  106. if (Status == NO_ERROR)
  107. *pDataSize = Size;
  108. if( pEntryNode )
  109. RegCloseKey( hkeyEntryNode );
  110. }
  111. RegCloseKey( hkeyRegPath );
  112. }
  113. return Status;
  114. }
  115. /* WriteRegistryData
  116. *
  117. * Writes a bunch of information to the registry
  118. *
  119. * Parameters:
  120. *
  121. * pEntryNode - The node under szRegPath which should be created
  122. * or opened for this data. If this is NULL, the value is
  123. * written directly under szRegPath.
  124. *
  125. * pEntryName - The name of the value under pEntryNode to be set.
  126. *
  127. * Type - Type of data to read (e.g. REG_SZ).
  128. *
  129. * pData - Pointer to the value data to be written. If this is NULL,
  130. * the value under pEntryNode is deleted.
  131. *
  132. * Size - Size, in bytes, of the buffer pointed to by pData.
  133. *
  134. *
  135. * This routine is fairly generic, apart from the name of the top-level node.
  136. *
  137. * The data are stored in the following registry tree:
  138. *
  139. * HKEY_CURRENT_USER
  140. *
  141. * Software
  142. *
  143. * Microsoft
  144. *
  145. * Windows NT
  146. *
  147. * CurrentVersion
  148. *
  149. * Media Player
  150. *
  151. * AVIVideo
  152. *
  153. * DisplayPosition
  154. *
  155. * SysIni
  156. *
  157. *
  158. * Return:
  159. *
  160. * Registry status return (NO_ERROR is good)
  161. *
  162. *
  163. * Andrew Bell (andrewbe) wrote it, 10 September 1992
  164. *
  165. */
  166. DWORD WriteRegistryData( LPTSTR pEntryNode,
  167. LPTSTR pEntryName,
  168. DWORD Type,
  169. LPBYTE pData,
  170. DWORD Size )
  171. {
  172. DWORD Status;
  173. HKEY hkeyRegPath;
  174. HKEY hkeyEntryNode;
  175. /* Open or create the top-level node. For Media Player this is:
  176. * "Software\\Microsoft\\Windows NT\\CurrentVersion\\Media Player"
  177. */
  178. Status = RegCreateKeyEx( HKEY_CURRENT_USER, szRegPath, 0,
  179. NULL, 0, KEY_WRITE, NULL, &hkeyRegPath, NULL );
  180. if( Status == NO_ERROR )
  181. {
  182. /* Open or create the sub-node.
  183. */
  184. if( pEntryNode )
  185. Status = RegCreateKeyEx( hkeyRegPath, pEntryNode, 0,
  186. NULL, 0, KEY_WRITE, NULL, &hkeyEntryNode, NULL );
  187. else
  188. hkeyEntryNode = hkeyRegPath;
  189. if( Status == NO_ERROR )
  190. {
  191. if( pData )
  192. {
  193. Status = RegSetValueEx( hkeyEntryNode,
  194. pEntryName,
  195. 0,
  196. Type,
  197. pData,
  198. Size );
  199. }
  200. else
  201. {
  202. Status = RegDeleteValue( hkeyEntryNode, pEntryName );
  203. }
  204. if( pEntryNode )
  205. RegCloseKey( hkeyEntryNode );
  206. }
  207. RegCloseKey( hkeyRegPath );
  208. }
  209. return Status;
  210. }
  211. /*
  212. * Save/Restore window position
  213. */
  214. BOOL SoundRec_GetSetRegistryRect(
  215. HWND hwnd,
  216. int Get)
  217. {
  218. const TCHAR aszXPos[] = TEXT("X");
  219. const TCHAR aszYPos[] = TEXT("Y");
  220. RECT rcWnd,rc;
  221. if (!GetWindowRect(hwnd, &rcWnd))
  222. return FALSE;
  223. switch (Get)
  224. {
  225. case SGSRR_GET:
  226. if (ReadRegistryData((LPTSTR)NULL
  227. , (LPTSTR)aszXPos
  228. , NULL
  229. , (LPBYTE)&rc.left
  230. , sizeof(rc.left)) != NO_ERROR)
  231. {
  232. break;
  233. }
  234. if (ReadRegistryData((LPTSTR)NULL
  235. , (LPTSTR)aszYPos
  236. , NULL
  237. , (LPBYTE)&rc.top
  238. , sizeof(rc.top)) != NO_ERROR)
  239. {
  240. break;
  241. }
  242. //
  243. // Restore window position
  244. //
  245. MoveWindow(hwnd
  246. , rc.left
  247. , rc.top
  248. , rcWnd.right - rcWnd.left
  249. , rcWnd.bottom - rcWnd.top
  250. , FALSE );
  251. return TRUE;
  252. case SGSRR_SET:
  253. //
  254. // don't save iconic or hidden window states
  255. //
  256. if (IsIconic(hwnd) || !IsWindowVisible(hwnd))
  257. break;
  258. if (WriteRegistryData((LPTSTR)NULL
  259. , (LPTSTR)aszXPos
  260. , REG_DWORD
  261. , (LPBYTE)&rcWnd.left
  262. , sizeof(rcWnd.left)) != NO_ERROR)
  263. {
  264. break;
  265. }
  266. if (WriteRegistryData((LPTSTR)NULL
  267. , (LPTSTR)aszYPos
  268. , REG_DWORD
  269. , (LPBYTE)&rcWnd.top
  270. , sizeof(rcWnd.top)) != NO_ERROR)
  271. {
  272. break;
  273. }
  274. return TRUE;
  275. default:
  276. break;
  277. }
  278. return FALSE;
  279. }
  280. /*
  281. *
  282. * */
  283. const TCHAR szAudioRegPath[] = REGSTR_PATH_MULTIMEDIA_AUDIO;
  284. const TCHAR szWaveFormats[] = REGSTR_PATH_MULTIMEDIA_AUDIO TEXT("\\WaveFormats");
  285. const TCHAR szDefaultFormat[] = TEXT("DefaultFormat");
  286. /*
  287. * BOOL SoundRec_SetDefaultFormat
  288. *
  289. * Write the DefaultFormat friendly name into the registry. Under DAYTONA
  290. * we don't have a UI to set DefaultFormat, so this is a way of setting
  291. * it from an application.
  292. *
  293. * Under Chicago, the Audio page in MMCPL manages this information.
  294. * */
  295. BOOL SoundRec_SetDefaultFormat(
  296. LPTSTR lpFormat)
  297. {
  298. DWORD cbFormat;
  299. DWORD Status;
  300. HKEY hkeyRegPath;
  301. cbFormat = (lstrlen(lpFormat) + 1) * sizeof(TCHAR);
  302. //
  303. // Don't store NULL.
  304. //
  305. if (cbFormat <= sizeof(TCHAR) )
  306. return FALSE;
  307. Status = RegOpenKeyEx( HKEY_CURRENT_USER, szAudioRegPath, 0,
  308. KEY_WRITE, &hkeyRegPath );
  309. if (Status != NO_ERROR)
  310. return FALSE;
  311. //
  312. // Get the format tag string
  313. //
  314. Status = RegSetValueEx( hkeyRegPath,
  315. szDefaultFormat,
  316. 0,
  317. REG_SZ,
  318. (CONST BYTE*)lpFormat,
  319. cbFormat );
  320. return (Status == NO_ERROR);
  321. }
  322. /* IsValidWFX
  323. *
  324. * Validate the wfx in case it was corrupted.
  325. *
  326. * */
  327. BOOL IsValidWFX(
  328. LPWAVEFORMATEX lpwfx,
  329. DWORD cbwfx)
  330. {
  331. if (cbwfx < sizeof(WAVEFORMAT))
  332. return FALSE;
  333. if (lpwfx->nChannels == 0)
  334. return FALSE;
  335. if (lpwfx->nSamplesPerSec == 0)
  336. return FALSE;
  337. if (lpwfx->nAvgBytesPerSec == 0)
  338. return FALSE;
  339. if (lpwfx->nBlockAlign == 0)
  340. return FALSE;
  341. if (lpwfx->wFormatTag == WAVE_FORMAT_PCM)
  342. return TRUE;
  343. if (cbwfx < (sizeof(WAVEFORMATEX) + lpwfx->cbSize))
  344. return FALSE;
  345. return TRUE;
  346. }
  347. /*
  348. * BOOL SoundRec_GetDefaultFormat
  349. *
  350. * Default format is stored in a public area of the registry.
  351. *
  352. * */
  353. BOOL SoundRec_GetDefaultFormat(
  354. LPWAVEFORMATEX *ppwfx,
  355. DWORD *pcbwfx)
  356. {
  357. DWORD Status;
  358. HKEY hkeyRegPath;
  359. HKEY hkeyFmtPath;
  360. LPTSTR lpsz;
  361. DWORD cbsz;
  362. DWORD cbwfx = 0;
  363. LPWAVEFORMATEX lpwfx = NULL;
  364. //
  365. // Null out params
  366. //
  367. *ppwfx = NULL;
  368. *pcbwfx = 0;
  369. Status = RegOpenKeyEx( HKEY_CURRENT_USER, szAudioRegPath, 0,
  370. KEY_READ, &hkeyRegPath );
  371. if (Status != NO_ERROR)
  372. return FALSE;
  373. //
  374. // Get the format tag string
  375. //
  376. Status = RegQueryValueEx( hkeyRegPath, szDefaultFormat, 0, NULL, NULL,
  377. &cbsz );
  378. if (Status != NO_ERROR)
  379. return FALSE;
  380. lpsz = (LPTSTR)GlobalAllocPtr(GHND, cbsz);
  381. if (!lpsz)
  382. return FALSE;
  383. Status = RegQueryValueEx( hkeyRegPath, szDefaultFormat, 0, NULL,
  384. (LPBYTE)lpsz, &cbsz );
  385. if (Status == NO_ERROR)
  386. {
  387. //
  388. // Get the format
  389. //
  390. Status = RegOpenKeyEx( HKEY_CURRENT_USER, szWaveFormats, 0,
  391. KEY_READ, &hkeyFmtPath );
  392. if (Status == NO_ERROR)
  393. {
  394. Status = RegQueryValueEx( hkeyFmtPath, lpsz, 0, NULL, NULL,
  395. &cbwfx );
  396. //
  397. // Make sure the structure is at minimum a WAVEFORMAT in size
  398. //
  399. if ((Status == NO_ERROR) && (cbwfx >= sizeof(WAVEFORMAT)))
  400. {
  401. lpwfx = (LPWAVEFORMATEX)GlobalAllocPtr(GHND, cbwfx);
  402. if (lpwfx)
  403. {
  404. Status = RegQueryValueEx( hkeyFmtPath, lpsz, 0, NULL,
  405. (LPBYTE)lpwfx, &cbwfx );
  406. }
  407. }
  408. RegCloseKey(hkeyFmtPath);
  409. }
  410. }
  411. RegCloseKey(hkeyRegPath);
  412. GlobalFreePtr(lpsz);
  413. //
  414. // Sanity Check.
  415. //
  416. if (lpwfx)
  417. {
  418. if (Status == NO_ERROR && IsValidWFX(lpwfx, cbwfx))
  419. {
  420. cbwfx = sizeof(WAVEFORMATEX)
  421. + ((lpwfx->wFormatTag == WAVE_FORMAT_PCM)?0:lpwfx->cbSize);
  422. *pcbwfx = cbwfx;
  423. *ppwfx = lpwfx;
  424. return TRUE;
  425. }
  426. else
  427. {
  428. GlobalFreePtr(lpwfx);
  429. return FALSE;
  430. }
  431. }
  432. return FALSE;
  433. }