utils.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Services.exe - some utility functions
  3. *
  4. * Copyright 2007 Google (Mikolaj Zalewski)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #define WIN32_LEAN_AND_MEAN
  21. #include <stdarg.h>
  22. #include <windows.h>
  23. #include <winsvc.h>
  24. #include "wine/debug.h"
  25. #include "services.h"
  26. WINE_DEFAULT_DEBUG_CHANNEL(service);
  27. LPWSTR strdupW(LPCWSTR str)
  28. {
  29. int len;
  30. WCHAR *buf;
  31. if (str == NULL)
  32. return NULL;
  33. len = lstrlenW(str);
  34. buf = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(len+1));
  35. if (buf == NULL)
  36. return NULL;
  37. lstrcpyW(buf, str);
  38. return buf;
  39. }
  40. BOOL check_multisz(LPCWSTR lpMultiSz, DWORD cbSize)
  41. {
  42. if (cbSize == 0 || (cbSize == sizeof(WCHAR) && lpMultiSz[0] == 0))
  43. return TRUE;
  44. if ((cbSize % sizeof(WCHAR)) != 0 || cbSize < 2*sizeof(WCHAR))
  45. return FALSE;
  46. if (lpMultiSz[cbSize/2 - 1] || lpMultiSz[cbSize/2 - 2])
  47. return FALSE;
  48. return TRUE;
  49. }
  50. DWORD load_reg_string(HKEY hKey, LPCWSTR szValue, BOOL bExpand, LPWSTR *output)
  51. {
  52. DWORD size, type;
  53. LPWSTR buf = NULL;
  54. DWORD err;
  55. *output = NULL;
  56. if ((err = RegQueryValueExW(hKey, szValue, 0, &type, NULL, &size)) != 0)
  57. {
  58. if (err == ERROR_FILE_NOT_FOUND)
  59. return ERROR_SUCCESS;
  60. goto failed;
  61. }
  62. if (!(type == REG_SZ || (type == REG_EXPAND_SZ && bExpand)))
  63. {
  64. err = ERROR_INVALID_DATATYPE;
  65. goto failed;
  66. }
  67. buf = HeapAlloc(GetProcessHeap(), 0, size + sizeof(WCHAR));
  68. if ((err = RegQueryValueExW(hKey, szValue, 0, &type, (LPBYTE)buf, &size)) != 0)
  69. goto failed;
  70. buf[size/sizeof(WCHAR)] = 0;
  71. if (type == REG_EXPAND_SZ)
  72. {
  73. LPWSTR str;
  74. if (!(size = ExpandEnvironmentStringsW(buf, NULL, 0)))
  75. {
  76. err = GetLastError();
  77. goto failed;
  78. }
  79. str = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
  80. ExpandEnvironmentStringsW(buf, str, size);
  81. HeapFree(GetProcessHeap(), 0, buf);
  82. *output = str;
  83. }
  84. else
  85. *output = buf;
  86. return ERROR_SUCCESS;
  87. failed:
  88. WINE_ERR("Error %d while reading value %s\n", err, wine_dbgstr_w(szValue));
  89. HeapFree(GetProcessHeap(), 0, buf);
  90. return err;
  91. }
  92. DWORD load_reg_multisz(HKEY hKey, LPCWSTR szValue, BOOL bAllowSingle, LPWSTR *output)
  93. {
  94. DWORD size, type;
  95. LPWSTR buf = NULL;
  96. DWORD err;
  97. *output = NULL;
  98. if ((err = RegQueryValueExW(hKey, szValue, 0, &type, NULL, &size)) != 0)
  99. {
  100. if (err == ERROR_FILE_NOT_FOUND)
  101. {
  102. *output = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
  103. return ERROR_SUCCESS;
  104. }
  105. goto failed;
  106. }
  107. if (!((type == REG_MULTI_SZ) || ((type == REG_SZ) && bAllowSingle)))
  108. {
  109. err = ERROR_INVALID_DATATYPE;
  110. goto failed;
  111. }
  112. buf = HeapAlloc(GetProcessHeap(), 0, size + 2*sizeof(WCHAR));
  113. if ((err = RegQueryValueExW(hKey, szValue, 0, &type, (LPBYTE)buf, &size)) != 0)
  114. goto failed;
  115. buf[size/sizeof(WCHAR)] = 0;
  116. buf[size/sizeof(WCHAR) + 1] = 0;
  117. *output = buf;
  118. return ERROR_SUCCESS;
  119. failed:
  120. WINE_ERR("Error %d while reading value %s\n", err, wine_dbgstr_w(szValue));
  121. HeapFree(GetProcessHeap(), 0, buf);
  122. return err;
  123. }
  124. DWORD load_reg_dword(HKEY hKey, LPCWSTR szValue, DWORD *output)
  125. {
  126. DWORD size, type;
  127. DWORD err;
  128. *output = 0;
  129. size = sizeof(DWORD);
  130. if ((err = RegQueryValueExW(hKey, szValue, 0, &type, (LPBYTE)output, &size)) != 0)
  131. {
  132. if (err == ERROR_FILE_NOT_FOUND)
  133. return ERROR_SUCCESS;
  134. goto failed;
  135. }
  136. if ((type != REG_DWORD && type != REG_BINARY) || size != sizeof(DWORD))
  137. {
  138. err = ERROR_INVALID_DATATYPE;
  139. goto failed;
  140. }
  141. return ERROR_SUCCESS;
  142. failed:
  143. WINE_ERR("Error %d while reading value %s\n", err, wine_dbgstr_w(szValue));
  144. return err;
  145. }