Leaked source code of windows server 2003
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.

552 lines
11 KiB

  1. /*++
  2. Copyright (c) 1996-1999 Microsoft Corporation
  3. Module Name:
  4. UTIL.CPP
  5. Abstract:
  6. Utility functions
  7. Author:
  8. Vlad Sadovsky (vlads) 4-12-99
  9. Revision History:
  10. --*/
  11. //
  12. // Headers
  13. //
  14. #include "stdafx.h"
  15. #include "stimon.h"
  16. #include "memory.h"
  17. #include <windowsx.h>
  18. #include <regstr.h>
  19. BOOL
  20. ParseCommandLine(
  21. LPTSTR lpszCmdLine,
  22. UINT *pargc,
  23. LPTSTR *argv
  24. )
  25. /*++
  26. Routine Description:
  27. Parses command line into standard arg.. array.
  28. Arguments:
  29. None.
  30. Return Value:
  31. TRUE - command line parsed
  32. --*/
  33. {
  34. USES_CONVERSION;
  35. LPTSTR pszT = lpszCmdLine;
  36. TCHAR cOption;
  37. UINT iCurrentOption = 0;
  38. *pargc=0;
  39. //
  40. // Get to first parameter in command line.
  41. //
  42. while (*pszT && ((*pszT != '-') && (*pszT != '/')) ) {
  43. pszT++;
  44. }
  45. //
  46. // Parse options from command line
  47. //
  48. while (*pszT) {
  49. // Skip white spaces
  50. while (*pszT && *pszT <= ' ') {
  51. pszT++;
  52. }
  53. if (!*pszT)
  54. break;
  55. if ('-' == *pszT || '/' == *pszT) {
  56. pszT++;
  57. if (!*pszT)
  58. break;
  59. argv[*pargc] = pszT;
  60. (*pargc)++;
  61. }
  62. // Skip till space
  63. while (*pszT && *pszT > ' ') {
  64. pszT++;
  65. }
  66. if (!*pszT)
  67. break;
  68. // Got next argument
  69. *pszT++='\0';
  70. }
  71. //
  72. // Interpret options
  73. //
  74. if (*pargc) {
  75. for (iCurrentOption=0;
  76. iCurrentOption < *pargc;
  77. iCurrentOption++) {
  78. cOption = *argv[iCurrentOption];
  79. pszT = argv[iCurrentOption]+ 2 * sizeof(TCHAR);
  80. switch ((TCHAR)LOWORD(::CharUpper((LPTSTR)cOption))) {
  81. case 'Q':
  82. //
  83. // Exit main service instance
  84. //
  85. g_fStoppingRequest = TRUE;
  86. break;
  87. case 'V':
  88. // Become visible
  89. g_fUIPermitted = TRUE;
  90. break;
  91. case 'H':
  92. // Become invisible
  93. g_fUIPermitted = FALSE;
  94. break;
  95. case 'R':
  96. // Refresh device list
  97. g_fRefreshDeviceList = TRUE;
  98. break;
  99. case 'A':
  100. // Not running as a service, but as an app
  101. g_fRunningAsService = FALSE;
  102. break;
  103. case 'T':
  104. // Value of timeout in seconds
  105. {
  106. UINT uiT = atoi(T2A(pszT));
  107. if (uiT) {
  108. g_uiDefaultPollTimeout = uiT * 1000;
  109. }
  110. }
  111. break;
  112. case 'I':
  113. // Install STI service
  114. g_fInstallingRequest = TRUE;
  115. break;
  116. case 'U':
  117. // Uninstall STI service
  118. g_fRemovingRequest = TRUE;
  119. break;
  120. default:;
  121. break;
  122. }
  123. }
  124. }
  125. //
  126. // Print parsed options for debug build
  127. //
  128. return TRUE;
  129. } // ParseCommandLine
  130. BOOL
  131. IsSetupInProgressMode(
  132. BOOL *pUpgradeFlag // = NULL
  133. )
  134. /*++
  135. Routine Description:
  136. IsSetupInProgressMode
  137. Arguments:
  138. Pointer to the flag, receiving InUpgrade value
  139. Return Value:
  140. TRUE - setup is in progress
  141. FALSE - not
  142. Side effects:
  143. --*/
  144. {
  145. LPCTSTR szKeyName = TEXT("SYSTEM\\Setup");
  146. DWORD dwType, dwSize;
  147. HKEY hKeySetup;
  148. DWORD dwSystemSetupInProgress,dwUpgradeInProcess;
  149. LONG lResult;
  150. DBGTRACE _t(TEXT("IsSetupInProgressMode"));
  151. if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, szKeyName, 0,
  152. KEY_READ, &hKeySetup) == ERROR_SUCCESS) {
  153. dwSize = sizeof(DWORD);
  154. lResult = RegQueryValueEx (hKeySetup, TEXT("SystemSetupInProgress"), NULL,
  155. &dwType, (LPBYTE) &dwSystemSetupInProgress, &dwSize);
  156. if (lResult == ERROR_SUCCESS) {
  157. lResult = RegQueryValueEx (hKeySetup, TEXT("UpgradeInProgress"), NULL,
  158. &dwType, (LPBYTE) &dwUpgradeInProcess, &dwSize);
  159. if (lResult == ERROR_SUCCESS) {
  160. DPRINTF(DM_TRACE,
  161. TEXT("[IsInSetupUpgradeMode] dwSystemSetupInProgress =%d, dwUpgradeInProcess=%d "),
  162. dwSystemSetupInProgress,dwUpgradeInProcess);
  163. if( pUpgradeFlag ) {
  164. *pUpgradeFlag = dwUpgradeInProcess ? TRUE : FALSE;
  165. }
  166. if (dwSystemSetupInProgress != 0) {
  167. return TRUE;
  168. }
  169. }
  170. }
  171. RegCloseKey (hKeySetup);
  172. }
  173. return FALSE ;
  174. }
  175. BOOL WINAPI
  176. IsPlatformNT()
  177. {
  178. OSVERSIONINFOA ver;
  179. BOOL bReturn = FALSE;
  180. ZeroMemory(&ver,sizeof(ver));
  181. ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
  182. // Just always call the ANSI function
  183. if(!GetVersionExA(&ver)) {
  184. bReturn = FALSE;
  185. }
  186. else {
  187. switch(ver.dwPlatformId) {
  188. case VER_PLATFORM_WIN32_WINDOWS:
  189. bReturn = FALSE;
  190. break;
  191. case VER_PLATFORM_WIN32_NT:
  192. bReturn = TRUE;
  193. break;
  194. default:
  195. bReturn = FALSE;
  196. break;
  197. }
  198. }
  199. return bReturn;
  200. } // endproc
  201. LONG
  202. RegQueryDword (
  203. IN HKEY hkey,
  204. IN LPCTSTR pszValueName,
  205. OUT LPDWORD pdwValue
  206. )
  207. {
  208. LONG lr;
  209. DWORD dwType;
  210. DWORD dwSize;
  211. ASSERT (hkey);
  212. ASSERT (pszValueName);
  213. ASSERT (pdwValue);
  214. dwSize = sizeof(DWORD);
  215. lr = RegQueryValueEx (
  216. hkey,
  217. pszValueName,
  218. NULL,
  219. &dwType,
  220. (LPBYTE)pdwValue,
  221. &dwSize);
  222. if (!lr && (REG_DWORD != dwType))
  223. {
  224. *pdwValue = 0;
  225. lr = ERROR_INVALID_DATATYPE;
  226. }
  227. return lr;
  228. }
  229. LONG
  230. RegQueryValueWithAlloc (
  231. IN HKEY hkey,
  232. IN LPCTSTR pszValueName,
  233. IN DWORD dwTypeMustBe,
  234. OUT LPBYTE* ppbData,
  235. OUT LPDWORD pdwSize
  236. )
  237. {
  238. LONG lr;
  239. DWORD dwType;
  240. DWORD dwSize;
  241. ASSERT (hkey);
  242. ASSERT (pszValueName);
  243. ASSERT (ppbData);
  244. ASSERT (pdwSize);
  245. // Initialize the output parameters.
  246. //
  247. *ppbData = NULL;
  248. *pdwSize = 0;
  249. // Get the size of the buffer required.
  250. //
  251. dwSize = 0;
  252. lr = RegQueryValueEx (
  253. hkey,
  254. pszValueName,
  255. NULL,
  256. &dwType,
  257. NULL,
  258. &dwSize);
  259. if (!lr && (dwType == dwTypeMustBe) && dwSize)
  260. {
  261. LPBYTE pbData;
  262. // Allocate the buffer.
  263. //
  264. lr = ERROR_OUTOFMEMORY;
  265. pbData = (PBYTE)MemAlloc (0, dwSize);
  266. if (pbData)
  267. {
  268. // Get the data.
  269. //
  270. lr = RegQueryValueEx (
  271. hkey,
  272. pszValueName,
  273. NULL,
  274. &dwType,
  275. pbData,
  276. &dwSize);
  277. if (!lr)
  278. {
  279. *ppbData = pbData;
  280. *pdwSize = dwSize;
  281. }
  282. else
  283. {
  284. MemFree (pbData);
  285. }
  286. }
  287. }
  288. else if (!lr && (dwType != dwTypeMustBe))
  289. {
  290. lr = ERROR_INVALID_DATA;
  291. }
  292. return lr;
  293. }
  294. LONG
  295. RegQueryString (
  296. IN HKEY hkey,
  297. IN LPCTSTR pszValueName,
  298. IN DWORD dwTypeMustBe,
  299. OUT PTSTR* ppszData
  300. )
  301. {
  302. LONG lr;
  303. DWORD dwSize;
  304. ASSERT (hkey);
  305. ASSERT (pszValueName);
  306. lr = RegQueryValueWithAlloc (
  307. hkey,
  308. pszValueName,
  309. dwTypeMustBe,
  310. (LPBYTE*)ppszData,
  311. &dwSize);
  312. return lr;
  313. }
  314. LONG
  315. RegQueryStringA (
  316. IN HKEY hkey,
  317. IN LPCTSTR pszValueName,
  318. IN DWORD dwTypeMustBe,
  319. OUT PTSTR* ppszData
  320. )
  321. {
  322. LONG lr;
  323. PTSTR pszUnicode;
  324. ASSERT (hkey);
  325. ASSERT (pszValueName);
  326. ASSERT (ppszData);
  327. // Initialize the output parameter.
  328. //
  329. *ppszData = NULL;
  330. lr = RegQueryString (
  331. hkey,
  332. pszValueName,
  333. dwTypeMustBe,
  334. &pszUnicode);
  335. if (!lr && pszUnicode)
  336. {
  337. INT cb;
  338. INT cchUnicode = lstrlen (pszUnicode) + 1;
  339. // Compute the number of bytes required to hold the ANSI string.
  340. //
  341. cb = WideCharToMultiByte (
  342. CP_ACP, // CodePage
  343. 0, // dwFlags
  344. (LPCWSTR)pszUnicode,
  345. cchUnicode,
  346. NULL, // no buffer to receive translated string
  347. 0, // return the number of bytes required
  348. NULL, // lpDefaultChar
  349. NULL); // lpUsedDefaultChar
  350. if (cb)
  351. {
  352. PSTR pszAnsi;
  353. lr = ERROR_OUTOFMEMORY;
  354. pszAnsi = (PSTR)MemAlloc (0, cb);
  355. if (pszAnsi)
  356. {
  357. lr = NOERROR;
  358. // Now translate the UNICODE string to ANSI.
  359. //
  360. cb = WideCharToMultiByte (
  361. CP_ACP, // CodePage
  362. 0, // dwFlags
  363. (LPCWSTR)pszUnicode,
  364. cchUnicode,
  365. pszAnsi, // buffer to receive translated string
  366. cb, // return the number of bytes required
  367. NULL, // lpDefaultChar
  368. NULL); // lpUsedDefaultChar
  369. if (cb)
  370. {
  371. *ppszData = (PTSTR)pszAnsi;
  372. }
  373. else
  374. {
  375. MemFree (pszAnsi);
  376. lr = GetLastError ();
  377. }
  378. }
  379. }
  380. MemFree (pszUnicode);
  381. } else {
  382. lr = ERROR_NOT_ENOUGH_MEMORY;
  383. }
  384. return lr;
  385. }
  386. LONG
  387. OpenServiceParametersKey (
  388. LPCTSTR pszServiceName,
  389. HKEY* phkey
  390. )
  391. {
  392. LONG lr;
  393. HKEY hkeyServices;
  394. // Open the Services key.
  395. //
  396. lr = RegOpenKeyEx (
  397. HKEY_LOCAL_MACHINE,
  398. REGSTR_PATH_SERVICES,
  399. 0,
  400. KEY_READ,
  401. &hkeyServices);
  402. if (!lr)
  403. {
  404. HKEY hkeySvc;
  405. // Open the service key.
  406. //
  407. lr = RegOpenKeyEx (
  408. hkeyServices,
  409. pszServiceName,
  410. 0,
  411. KEY_READ,
  412. &hkeySvc);
  413. if (!lr)
  414. {
  415. // Open the Parameters key.
  416. //
  417. lr = RegOpenKeyEx (
  418. hkeySvc,
  419. TEXT("Parameters"),
  420. 0,
  421. KEY_READ,
  422. phkey);
  423. RegCloseKey (hkeySvc);
  424. }
  425. RegCloseKey (hkeyServices);
  426. }
  427. return lr;
  428. }