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.

326 lines
11 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. // Windows Include files
  23. //
  24. #include <nt.h>
  25. #include <ntrtl.h>
  26. #include <nturtl.h>
  27. #include <windows.h>
  28. #include <locale.h>
  29. #include "mbctype.h"
  30. #include "strsafe.h"
  31. #include <winperf.h>
  32. #include <loadperf.h>
  33. static CHAR szFileNameBuffer[MAX_PATH * 2];
  34. LPWSTR
  35. LodctrMultiByteToWideChar(LPSTR aszString)
  36. {
  37. LPWSTR wszString = NULL;
  38. int dwValue = MultiByteToWideChar(_getmbcp(), 0, aszString, -1, NULL, 0);
  39. if (dwValue != 0) {
  40. wszString = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (dwValue + 1) * sizeof(WCHAR));
  41. if (wszString != NULL) {
  42. MultiByteToWideChar(_getmbcp(), 0, aszString, -1, wszString, dwValue + 1);
  43. }
  44. }
  45. return wszString;
  46. }
  47. LPCSTR GetTrustedServiceName(LPCSTR szArg1)
  48. {
  49. LPSTR szReturn = NULL;
  50. if (lstrlenA(szArg1) >= 4) {
  51. if ((szArg1[0] == '-' || szArg1[0] == '/') && (szArg1[1] == 't' || szArg1[1] == 'T') && (szArg1[2] == ':')) {
  52. szReturn = (LPSTR) & szArg1[3];
  53. }
  54. }
  55. return (LPCSTR) szReturn;
  56. }
  57. BOOL
  58. GetUpgradeFileNames(LPCSTR * szArgs, LPSTR * szCounterFile, LPSTR * szHelpFile, LPSTR * szLangId)
  59. {
  60. DWORD dwArgIdx = 1;
  61. DWORD dwMask = 0;
  62. * szCounterFile = NULL;
  63. * szHelpFile = NULL;
  64. * szLangId = NULL;
  65. do {
  66. if (lstrlenA(szArgs[dwArgIdx]) >= 4) {
  67. if ((szArgs[dwArgIdx][0] == '-') || (szArgs[dwArgIdx][0] == '/')) {
  68. if ((szArgs[dwArgIdx][1] == 'c' || szArgs[dwArgIdx ][1] == 'C') && (szArgs[dwArgIdx][2] == ':')) {
  69. * szCounterFile = (LPSTR) & szArgs[dwArgIdx][3];
  70. dwMask |= 1;
  71. }
  72. else if ((szArgs[dwArgIdx][1] == 'h' || szArgs[dwArgIdx][1] == 'H') && (szArgs[dwArgIdx][2] == ':')) {
  73. * szHelpFile = (LPSTR) & szArgs[dwArgIdx][3];
  74. dwMask |= 2;
  75. }
  76. else if ((szArgs[dwArgIdx][1] == 'l' || szArgs[dwArgIdx][1] == 'L') && (szArgs[dwArgIdx][2] == ':')) {
  77. * szLangId = (LPSTR) & szArgs[dwArgIdx][3];
  78. dwMask |= 4;
  79. }
  80. }
  81. }
  82. dwArgIdx ++;
  83. }
  84. while (dwArgIdx <= 3);
  85. return (dwMask == 7) ? (TRUE) : (FALSE);
  86. }
  87. BOOL GetSaveFileName(LPCSTR szArg1, LPCSTR * szSaveFile)
  88. {
  89. BOOL bReturn = FALSE;
  90. DWORD dwSize = 0;
  91. * szSaveFile = NULL;
  92. if (lstrlenA(szArg1) >= 4) {
  93. if ((szArg1[0] == '-' || szArg1[0] == '/') && (szArg1[1] == 's' || szArg1[1] == 'S') && (szArg1[2] == ':')) {
  94. bReturn = TRUE;
  95. ZeroMemory(szFileNameBuffer, sizeof(szFileNameBuffer));
  96. dwSize = SearchPathA(NULL,
  97. (LPSTR) & szArg1[3],
  98. NULL,
  99. RTL_NUMBER_OF(szFileNameBuffer),
  100. szFileNameBuffer,
  101. NULL);
  102. if (dwSize == 0) {
  103. * szSaveFile = (LPSTR) & szArg1[3];
  104. }
  105. else {
  106. * szSaveFile = szFileNameBuffer;
  107. }
  108. }
  109. }
  110. return bReturn;
  111. }
  112. BOOL GetRestoreFileName(LPCSTR szArg1, LPCSTR * szRestoreFile)
  113. {
  114. BOOL bReturn = FALSE;
  115. DWORD dwSize = 0;
  116. * szRestoreFile = NULL;
  117. if (lstrlenA(szArg1) >= 2) {
  118. if ((szArg1[0] == '-' || szArg1[0] == '/') && (szArg1[1] == 'r' || szArg1[1] == 'R')) {
  119. if (lstrlenA(szArg1) >= 4 && szArg1[2] == ':') {
  120. ZeroMemory(szFileNameBuffer, sizeof(szFileNameBuffer));
  121. dwSize = SearchPathA(NULL,
  122. (LPSTR) & szArg1[3],
  123. NULL,
  124. RTL_NUMBER_OF(szFileNameBuffer),
  125. szFileNameBuffer,
  126. NULL);
  127. if (dwSize == 0) {
  128. * szRestoreFile = (LPSTR) & szArg1[3];
  129. }
  130. else {
  131. * szRestoreFile = szFileNameBuffer;
  132. }
  133. }
  134. bReturn = TRUE;
  135. }
  136. }
  137. return bReturn;
  138. }
  139. ////////////////////////////////////////////////////////////////////////////
  140. //
  141. // MySetThreadUILanguage
  142. //
  143. // This routine sets the thread UI language based on the console codepage.
  144. //
  145. // 9-29-00 WeiWu Created.
  146. // Copied from Base\Win32\Winnls so that it works in W2K as well
  147. ////////////////////////////////////////////////////////////////////////////
  148. LANGID WINAPI MySetThreadUILanguage(WORD wReserved)
  149. {
  150. //
  151. // Cache system locale and CP info
  152. //
  153. static LCID s_lidSystem = 0;
  154. static UINT s_uiSysCp = 0;
  155. static UINT s_uiSysOEMCp = 0;
  156. ULONG uiUserUICp;
  157. ULONG uiUserUIOEMCp;
  158. WCHAR szData[16];
  159. UNICODE_STRING ucStr;
  160. LANGID lidUserUI = GetUserDefaultUILanguage();
  161. LCID lcidThreadOld = GetThreadLocale();
  162. //
  163. // Set default thread locale to EN-US
  164. //
  165. // This allow us to fall back to English UI to avoid trashed characters
  166. // when console doesn't meet the criteria of rendering native UI.
  167. //
  168. LCID lcidThread = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  169. UINT uiConsoleCp = GetConsoleOutputCP();
  170. //
  171. // Make sure nobody uses it yet
  172. //
  173. ASSERT(wReserved == 0);
  174. //
  175. // Get cached system locale and CP info.
  176. //
  177. if (!s_uiSysCp) {
  178. LCID lcidSystem = GetSystemDefaultLCID();
  179. if (lcidSystem) {
  180. //
  181. // Get ANSI CP
  182. //
  183. GetLocaleInfoW(lcidSystem, LOCALE_IDEFAULTANSICODEPAGE, szData, sizeof(szData)/sizeof(WCHAR));
  184. RtlInitUnicodeString(&ucStr, szData);
  185. RtlUnicodeStringToInteger(&ucStr, 10, &uiUserUICp);
  186. //
  187. // Get OEM CP
  188. //
  189. GetLocaleInfoW(lcidSystem, LOCALE_IDEFAULTCODEPAGE, szData, sizeof(szData)/sizeof(WCHAR));
  190. RtlInitUnicodeString(&ucStr, szData);
  191. RtlUnicodeStringToInteger(&ucStr, 10, &s_uiSysOEMCp);
  192. //
  193. // Cache system primary langauge
  194. //
  195. s_lidSystem = PRIMARYLANGID(LANGIDFROMLCID(lcidSystem));
  196. }
  197. }
  198. //
  199. // Don't cache user UI language and CP info, UI language can be changed without system reboot.
  200. //
  201. if (lidUserUI) {
  202. GetLocaleInfoW(MAKELCID(lidUserUI,SORT_DEFAULT), LOCALE_IDEFAULTANSICODEPAGE, szData, sizeof(szData)/sizeof(WCHAR));
  203. RtlInitUnicodeString(& ucStr, szData);
  204. RtlUnicodeStringToInteger(& ucStr, 10, &uiUserUICp);
  205. GetLocaleInfoW(MAKELCID(lidUserUI,SORT_DEFAULT), LOCALE_IDEFAULTCODEPAGE, szData, sizeof(szData)/sizeof(WCHAR));
  206. RtlInitUnicodeString(& ucStr, szData);
  207. RtlUnicodeStringToInteger(& ucStr, 10, &uiUserUIOEMCp);
  208. }
  209. //
  210. // Complex scripts cannot be rendered in the console, so we
  211. // force the English (US) resource.
  212. //
  213. if (uiConsoleCp && s_lidSystem != LANG_ARABIC && s_lidSystem != LANG_HEBREW &&
  214. s_lidSystem != LANG_VIETNAMESE && s_lidSystem != LANG_THAI) {
  215. //
  216. // Use UI language for console only when console CP, system CP and UI language CP match.
  217. //
  218. if ((uiConsoleCp == s_uiSysCp || uiConsoleCp == s_uiSysOEMCp) &&
  219. (uiConsoleCp == uiUserUICp || uiConsoleCp == uiUserUIOEMCp)) {
  220. lcidThread = MAKELCID(lidUserUI, SORT_DEFAULT);
  221. }
  222. }
  223. //
  224. // Set the thread locale if it's different from the currently set
  225. // thread locale.
  226. //
  227. if ((lcidThread != lcidThreadOld) && (!SetThreadLocale(lcidThread))) {
  228. lcidThread = lcidThreadOld;
  229. }
  230. //
  231. // Return the thread locale that was set.
  232. //
  233. return (LANGIDFROMLCID(lcidThread));
  234. }
  235. int __cdecl main(int argc, char * argv[])
  236. {
  237. LPSTR szCmdArgFileName = NULL;
  238. LPWSTR wszFileName = NULL;
  239. int nReturn = 0;
  240. BOOL bSuccess = FALSE;
  241. setlocale(LC_ALL, ".OCP");
  242. MySetThreadUILanguage(0);
  243. // check for a service name in the command line
  244. if (argc >= 4) {
  245. LPSTR szCounterFile = NULL;
  246. LPSTR szHelpFile = NULL;
  247. LPSTR szLanguageID = NULL;
  248. bSuccess = GetUpgradeFileNames(argv, & szCounterFile, & szHelpFile, & szLanguageID);
  249. if (bSuccess) {
  250. nReturn = (int) UpdatePerfNameFilesA(szCounterFile, szHelpFile, szLanguageID, 0);
  251. }
  252. }
  253. else if (argc >= 2) {
  254. // then there's a param to check
  255. bSuccess = GetSaveFileName(argv[1], & szCmdArgFileName);
  256. if (bSuccess && szCmdArgFileName != NULL) {
  257. wszFileName = LodctrMultiByteToWideChar(szCmdArgFileName);
  258. if (wszFileName != NULL) {
  259. nReturn = (int) BackupPerfRegistryToFileW((LPCWSTR) wszFileName, (LPCWSTR) L"");
  260. HeapFree(GetProcessHeap(), 0, wszFileName);
  261. }
  262. }
  263. if (! bSuccess) {
  264. bSuccess = GetRestoreFileName(argv[1], & szCmdArgFileName);
  265. if (bSuccess) {
  266. wszFileName = NULL;
  267. if (szCmdArgFileName != NULL) {
  268. wszFileName = LodctrMultiByteToWideChar(szCmdArgFileName);
  269. }
  270. nReturn = (int) RestorePerfRegistryFromFileW((LPCWSTR) wszFileName, NULL);
  271. if (wszFileName != NULL) {
  272. HeapFree(GetProcessHeap(), 0, wszFileName);
  273. }
  274. }
  275. }
  276. if (! bSuccess) {
  277. szCmdArgFileName = (LPSTR) GetTrustedServiceName(argv[1]);
  278. if (szCmdArgFileName != NULL) {
  279. wszFileName = LodctrMultiByteToWideChar(szCmdArgFileName);
  280. if (wszFileName != NULL) {
  281. nReturn = (int) SetServiceAsTrustedW(NULL, (LPCWSTR) wszFileName);
  282. bSuccess = TRUE;
  283. HeapFree(GetProcessHeap(), 0, wszFileName);
  284. }
  285. }
  286. }
  287. }
  288. if (! bSuccess) {
  289. // if here then load the registry from an ini file
  290. LPWSTR lpCommandLine = GetCommandLineW();
  291. nReturn = (int) LoadPerfCounterTextStringsW(lpCommandLine, FALSE);
  292. }
  293. return nReturn;
  294. }