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.

287 lines
8.6 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. lodctr.c
  5. Abstract:
  6. Program to read the contents of the file specified in the command line
  7. and update the registry accordingly
  8. Author:
  9. Bob Watson (a-robw) 10 Feb 93
  10. Revision History:
  11. a-robw 25-Feb-93 revised calls to make it compile as a UNICODE or
  12. an ANSI app.
  13. a-robw 10-Nov-95 revised to use DLL functions for all the dirty work
  14. // command line arguments supported:
  15. /C:<filename> upgrade counter text strings using <filename>
  16. /H:<filename> upgrade help text strings using <filename>
  17. /L:<LangID> /C and /H params are for language <LangID>
  18. /S:<filename> save current perf registry strings & info to <filname>
  19. /R:<filename> restore perf registry strings & info using <filname>
  20. /T:<service> set <service> to be Trusted using current DLL
  21. --*/
  22. #define UNICODE 1
  23. #define _UNICODE 1
  24. //
  25. // Windows Include files
  26. //
  27. #include <nt.h>
  28. #include <ntrtl.h>
  29. #include <nturtl.h>
  30. #include <windows.h>
  31. #include <stdlib.h>
  32. #include <tchar.h>
  33. #include <loadperf.h>
  34. #include "..\common\common.h"
  35. static CHAR szFileNameBuffer[MAX_PATH*2];
  36. static
  37. LPCSTR
  38. GetTrustedServiceName (
  39. LPCSTR szArg1
  40. )
  41. {
  42. LPSTR szReturn = NULL;
  43. if ((szArg1[0] == '-') || (szArg1[0] == '/')) {
  44. //it's a switch command
  45. if ((szArg1[1] == 't') || (szArg1[1] == 'T')) {
  46. // the command is to Save the configuration to a file
  47. if (szArg1[2] == ':') {
  48. // then the rest of the string is the name of the trusted service
  49. szReturn = (LPSTR)&szArg1[3];
  50. }
  51. }
  52. }
  53. return (LPCSTR)szReturn;
  54. }
  55. static
  56. BOOL
  57. GetUpgradeFileNames (
  58. LPCSTR *szArgs,
  59. LPSTR *szCounterFile,
  60. LPSTR *szHelpFile,
  61. LPSTR *szLangId
  62. )
  63. {
  64. DWORD dwArgIdx = 1;
  65. DWORD dwMask = 0;
  66. do {
  67. // check first arg, function assumes there are 2 args or more
  68. if ((szArgs[dwArgIdx][0] == '-') || (szArgs[dwArgIdx][0] == '/')) {
  69. //it's a switch command
  70. if ((szArgs[dwArgIdx][1] == 'c') || (szArgs[dwArgIdx ][1] == 'C')) {
  71. // the command is to load a service
  72. if (szArgs[dwArgIdx ][2] == ':') {
  73. // then the rest of the string is the service name
  74. *szCounterFile = (LPSTR)&szArgs[dwArgIdx ][3];
  75. dwMask |= 1;
  76. }
  77. } else if ((szArgs[dwArgIdx][1] == 'h') || (szArgs[dwArgIdx ][1] == 'H')) {
  78. // the command is to load a service
  79. if (szArgs[dwArgIdx][2] == ':') {
  80. // then the rest of the string is the service name
  81. *szHelpFile = (LPSTR)&szArgs[dwArgIdx ][3];
  82. dwMask |= 2;
  83. }
  84. } else if ((szArgs[dwArgIdx][1] == 'l') || (szArgs[dwArgIdx ][1] == 'L')) {
  85. // the command is to load a service
  86. if (szArgs[dwArgIdx][2] == ':') {
  87. // then the rest of the string is the service name
  88. *szLangId = (LPSTR)&szArgs[dwArgIdx ][3];
  89. dwMask |= 4;
  90. }
  91. }
  92. }
  93. dwArgIdx++;
  94. } while (dwArgIdx <= 3);
  95. if (dwMask == 7) {
  96. // all names found
  97. return TRUE;
  98. } else {
  99. return FALSE;
  100. }
  101. }
  102. static
  103. LPCSTR
  104. GetSaveFileName (
  105. LPCSTR szArg1
  106. )
  107. {
  108. LPSTR szReturn = NULL;
  109. DWORD dwSize;
  110. if ((szArg1[0] == '-') || (szArg1[0] == '/')) {
  111. //it's a switch command
  112. if ((szArg1[1] == 's') || (szArg1[1] == 'S')) {
  113. // the command is to Save the configuration to a file
  114. if (szArg1[2] == ':') {
  115. // then the rest of the string is the input file name
  116. dwSize = SearchPathA (NULL, (LPSTR)&szArg1[3], NULL,
  117. sizeof(szFileNameBuffer)/sizeof(szFileNameBuffer[0]),
  118. szFileNameBuffer, NULL);
  119. if (dwSize == 0) {
  120. //unable to find file in path so use it as is
  121. szReturn = (LPSTR)&szArg1[3];
  122. } else {
  123. // else return the expanded file name
  124. szReturn = szFileNameBuffer;
  125. }
  126. }
  127. }
  128. }
  129. return (LPCSTR)szReturn;
  130. }
  131. static
  132. LPCSTR
  133. GetRestoreFileName (
  134. LPCSTR szArg1
  135. )
  136. {
  137. LPSTR szReturn = NULL;
  138. DWORD dwSize;
  139. if ((szArg1[0] == '-') || (szArg1[0] == '/')) {
  140. //it's a switch command
  141. if ((szArg1[1] == 'r') || (szArg1[1] == 'R')) {
  142. // the command is to Save the configuration to a file
  143. if (szArg1[2] == ':') {
  144. // then the rest of the string is the input file name
  145. dwSize = SearchPathA (NULL, (LPSTR)&szArg1[3], NULL,
  146. sizeof(szFileNameBuffer)/sizeof(szFileNameBuffer[0]),
  147. szFileNameBuffer, NULL);
  148. if (dwSize == 0) {
  149. //unable to find file in path so use it as is
  150. szReturn = (LPSTR)&szArg1[3];
  151. } else {
  152. // else return the expanded file name
  153. szReturn = szFileNameBuffer;
  154. }
  155. }
  156. }
  157. }
  158. return (LPCSTR)szReturn;
  159. }
  160. int
  161. __cdecl main(
  162. int argc,
  163. char *argv[]
  164. )
  165. /*++
  166. main
  167. Arguments
  168. ReturnValue
  169. 0 (ERROR_SUCCESS) if command was processed
  170. Non-Zero if command error was detected.
  171. --*/
  172. {
  173. LPTSTR lpCommandLine;
  174. LPSTR szCmdArgFileName;
  175. LPWSTR wszFileName;
  176. DWORD dwFileNameLen;
  177. int nReturn;
  178. lpCommandLine = GetCommandLine(); // get command line
  179. // check for a service name in the command line
  180. if (argc >= 2) {
  181. if (argc >= 4) {
  182. BOOL bDoUpdate;
  183. LPSTR szCounterFile = NULL;
  184. LPSTR szHelpFile = NULL;
  185. LPSTR szLanguageID = NULL;
  186. bDoUpdate = GetUpgradeFileNames (
  187. argv,
  188. &szCounterFile,
  189. &szHelpFile,
  190. &szLanguageID);
  191. if (bDoUpdate) {
  192. return (int) UpdatePerfNameFilesA (
  193. szCounterFile,
  194. szHelpFile,
  195. szLanguageID,
  196. 0);
  197. }
  198. } else {
  199. // then there's a param to check
  200. szCmdArgFileName = (LPSTR)GetSaveFileName (argv[1]);
  201. if (szCmdArgFileName != NULL) {
  202. dwFileNameLen = lstrlenA(szCmdArgFileName) + 1;
  203. wszFileName = HeapAlloc (GetProcessHeap(), 0, (dwFileNameLen) * sizeof (WCHAR));
  204. if (wszFileName != NULL) {
  205. mbstowcs (wszFileName, szCmdArgFileName, dwFileNameLen);
  206. nReturn = (int)BackupPerfRegistryToFileW (
  207. (LPCWSTR)wszFileName,
  208. (LPCWSTR)L"");
  209. HeapFree (GetProcessHeap(), 0, wszFileName);
  210. return nReturn;
  211. }
  212. }
  213. // try Restore file name
  214. szCmdArgFileName = (LPSTR)GetRestoreFileName (argv[1]);
  215. if (szCmdArgFileName != NULL) {
  216. dwFileNameLen = lstrlenA(szCmdArgFileName) + 1;
  217. wszFileName = HeapAlloc (GetProcessHeap(), 0, (dwFileNameLen) * sizeof (WCHAR));
  218. if (wszFileName != NULL) {
  219. mbstowcs (wszFileName, szCmdArgFileName, dwFileNameLen);
  220. nReturn = (int)RestorePerfRegistryFromFileW(
  221. (LPCWSTR) wszFileName,
  222. NULL);
  223. HeapFree (GetProcessHeap(), 0, wszFileName);
  224. return nReturn;
  225. }
  226. }
  227. // try Trusted service name
  228. szCmdArgFileName = (LPSTR)GetTrustedServiceName (argv[1]);
  229. if (szCmdArgFileName != NULL) {
  230. dwFileNameLen = lstrlenA(szCmdArgFileName) + 1;
  231. wszFileName = HeapAlloc (GetProcessHeap(), 0, (dwFileNameLen) * sizeof (WCHAR));
  232. if (wszFileName != NULL) {
  233. mbstowcs (wszFileName, szCmdArgFileName, dwFileNameLen);
  234. nReturn = (int)SetServiceAsTrusted (NULL,
  235. (LPCWSTR)wszFileName); // filename is really the service name
  236. HeapFree (GetProcessHeap(), 0, wszFileName);
  237. return nReturn;
  238. }
  239. }
  240. }
  241. }
  242. // if here then load the registry from an ini file
  243. return (int) LoadPerfCounterTextStrings (
  244. lpCommandLine,
  245. FALSE); // show text strings to console
  246. }