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.

488 lines
11 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. util.c
  5. Abstract:
  6. Shared utility routines
  7. Author:
  8. Jin Huang (jinhuang) 14-Jul-1997
  9. Revision History:
  10. --*/
  11. #include "util.h"
  12. #pragma hdrstop
  13. DWORD
  14. SmbsvcpRegQueryIntValue(
  15. IN HKEY hKeyRoot,
  16. IN PWSTR SubKey,
  17. IN PWSTR ValueName,
  18. OUT DWORD *Value
  19. )
  20. /* ++
  21. Routine Description:
  22. This routine queries a REG_DWORD value from a value name/subkey.
  23. Arguments:
  24. hKeyRoot - root
  25. SubKey - key path
  26. ValueName - name of the value
  27. Value - the output value for the ValueName
  28. Return values:
  29. Win32 error code
  30. -- */
  31. {
  32. DWORD Rcode;
  33. DWORD RegType;
  34. DWORD dSize=0;
  35. HKEY hKey=NULL;
  36. if(( Rcode = RegOpenKeyEx(hKeyRoot,
  37. SubKey,
  38. 0,
  39. KEY_READ,
  40. &hKey
  41. )) == ERROR_SUCCESS ) {
  42. if(( Rcode = RegQueryValueEx(hKey,
  43. ValueName,
  44. 0,
  45. &RegType,
  46. NULL,
  47. &dSize
  48. )) == ERROR_SUCCESS ) {
  49. switch (RegType) {
  50. case REG_DWORD:
  51. case REG_DWORD_BIG_ENDIAN:
  52. Rcode = RegQueryValueEx(hKey,
  53. ValueName,
  54. 0,
  55. &RegType,
  56. (BYTE *)Value,
  57. &dSize
  58. );
  59. if ( Rcode != ERROR_SUCCESS ) {
  60. if ( Value != NULL )
  61. *Value = 0;
  62. }
  63. break;
  64. default:
  65. Rcode = ERROR_INVALID_DATATYPE;
  66. break;
  67. }
  68. }
  69. }
  70. if( hKey )
  71. RegCloseKey( hKey );
  72. return(Rcode);
  73. }
  74. DWORD
  75. SmbsvcpRegSetIntValue(
  76. IN HKEY hKeyRoot,
  77. IN PWSTR SubKey,
  78. IN PWSTR ValueName,
  79. IN DWORD Value
  80. )
  81. /* ++
  82. Routine Description:
  83. This routine sets a REG_DWORD value to a value name/subkey.
  84. Arguments:
  85. hKeyRoot - root
  86. SubKey - key path
  87. ValueName - name of the value
  88. Value - the value to set
  89. Return values:
  90. Win32 error code
  91. -- */
  92. {
  93. DWORD Rcode;
  94. HKEY hKey=NULL;
  95. if(( Rcode = RegOpenKeyEx(hKeyRoot,
  96. SubKey,
  97. 0,
  98. KEY_SET_VALUE,
  99. &hKey
  100. )) == ERROR_SUCCESS ) {
  101. Rcode = RegSetValueEx( hKey,
  102. ValueName,
  103. 0,
  104. REG_DWORD,
  105. (BYTE *)&Value,
  106. 4
  107. );
  108. }
  109. if( hKey )
  110. RegCloseKey( hKey );
  111. return(Rcode);
  112. }
  113. DWORD
  114. SmbsvcpRegSetValue(
  115. IN HKEY hKeyRoot,
  116. IN PWSTR SubKey,
  117. IN PWSTR ValueName,
  118. IN DWORD RegType,
  119. IN BYTE *Value,
  120. IN DWORD ValueLen
  121. )
  122. /* ++
  123. Routine Description:
  124. This routine sets a string value to a value name/subkey.
  125. Arguments:
  126. hKeyRoot - root
  127. SubKey - key path
  128. ValueName - name of the value
  129. Value - the value to set
  130. ValueLen - The number of bytes in Value
  131. Return values:
  132. Win32 error code
  133. -- */
  134. {
  135. DWORD Rcode;
  136. DWORD NewKey;
  137. HKEY hKey=NULL;
  138. SECURITY_ATTRIBUTES SecurityAttributes;
  139. PSECURITY_DESCRIPTOR SecurityDescriptor=NULL;
  140. if(( Rcode = RegOpenKeyEx(hKeyRoot,
  141. SubKey,
  142. 0,
  143. KEY_SET_VALUE,
  144. &hKey
  145. )) != ERROR_SUCCESS ) {
  146. SecurityAttributes.nLength = sizeof( SECURITY_ATTRIBUTES );
  147. SecurityAttributes.lpSecurityDescriptor = SecurityDescriptor;
  148. SecurityAttributes.bInheritHandle = FALSE;
  149. Rcode = RegCreateKeyEx(
  150. hKeyRoot,
  151. SubKey,
  152. 0,
  153. NULL, // LPTSTR lpClass,
  154. 0,
  155. KEY_SET_VALUE,
  156. NULL, // &SecurityAttributes,
  157. &hKey,
  158. &NewKey
  159. );
  160. }
  161. if ( Rcode == ERROR_SUCCESS ) {
  162. Rcode = RegSetValueEx( hKey,
  163. ValueName,
  164. 0,
  165. RegType,
  166. Value,
  167. ValueLen
  168. );
  169. }
  170. if( hKey )
  171. RegCloseKey( hKey );
  172. return(Rcode);
  173. }
  174. DWORD
  175. SmbsvcpRegQueryValue(
  176. IN HKEY hKeyRoot,
  177. IN PWSTR SubKey,
  178. IN PCWSTR ValueName,
  179. OUT PVOID *Value,
  180. OUT LPDWORD pRegType
  181. )
  182. /* ++
  183. Routine Description:
  184. This routine queries a REG_SZ value from a value name/subkey.
  185. The output buffer is allocated if it is NULL. It must be freed
  186. by LocalFree
  187. Arguments:
  188. hKeyRoot - root
  189. SubKey - key path
  190. ValueName - name of the value
  191. Value - the output string for the ValueName
  192. Return values:
  193. Win32 error code
  194. -- */
  195. {
  196. DWORD Rcode;
  197. DWORD dSize=0;
  198. HKEY hKey=NULL;
  199. BOOL FreeMem=FALSE;
  200. if ( SubKey == NULL || ValueName == NULL ||
  201. Value == NULL || pRegType == NULL ) {
  202. return(SCESTATUS_INVALID_PARAMETER);
  203. }
  204. if(( Rcode = RegOpenKeyEx(hKeyRoot,
  205. SubKey,
  206. 0,
  207. KEY_READ,
  208. &hKey
  209. )) == ERROR_SUCCESS ) {
  210. if(( Rcode = RegQueryValueEx(hKey,
  211. ValueName,
  212. 0,
  213. pRegType,
  214. NULL,
  215. &dSize
  216. )) == ERROR_SUCCESS ) {
  217. switch (*pRegType) {
  218. case REG_DWORD:
  219. case REG_DWORD_BIG_ENDIAN:
  220. Rcode = RegQueryValueEx(hKey,
  221. ValueName,
  222. 0,
  223. pRegType,
  224. (BYTE *)(*Value),
  225. &dSize
  226. );
  227. if ( Rcode != ERROR_SUCCESS ) {
  228. if ( *Value != NULL )
  229. *((BYTE *)(*Value)) = 0;
  230. }
  231. break;
  232. case REG_SZ:
  233. case REG_EXPAND_SZ:
  234. case REG_MULTI_SZ:
  235. if ( *Value == NULL ) {
  236. *Value = (PVOID)LocalAlloc( LMEM_ZEROINIT, (dSize+1)*sizeof(TCHAR));
  237. FreeMem = TRUE;
  238. }
  239. if ( *Value == NULL ) {
  240. Rcode = ERROR_NOT_ENOUGH_MEMORY;
  241. } else {
  242. Rcode = RegQueryValueEx(hKey,
  243. ValueName,
  244. 0,
  245. pRegType,
  246. (BYTE *)(*Value),
  247. &dSize
  248. );
  249. if ( Rcode != ERROR_SUCCESS && FreeMem ) {
  250. LocalFree(*Value);
  251. *Value = NULL;
  252. }
  253. }
  254. break;
  255. default:
  256. Rcode = ERROR_INVALID_DATATYPE;
  257. break;
  258. }
  259. }
  260. }
  261. if( hKey )
  262. RegCloseKey( hKey );
  263. return(Rcode);
  264. }
  265. DWORD
  266. SmbsvcpSceStatusToDosError(
  267. IN SCESTATUS SceStatus
  268. )
  269. // converts SCESTATUS error code to dos error defined in winerror.h
  270. {
  271. switch(SceStatus) {
  272. case SCESTATUS_SUCCESS:
  273. return(NO_ERROR);
  274. case SCESTATUS_OTHER_ERROR:
  275. return(ERROR_EXTENDED_ERROR);
  276. case SCESTATUS_INVALID_PARAMETER:
  277. return(ERROR_INVALID_PARAMETER);
  278. case SCESTATUS_RECORD_NOT_FOUND:
  279. return(ERROR_NO_MORE_ITEMS);
  280. case SCESTATUS_INVALID_DATA:
  281. return(ERROR_INVALID_DATA);
  282. case SCESTATUS_OBJECT_EXIST:
  283. return(ERROR_FILE_EXISTS);
  284. case SCESTATUS_BUFFER_TOO_SMALL:
  285. return(ERROR_INSUFFICIENT_BUFFER);
  286. case SCESTATUS_PROFILE_NOT_FOUND:
  287. return(ERROR_FILE_NOT_FOUND);
  288. case SCESTATUS_BAD_FORMAT:
  289. return(ERROR_BAD_FORMAT);
  290. case SCESTATUS_NOT_ENOUGH_RESOURCE:
  291. return(ERROR_NOT_ENOUGH_MEMORY);
  292. case SCESTATUS_ACCESS_DENIED:
  293. return(ERROR_ACCESS_DENIED);
  294. case SCESTATUS_CANT_DELETE:
  295. return(ERROR_CURRENT_DIRECTORY);
  296. case SCESTATUS_PREFIX_OVERFLOW:
  297. return(ERROR_BUFFER_OVERFLOW);
  298. case SCESTATUS_ALREADY_RUNNING:
  299. return(ERROR_SERVICE_ALREADY_RUNNING);
  300. case SCESTATUS_SERVICE_NOT_SUPPORT:
  301. return(ERROR_NOT_SUPPORTED);
  302. default:
  303. return(ERROR_EXTENDED_ERROR);
  304. }
  305. }
  306. SCESTATUS
  307. SmbsvcpDosErrorToSceStatus(
  308. DWORD rc
  309. )
  310. {
  311. switch(rc) {
  312. case NO_ERROR:
  313. return(SCESTATUS_SUCCESS);
  314. case ERROR_INVALID_PARAMETER:
  315. return(SCESTATUS_INVALID_PARAMETER);
  316. case ERROR_INVALID_DATA:
  317. return(SCESTATUS_INVALID_DATA);
  318. case ERROR_FILE_NOT_FOUND:
  319. case ERROR_PATH_NOT_FOUND:
  320. return(SCESTATUS_PROFILE_NOT_FOUND);
  321. case ERROR_ACCESS_DENIED:
  322. case ERROR_SHARING_VIOLATION:
  323. case ERROR_LOCK_VIOLATION:
  324. case ERROR_NETWORK_ACCESS_DENIED:
  325. return(SCESTATUS_ACCESS_DENIED);
  326. case ERROR_NOT_ENOUGH_MEMORY:
  327. case ERROR_OUTOFMEMORY:
  328. return(SCESTATUS_NOT_ENOUGH_RESOURCE);
  329. case ERROR_BAD_FORMAT:
  330. return(SCESTATUS_BAD_FORMAT);
  331. case ERROR_CURRENT_DIRECTORY:
  332. return(SCESTATUS_CANT_DELETE);
  333. case ERROR_SECTOR_NOT_FOUND:
  334. case ERROR_NONE_MAPPED:
  335. case ERROR_SERVICE_DOES_NOT_EXIST:
  336. case ERROR_RESOURCE_DATA_NOT_FOUND:
  337. case ERROR_NO_MORE_ITEMS:
  338. #if !defined(_NT4BACK_PORT)
  339. case ERROR_INVALID_TRANSFORM:
  340. #endif
  341. return(SCESTATUS_RECORD_NOT_FOUND);
  342. case ERROR_DUP_NAME:
  343. case ERROR_FILE_EXISTS:
  344. return(SCESTATUS_OBJECT_EXIST);
  345. case ERROR_BUFFER_OVERFLOW:
  346. return(SCESTATUS_PREFIX_OVERFLOW);
  347. case ERROR_INSUFFICIENT_BUFFER:
  348. return(SCESTATUS_BUFFER_TOO_SMALL);
  349. case ERROR_SERVICE_ALREADY_RUNNING:
  350. return(SCESTATUS_ALREADY_RUNNING);
  351. case ERROR_NOT_SUPPORTED:
  352. return(SCESTATUS_SERVICE_NOT_SUPPORT);
  353. default:
  354. return(SCESTATUS_OTHER_ERROR);
  355. }
  356. }