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.

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