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.

293 lines
5.9 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. registry.c
  5. Abstract:
  6. Functions for accessing registry information under:
  7. HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE
  8. Environment:
  9. Windows NT fax driver user interface
  10. Revision History:
  11. 01/29/96 -davidx-
  12. Created it.
  13. mm/dd/yy -author-
  14. description
  15. --*/
  16. #include "faxlib.h"
  17. #include "registry.h"
  18. LPTSTR
  19. GetRegistryExpandStr(
  20. HKEY hRootKey,
  21. LPTSTR pKeyName,
  22. LPTSTR pValueName
  23. )
  24. /*++
  25. Routine Description:
  26. Get a EXPAND_SZ value from the registry
  27. Arguments:
  28. hRootKey - Specifies a handle to the root registry key
  29. pKeyName - Specifies the name of the sub registry key
  30. pValueName - Specifies the name of the registry value
  31. Return Value:
  32. Pointer to an expanded string, NULL if there is an error
  33. --*/
  34. {
  35. DWORD size, type;
  36. LONG status;
  37. HKEY hRegKey;
  38. LPTSTR pRegStr = NULL, pExpandedStr;
  39. //
  40. // Get a handle to the user info registry key
  41. //
  42. if (! (hRegKey = OpenRegistryKey(hRootKey, pKeyName, FALSE, REG_READONLY)))
  43. return NULL;
  44. //
  45. // Figure out how much memory to allocate
  46. //
  47. size = 0;
  48. status = RegQueryValueEx(hRegKey, pValueName, NULL, &type, NULL, &size);
  49. if ((status == ERROR_SUCCESS) &&
  50. (type == REG_EXPAND_SZ || type == REG_SZ) &&
  51. (pRegStr = MemAlloc(size)))
  52. {
  53. //
  54. // Read the registry value
  55. //
  56. status = RegQueryValueEx(hRegKey, pValueName, NULL, NULL, (PBYTE) pRegStr, &size);
  57. if (status != ERROR_SUCCESS || IsEmptyString(pRegStr)) {
  58. MemFree(pRegStr);
  59. pRegStr = NULL;
  60. } else if (type == REG_EXPAND_SZ) {
  61. //
  62. // Substitute any environment variables
  63. //
  64. if ((size = ExpandEnvironmentStrings(pRegStr, NULL, 0)) == 0 ||
  65. (pExpandedStr = MemAlloc(sizeof(TCHAR) * size)) == NULL)
  66. {
  67. MemFree(pRegStr);
  68. pRegStr = NULL;
  69. } else {
  70. if (ExpandEnvironmentStrings(pRegStr, pExpandedStr, size) == 0)
  71. *pExpandedStr = NUL;
  72. MemFree(pRegStr);
  73. pRegStr = pExpandedStr;
  74. }
  75. }
  76. }
  77. RegCloseKey(hRegKey);
  78. return pRegStr;
  79. }
  80. PDEVMODE
  81. GetPerUserDevmode(
  82. LPTSTR pPrinterName
  83. )
  84. /*++
  85. Routine Description:
  86. Get per-user devmode information for the specified printer
  87. Arguments:
  88. pPrinterName - Specifies the name of the printer we're interested in
  89. Return Value:
  90. Pointer to per-user devmode information read from the registry
  91. --*/
  92. {
  93. PVOID pDevmode = NULL;
  94. HANDLE hPrinter;
  95. PPRINTER_INFO_9 pPrinterInfo;
  96. TCHAR PrinterBuffer[64];
  97. //
  98. // Make sure the printer name is valid
  99. //
  100. if (pPrinterName == NULL) {
  101. _tcscpy(PrinterBuffer,L"Fax");
  102. } else {
  103. _tcscpy(PrinterBuffer,pPrinterName);
  104. }
  105. //
  106. // Open the printer
  107. //
  108. if (!OpenPrinter(PrinterBuffer,&hPrinter,NULL) ) {
  109. return NULL;
  110. }
  111. pPrinterInfo = MyGetPrinter(hPrinter,9);
  112. if (!pPrinterInfo || !pPrinterInfo->pDevMode) {
  113. ClosePrinter(hPrinter);
  114. return NULL;
  115. }
  116. pDevmode = MemAlloc(sizeof(DRVDEVMODE) );
  117. if (!pDevmode) {
  118. MemFree(pPrinterInfo);
  119. ClosePrinter(hPrinter);
  120. return NULL;
  121. }
  122. CopyMemory((PVOID) pDevmode,
  123. (PVOID) pPrinterInfo->pDevMode,
  124. sizeof(DRVDEVMODE) );
  125. MemFree( pPrinterInfo );
  126. ClosePrinter( hPrinter );
  127. return pDevmode;
  128. }
  129. VOID
  130. SavePerUserDevmode(
  131. LPTSTR pPrinterName,
  132. PDEVMODE pDevmode
  133. )
  134. /*++
  135. Routine Description:
  136. Save per-user devmode information for the specified printer
  137. Arguments:
  138. pPrinterName - Specifies the name of the printer we're interested in
  139. pDevmode - Points to the devmode to be saved
  140. Return Value:
  141. NONE
  142. --*/
  143. {
  144. HKEY hRegKey;
  145. INT size;
  146. //
  147. // Make sure the printer name is valid
  148. //
  149. if (pPrinterName == NULL) {
  150. Error(("Bad printer name\n"));
  151. return;
  152. }
  153. //
  154. // Open the registry key for write access
  155. //
  156. if (! (hRegKey = GetUserInfoRegKey(REGKEY_FAX_DEVMODE, REG_READWRITE)))
  157. return;
  158. //
  159. // Save the devmode information as binary data
  160. //
  161. size = pDevmode->dmSize + pDevmode->dmDriverExtra;
  162. RegSetValueEx(hRegKey, pPrinterName, 0, REG_BINARY, (PBYTE) pDevmode, size);
  163. RegCloseKey(hRegKey);
  164. }
  165. LPTSTR
  166. GetUserCoverPageDir(
  167. VOID
  168. )
  169. {
  170. LPTSTR CpDirTmp = NULL;
  171. LPTSTR CpDir = MemAlloc(MAX_PATH*sizeof(TCHAR));
  172. SHGetSpecialFolderPath(NULL, CpDir , CSIDL_PERSONAL, FALSE);
  173. CpDirTmp = GetRegistryExpandStr(HKEY_CURRENT_USER, REGKEY_FAX_SETUP, REGVAL_CP_LOCATION);
  174. if (CpDirTmp == NULL) {
  175. HKEY hKey = OpenRegistryKey(HKEY_CURRENT_USER,REGKEY_FAX_SETUP,FALSE, REG_READONLY);
  176. if (hKey) {
  177. SetRegistryStringExpand(hKey,REGVAL_CP_LOCATION,DEFAULT_COVERPAGE_DIR);
  178. RegCloseKey(hKey);
  179. CpDirTmp = GetRegistryExpandStr(HKEY_CURRENT_USER, REGKEY_FAX_SETUP, REGVAL_CP_LOCATION);
  180. }
  181. }
  182. if (CpDir && CpDirTmp) {
  183. _tcscat( CpDir, TEXT("\\") );
  184. _tcscat( CpDir, CpDirTmp );
  185. }
  186. if (CpDirTmp) {
  187. MemFree(CpDirTmp);
  188. }
  189. return CpDir;
  190. }
  191. LPTSTR
  192. GetCoverPageEditor(
  193. VOID
  194. )
  195. {
  196. LPTSTR CpEd = GetRegistryStringExpand(HKEY_CURRENT_USER, REGKEY_FAX_SETUP, REGVAL_CP_EDITOR);
  197. if (CpEd == NULL) {
  198. HKEY hKey = OpenRegistryKey(HKEY_CURRENT_USER,REGKEY_FAX_SETUP,FALSE, REG_READWRITE);
  199. if (hKey) {
  200. SetRegistryStringExpand(hKey,REGVAL_CP_EDITOR,DEFAULT_COVERPAGE_EDITOR);
  201. RegCloseKey(hKey);
  202. return GetRegistryExpandStr(HKEY_CURRENT_USER, REGKEY_FAX_SETUP, REGVAL_CP_EDITOR);
  203. }
  204. }
  205. return CpEd;
  206. }