Windows NT 4.0 source code leak
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.

308 lines
5.9 KiB

4 years ago
  1. /*
  2. * profile.c
  3. *
  4. * win32/win16 utility functions to read and write profile items
  5. * for multimedia tools
  6. */
  7. #include <windows.h>
  8. #include <windowsx.h>
  9. #ifdef _WIN32
  10. #define KEYNAME "Software\\Microsoft\\Multimedia Tools\\"
  11. #define ROOTKEY HKEY_CURRENT_USER
  12. #else
  13. #define INIFILE "mmtools.ini"
  14. #endif
  15. /*
  16. * read a BOOL flag from the profile, or return default if
  17. * not found.
  18. */
  19. BOOL
  20. mmGetProfileFlag(LPSTR appname, LPSTR valuename, BOOL bDefault)
  21. {
  22. #ifdef _WIN32
  23. char achName[MAX_PATH];
  24. HKEY hkey;
  25. DWORD dwType;
  26. BOOL bValue = bDefault;
  27. DWORD dwData;
  28. int cbData;
  29. lstrcpy(achName, KEYNAME);
  30. lstrcat(achName, appname);
  31. if (RegOpenKey(ROOTKEY, achName, &hkey) != ERROR_SUCCESS) {
  32. return(bDefault);
  33. }
  34. cbData = sizeof(dwData);
  35. if (RegQueryValueEx(
  36. hkey,
  37. valuename,
  38. NULL,
  39. &dwType,
  40. (PBYTE) &dwData,
  41. &cbData) == ERROR_SUCCESS) {
  42. if (dwType == REG_DWORD) {
  43. if (dwData) {
  44. bValue = TRUE;
  45. } else {
  46. bValue = FALSE;
  47. }
  48. }
  49. }
  50. RegCloseKey(hkey);
  51. return(bValue);
  52. #else
  53. char ach[10];
  54. GetPrivateProfileString(appname, valuename, "X", ach, sizeof(ach),
  55. INIFILE);
  56. switch(ach[0]) {
  57. case 'N':
  58. case 'n':
  59. case '0':
  60. return(FALSE);
  61. case 'Y':
  62. case 'y':
  63. case '1':
  64. return(TRUE);
  65. default:
  66. return(bDefault);
  67. }
  68. #endif
  69. }
  70. /*
  71. * write a boolean value to the registry, if it is not the
  72. * same as the default or the value already there
  73. */
  74. VOID
  75. mmWriteProfileFlag(LPSTR appname, LPSTR valuename, BOOL bValue, BOOL bDefault)
  76. {
  77. if (mmGetProfileFlag(appname, valuename, bDefault) == bValue) {
  78. return;
  79. }
  80. #ifdef _WIN32
  81. {
  82. char achName[MAX_PATH];
  83. HKEY hkey;
  84. lstrcpy(achName, KEYNAME);
  85. lstrcat(achName, appname);
  86. if (RegCreateKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  87. RegSetValueEx(
  88. hkey,
  89. valuename,
  90. 0,
  91. REG_DWORD,
  92. (PBYTE) &bValue,
  93. sizeof(bValue)
  94. );
  95. RegCloseKey(hkey);
  96. }
  97. }
  98. #else
  99. WritePrivateProfileString(
  100. appname,
  101. valuename,
  102. bValue ? "1" : "0",
  103. INIFILE);
  104. #endif
  105. }
  106. /*
  107. * read a UINT from the profile, or return default if
  108. * not found.
  109. */
  110. UINT
  111. mmGetProfileInt(LPSTR appname, LPSTR valuename, UINT uDefault)
  112. {
  113. #ifdef _WIN32
  114. char achName[MAX_PATH];
  115. HKEY hkey;
  116. DWORD dwType;
  117. UINT value = uDefault;
  118. DWORD dwData;
  119. int cbData;
  120. lstrcpy(achName, KEYNAME);
  121. lstrcat(achName, appname);
  122. if (RegOpenKey(ROOTKEY, achName, &hkey) != ERROR_SUCCESS) {
  123. return(uDefault);
  124. }
  125. cbData = sizeof(dwData);
  126. if (RegQueryValueEx(
  127. hkey,
  128. valuename,
  129. NULL,
  130. &dwType,
  131. (PBYTE) &dwData,
  132. &cbData) == ERROR_SUCCESS) {
  133. if (dwType == REG_DWORD) {
  134. value = (UINT)dwData;
  135. }
  136. }
  137. RegCloseKey(hkey);
  138. return(value);
  139. #else
  140. return(GetPrivateProfileInt(appname, valuename, uDefault, INIFILE);
  141. #endif
  142. }
  143. /*
  144. * write a UINT to the profile, if it is not the
  145. * same as the default or the value already there
  146. */
  147. VOID
  148. mmWriteProfileInt(LPSTR appname, LPSTR valuename, UINT uValue, UINT uDefault)
  149. {
  150. if (mmGetProfileInt(appname, valuename, uDefault) == uValue) {
  151. return;
  152. }
  153. #ifdef _WIN32
  154. {
  155. char achName[MAX_PATH];
  156. HKEY hkey;
  157. lstrcpy(achName, KEYNAME);
  158. lstrcat(achName, appname);
  159. if (RegCreateKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  160. RegSetValueEx(
  161. hkey,
  162. valuename,
  163. 0,
  164. REG_DWORD,
  165. (PBYTE) &uValue,
  166. sizeof(uValue)
  167. );
  168. RegCloseKey(hkey);
  169. }
  170. }
  171. #else
  172. char ach[12];
  173. wsprintf(ach, "%d", uValue);
  174. WritePrivateProfileString(
  175. appname,
  176. valuename,
  177. ach,
  178. INIFILE);
  179. #endif
  180. }
  181. /*
  182. * read a string from the profile into pResult.
  183. * result is number of bytes written into pResult
  184. */
  185. DWORD
  186. mmGetProfileString(
  187. LPSTR appname,
  188. LPSTR valuename,
  189. LPSTR pDefault,
  190. LPSTR pResult,
  191. int cbResult
  192. )
  193. {
  194. #ifdef _WIN32
  195. char achName[MAX_PATH];
  196. HKEY hkey;
  197. DWORD dwType;
  198. lstrcpy(achName, KEYNAME);
  199. lstrcat(achName, appname);
  200. if (RegOpenKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  201. if (RegQueryValueEx(
  202. hkey,
  203. valuename,
  204. NULL,
  205. &dwType,
  206. pResult,
  207. &cbResult) == ERROR_SUCCESS) {
  208. if (dwType == REG_SZ) {
  209. // cbResult is set to the size including null
  210. RegCloseKey(hkey);
  211. return(cbResult - 1);
  212. }
  213. }
  214. RegCloseKey(hkey);
  215. }
  216. // if we got here, we didn't find it, or it was the wrong type - return
  217. // the default string
  218. lstrcpy(pResult, pDefault);
  219. return(lstrlen(pDefault));
  220. #else
  221. return GetPrivateProfileString(
  222. appname,
  223. valuename,
  224. pDefault,
  225. pResult,
  226. cbResult
  227. INIFILE);
  228. #endif
  229. }
  230. /*
  231. * write a string to the profile
  232. */
  233. VOID
  234. mmWriteProfileString(LPSTR appname, LPSTR valuename, LPSTR pData)
  235. {
  236. #ifdef _WIN32
  237. char achName[MAX_PATH];
  238. HKEY hkey;
  239. lstrcpy(achName, KEYNAME);
  240. lstrcat(achName, appname);
  241. if (RegCreateKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  242. RegSetValueEx(
  243. hkey,
  244. valuename,
  245. 0,
  246. REG_SZ,
  247. pData,
  248. lstrlen(pData) + 1
  249. );
  250. RegCloseKey(hkey);
  251. }
  252. #else
  253. WritePrivateProfileString(
  254. appname,
  255. valuename,
  256. pData,
  257. INIFILE);
  258. #endif
  259. }