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.

256 lines
5.4 KiB

  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. #include <profile.key>
  10. /*
  11. * read a UINT from the profile, or return default if
  12. * not found.
  13. */
  14. #ifdef _WIN32
  15. UINT mmGetProfileIntA(LPCSTR appname, LPCSTR valuename, INT uDefault)
  16. {
  17. CHAR achName[MAX_PATH];
  18. HKEY hkey;
  19. DWORD dwType;
  20. INT value = uDefault;
  21. DWORD dwData;
  22. int cbData;
  23. lstrcpyA(achName, KEYNAMEA);
  24. lstrcatA(achName, appname);
  25. if (RegOpenKeyA(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  26. cbData = sizeof(dwData);
  27. if (RegQueryValueExA(
  28. hkey,
  29. (LPSTR)valuename,
  30. NULL,
  31. &dwType,
  32. (PBYTE) &dwData,
  33. &cbData) == ERROR_SUCCESS) {
  34. if (dwType == REG_DWORD) {
  35. value = (INT)dwData;
  36. }
  37. }
  38. RegCloseKey(hkey);
  39. }
  40. return((UINT)value);
  41. }
  42. UINT
  43. mmGetProfileInt(LPCTSTR appname, LPCTSTR valuename, INT uDefault)
  44. {
  45. TCHAR achName[MAX_PATH];
  46. HKEY hkey;
  47. DWORD dwType;
  48. INT value = uDefault;
  49. DWORD dwData;
  50. int cbData;
  51. lstrcpy(achName, KEYNAME);
  52. lstrcat(achName, appname);
  53. if (RegOpenKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  54. cbData = sizeof(dwData);
  55. if (RegQueryValueEx(
  56. hkey,
  57. (LPTSTR)valuename,
  58. NULL,
  59. &dwType,
  60. (PBYTE) &dwData,
  61. &cbData) == ERROR_SUCCESS) {
  62. if (dwType == REG_DWORD) {
  63. value = (INT)dwData;
  64. }
  65. }
  66. RegCloseKey(hkey);
  67. }
  68. return((UINT)value);
  69. }
  70. #endif
  71. /*
  72. * write a UINT to the profile, if it is not the
  73. * same as the default or the value already there
  74. */
  75. #ifdef _WIN32
  76. VOID
  77. mmWriteProfileInt(LPCTSTR appname, LPCTSTR valuename, INT Value)
  78. {
  79. // If we would write the same as already there... return.
  80. if (mmGetProfileInt(appname, valuename, !Value) == (UINT)Value) {
  81. return;
  82. }
  83. {
  84. TCHAR achName[MAX_PATH];
  85. HKEY hkey;
  86. lstrcpy(achName, KEYNAME);
  87. lstrcat(achName, appname);
  88. if (RegCreateKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  89. RegSetValueEx(
  90. hkey,
  91. valuename,
  92. 0,
  93. REG_DWORD,
  94. (PBYTE) &Value,
  95. sizeof(Value)
  96. );
  97. RegCloseKey(hkey);
  98. }
  99. }
  100. }
  101. #else
  102. // For Win16 we use a macro and assume we have been passed a string value
  103. // char ach[12];
  104. //
  105. // wsprintf(ach, "%d", Value);
  106. //
  107. // WriteProfileString(
  108. // appname,
  109. // valuename,
  110. // ach);
  111. }
  112. #endif
  113. /*
  114. * read a string from the profile into pResult.
  115. * result is number of bytes written into pResult
  116. */
  117. #ifdef _WIN32
  118. DWORD
  119. mmGetProfileStringA(
  120. LPCSTR appname,
  121. LPCSTR valuename,
  122. LPCSTR pDefault,
  123. LPSTR pResult,
  124. int cbResult
  125. )
  126. {
  127. CHAR achName[MAX_PATH];
  128. HKEY hkey;
  129. DWORD dwType;
  130. lstrcpyA(achName, KEYNAMEA);
  131. lstrcatA(achName, appname);
  132. if (RegOpenKeyA(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  133. cbResult = cbResult * sizeof(TCHAR);
  134. if (RegQueryValueExA(
  135. hkey,
  136. (LPSTR)valuename,
  137. NULL,
  138. &dwType,
  139. (LPBYTE)pResult,
  140. &cbResult) == ERROR_SUCCESS) {
  141. if (dwType == REG_SZ) {
  142. // cbResult is set to the size including null
  143. RegCloseKey(hkey);
  144. return(cbResult - 1);
  145. }
  146. }
  147. RegCloseKey(hkey);
  148. }
  149. // if we got here, we didn't find it, or it was the wrong type - return
  150. // the default string
  151. lstrcpyA(pResult, pDefault);
  152. return(lstrlenA(pDefault));
  153. }
  154. DWORD
  155. mmGetProfileString(
  156. LPCTSTR appname,
  157. LPCTSTR valuename,
  158. LPCTSTR pDefault,
  159. LPTSTR pResult,
  160. int cbResult
  161. )
  162. {
  163. TCHAR achName[MAX_PATH];
  164. HKEY hkey;
  165. DWORD dwType;
  166. lstrcpy(achName, KEYNAME);
  167. lstrcat(achName, appname);
  168. if (RegOpenKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  169. cbResult = cbResult * sizeof(TCHAR);
  170. if (RegQueryValueEx(
  171. hkey,
  172. (LPTSTR)valuename,
  173. NULL,
  174. &dwType,
  175. (LPBYTE)pResult,
  176. &cbResult) == ERROR_SUCCESS) {
  177. if (dwType == REG_SZ) {
  178. // cbResult is set to the size including null
  179. RegCloseKey(hkey);
  180. return(cbResult/sizeof(TCHAR) - 1);
  181. }
  182. }
  183. RegCloseKey(hkey);
  184. }
  185. // if we got here, we didn't find it, or it was the wrong type - return
  186. // the default string
  187. lstrcpy(pResult, pDefault);
  188. return(lstrlen(pDefault));
  189. }
  190. #endif
  191. /*
  192. * write a string to the profile
  193. */
  194. #ifdef _WIN32
  195. VOID
  196. mmWriteProfileString(LPCTSTR appname, LPCTSTR valuename, LPCTSTR pData)
  197. {
  198. TCHAR achName[MAX_PATH];
  199. HKEY hkey;
  200. lstrcpy(achName, KEYNAME);
  201. lstrcat(achName, appname);
  202. if (RegCreateKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  203. if (pData) {
  204. RegSetValueEx(
  205. hkey,
  206. valuename,
  207. 0,
  208. REG_SZ,
  209. (LPBYTE)pData,
  210. (lstrlen(pData) + 1) * sizeof(TCHAR)
  211. );
  212. } else {
  213. RegDeleteValue(
  214. hkey,
  215. (LPTSTR)valuename
  216. );
  217. }
  218. RegCloseKey(hkey);
  219. }
  220. }
  221. #endif