dwreg.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright (C) 2001, Ghostgum Software Pty Ltd. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: dwreg.c,v 1.1 2001/08/02 08:36:07 ghostgum Exp $ */
  16. /* MS Windows registry values */
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #include <stdlib.h> /* for getenv */
  20. #include <string.h>
  21. #include "gscdefs.h" /* for gs_productfamily and gs_revision */
  22. /* We store registry named values under the key
  23. * "Software\\AFPL Ghostscript"
  24. * where "AFPL Ghostscript" is actually gs_productfamily.
  25. * Either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER will be used.
  26. */
  27. int
  28. win_registry_key(char *buf, int len)
  29. {
  30. const char *software = "Software";
  31. if (strlen(software) + 1 + strlen(gs_productfamily) >= len)
  32. return -1;
  33. strcpy(buf, software);
  34. strcat(buf, "\\");
  35. strcat(buf, gs_productfamily);
  36. return 0;
  37. }
  38. /*
  39. * Get a named registry value from HKCU.
  40. * name, ptr, plen and return values are the same as in gp_getenv();
  41. */
  42. int
  43. win_get_reg_value(const char *name, char *ptr, int *plen)
  44. {
  45. HKEY hkey;
  46. DWORD cbData, keytype;
  47. BYTE b;
  48. LONG rc;
  49. BYTE *bptr = (BYTE *)ptr;
  50. char key[256];
  51. win_registry_key(key, sizeof(key));
  52. if (RegOpenKeyEx(HKEY_CURRENT_USER, key, 0, KEY_READ, &hkey)
  53. == ERROR_SUCCESS) {
  54. keytype = REG_SZ;
  55. cbData = *plen;
  56. if (bptr == (char *)NULL)
  57. bptr = &b; /* Registry API won't return ERROR_MORE_DATA */
  58. /* if ptr is NULL */
  59. rc = RegQueryValueEx(hkey, (char *)name, 0, &keytype, bptr, &cbData);
  60. RegCloseKey(hkey);
  61. if (rc == ERROR_SUCCESS) {
  62. *plen = cbData;
  63. return 0; /* found environment variable and copied it */
  64. } else if (rc == ERROR_MORE_DATA) {
  65. /* buffer wasn't large enough */
  66. *plen = cbData;
  67. return -1;
  68. }
  69. }
  70. return 1; /* not found */
  71. }
  72. /*
  73. * Set a named registry value under HKCU.
  74. * name = name of named value
  75. * str = value of named value
  76. * Returns 0 on success.
  77. */
  78. int
  79. win_set_reg_value(const char *name, const char *value)
  80. {
  81. HKEY hkey;
  82. LONG rc;
  83. char key[256];
  84. DWORD dwDisposition;
  85. win_registry_key(key, sizeof(key));
  86. rc = RegOpenKeyEx(HKEY_CURRENT_USER, key, 0, KEY_WRITE, &hkey);
  87. if (rc != ERROR_SUCCESS)
  88. rc = RegCreateKeyEx(HKEY_CURRENT_USER, key, 0, "", 0,
  89. KEY_ALL_ACCESS, NULL, &hkey, &dwDisposition);
  90. if (rc == ERROR_SUCCESS) {
  91. rc = RegSetValueEx(hkey, name, 0, REG_SZ,
  92. (CONST BYTE *)value, strlen(value)+1);
  93. RegCloseKey(hkey);
  94. }
  95. return rc == ERROR_SUCCESS ? 0 : -1;
  96. }