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.

398 lines
8.5 KiB

  1. /**************************************************************************
  2. *
  3. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. * PURPOSE.
  7. *
  8. * Copyright (c) 1992 - 1995 Microsoft Corporation. All Rights Reserved.
  9. *
  10. **************************************************************************/
  11. /****************************************************************************
  12. *
  13. * profile.c: Stores profile info in the Registry
  14. *
  15. * Vidcap32 Source code
  16. *
  17. ***************************************************************************/
  18. /*
  19. * win32/win16 utility functions to read and write profile items
  20. * for multimedia tools
  21. */
  22. #include <windows.h>
  23. #include <windowsx.h>
  24. #ifdef _WIN32
  25. #define KEYNAME "Software\\Microsoft\\Multimedia Tools\\"
  26. #define ROOTKEY HKEY_CURRENT_USER
  27. #else
  28. #define INIFILE "mmtools.ini"
  29. #endif
  30. /*
  31. * read a BOOL flag from the profile, or return default if
  32. * not found.
  33. */
  34. BOOL
  35. mmGetProfileFlag(LPTSTR appname, LPTSTR valuename, BOOL bDefault)
  36. {
  37. #ifdef _WIN32
  38. TCHAR achName[MAX_PATH];
  39. HKEY hkey;
  40. DWORD dwType;
  41. BOOL bValue = bDefault;
  42. DWORD dwData;
  43. int cbData;
  44. lstrcpy(achName, KEYNAME);
  45. lstrcat(achName, appname);
  46. if (RegOpenKey(ROOTKEY, achName, &hkey) != ERROR_SUCCESS) {
  47. return(bDefault);
  48. }
  49. cbData = sizeof(dwData);
  50. if (RegQueryValueEx(
  51. hkey,
  52. valuename,
  53. NULL,
  54. &dwType,
  55. (PBYTE) &dwData,
  56. &cbData) == ERROR_SUCCESS) {
  57. if (dwType == REG_DWORD) {
  58. if (dwData) {
  59. bValue = TRUE;
  60. } else {
  61. bValue = FALSE;
  62. }
  63. }
  64. }
  65. RegCloseKey(hkey);
  66. return(bValue);
  67. #else
  68. TCHAR ach[10];
  69. GetPrivateProfileString(appname, valuename, "X", ach, sizeof(ach),
  70. INIFILE);
  71. switch(ach[0]) {
  72. case 'N':
  73. case 'n':
  74. case '0':
  75. return(FALSE);
  76. case 'Y':
  77. case 'y':
  78. case '1':
  79. return(TRUE);
  80. default:
  81. return(bDefault);
  82. }
  83. #endif
  84. }
  85. /*
  86. * write a boolean value to the registry, if it is not the
  87. * same as the default or the value already there
  88. */
  89. VOID
  90. mmWriteProfileFlag(LPTSTR appname, LPTSTR valuename, BOOL bValue, BOOL bDefault)
  91. {
  92. if (mmGetProfileFlag(appname, valuename, bDefault) == bValue) {
  93. return;
  94. }
  95. #ifdef _WIN32
  96. {
  97. TCHAR achName[MAX_PATH];
  98. HKEY hkey;
  99. lstrcpy(achName, KEYNAME);
  100. lstrcat(achName, appname);
  101. if (RegCreateKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  102. RegSetValueEx(
  103. hkey,
  104. valuename,
  105. 0,
  106. REG_DWORD,
  107. (PBYTE) &bValue,
  108. sizeof(bValue)
  109. );
  110. RegCloseKey(hkey);
  111. }
  112. }
  113. #else
  114. WritePrivateProfileString(
  115. appname,
  116. valuename,
  117. bValue ? "1" : "0",
  118. INIFILE);
  119. #endif
  120. }
  121. /*
  122. * read a UINT from the profile, or return default if
  123. * not found.
  124. */
  125. UINT
  126. mmGetProfileInt(LPTSTR appname, LPTSTR valuename, UINT uDefault)
  127. {
  128. #ifdef _WIN32
  129. TCHAR achName[MAX_PATH];
  130. HKEY hkey;
  131. DWORD dwType;
  132. UINT value = uDefault;
  133. DWORD dwData;
  134. int cbData;
  135. lstrcpy(achName, KEYNAME);
  136. lstrcat(achName, appname);
  137. if (RegOpenKey(ROOTKEY, achName, &hkey) != ERROR_SUCCESS) {
  138. return(uDefault);
  139. }
  140. cbData = sizeof(dwData);
  141. if (RegQueryValueEx(
  142. hkey,
  143. valuename,
  144. NULL,
  145. &dwType,
  146. (PBYTE) &dwData,
  147. &cbData) == ERROR_SUCCESS) {
  148. if (dwType == REG_DWORD) {
  149. value = (UINT)dwData;
  150. }
  151. }
  152. RegCloseKey(hkey);
  153. return(value);
  154. #else
  155. return(GetPrivateProfileInt(appname, valuename, uDefault, INIFILE);
  156. #endif
  157. }
  158. /*
  159. * write a UINT to the profile, if it is not the
  160. * same as the default or the value already there
  161. */
  162. VOID
  163. mmWriteProfileInt(LPTSTR appname, LPTSTR valuename, UINT uValue, UINT uDefault)
  164. {
  165. if (mmGetProfileInt(appname, valuename, uDefault) == uValue) {
  166. return;
  167. }
  168. #ifdef _WIN32
  169. {
  170. TCHAR achName[MAX_PATH];
  171. HKEY hkey;
  172. lstrcpy(achName, KEYNAME);
  173. lstrcat(achName, appname);
  174. if (RegCreateKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  175. RegSetValueEx(
  176. hkey,
  177. valuename,
  178. 0,
  179. REG_DWORD,
  180. (PBYTE) &uValue,
  181. sizeof(uValue)
  182. );
  183. RegCloseKey(hkey);
  184. }
  185. }
  186. #else
  187. TCHAR ach[12];
  188. wsprintf(ach, "%d", uValue);
  189. WritePrivateProfileString(
  190. appname,
  191. valuename,
  192. ach,
  193. INIFILE);
  194. #endif
  195. }
  196. /*
  197. * read a string from the profile into pResult.
  198. * result is number of bytes written into pResult
  199. */
  200. DWORD
  201. mmGetProfileString(
  202. LPTSTR appname,
  203. LPTSTR valuename,
  204. LPTSTR pDefault,
  205. LPTSTR pResult,
  206. int cbResult
  207. )
  208. {
  209. #ifdef _WIN32
  210. TCHAR achName[MAX_PATH];
  211. HKEY hkey;
  212. DWORD dwType;
  213. lstrcpy(achName, KEYNAME);
  214. lstrcat(achName, appname);
  215. if (RegOpenKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  216. if (RegQueryValueEx(
  217. hkey,
  218. valuename,
  219. NULL,
  220. &dwType,
  221. pResult,
  222. &cbResult) == ERROR_SUCCESS) {
  223. if (dwType == REG_SZ) {
  224. // cbResult is set to the size including null
  225. RegCloseKey(hkey);
  226. return(cbResult - 1);
  227. }
  228. }
  229. RegCloseKey(hkey);
  230. }
  231. // if we got here, we didn't find it, or it was the wrong type - return
  232. // the default string
  233. lstrcpy(pResult, pDefault);
  234. return(lstrlen(pDefault));
  235. #else
  236. return GetPrivateProfileString(
  237. appname,
  238. valuename,
  239. pDefault,
  240. pResult,
  241. cbResult
  242. INIFILE);
  243. #endif
  244. }
  245. /*
  246. * write a string to the profile
  247. */
  248. VOID
  249. mmWriteProfileString(LPTSTR appname, LPTSTR valuename, LPSTR pData)
  250. {
  251. #ifdef _WIN32
  252. TCHAR achName[MAX_PATH];
  253. HKEY hkey;
  254. lstrcpy(achName, KEYNAME);
  255. lstrcat(achName, appname);
  256. if (RegCreateKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  257. RegSetValueEx(
  258. hkey,
  259. valuename,
  260. 0,
  261. REG_SZ,
  262. pData,
  263. lstrlen(pData) + 1
  264. );
  265. RegCloseKey(hkey);
  266. }
  267. #else
  268. WritePrivateProfileString(
  269. appname,
  270. valuename,
  271. pData,
  272. INIFILE);
  273. #endif
  274. }
  275. /*
  276. * read binary values from the profile into pResult.
  277. * result is number of bytes written into pResult
  278. */
  279. DWORD
  280. mmGetProfileBinary(
  281. LPTSTR appname,
  282. LPTSTR valuename,
  283. LPVOID pDefault,
  284. LPVOID pResult, // if NULL, return the required size
  285. int cbSize
  286. )
  287. {
  288. TCHAR achName[MAX_PATH];
  289. HKEY hkey;
  290. DWORD dwType;
  291. int cbResult = cbSize;
  292. lstrcpy(achName, KEYNAME);
  293. lstrcat(achName, appname);
  294. if (RegOpenKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  295. if (RegQueryValueEx(
  296. hkey,
  297. valuename,
  298. NULL,
  299. &dwType,
  300. pResult,
  301. &cbResult) == ERROR_SUCCESS) {
  302. if (dwType == REG_BINARY) {
  303. // cbResult is the size
  304. RegCloseKey(hkey);
  305. return(cbResult);
  306. }
  307. }
  308. RegCloseKey(hkey);
  309. }
  310. // if we got here, we didn't find it, or it was the wrong type - return
  311. // the default values (use MoveMemory, since src could equal dst)
  312. MoveMemory (pResult, pDefault, cbSize);
  313. return cbSize;
  314. }
  315. /*
  316. * write binary data to the profile
  317. */
  318. VOID
  319. mmWriteProfileBinary(LPTSTR appname, LPTSTR valuename, LPVOID pData, int cbData)
  320. {
  321. TCHAR achName[MAX_PATH];
  322. HKEY hkey;
  323. lstrcpy(achName, KEYNAME);
  324. lstrcat(achName, appname);
  325. if (RegCreateKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  326. RegSetValueEx(
  327. hkey,
  328. valuename,
  329. 0,
  330. REG_BINARY,
  331. pData,
  332. cbData
  333. );
  334. RegCloseKey(hkey);
  335. }
  336. }