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.

213 lines
4.8 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1999.
  5. //
  6. // File: main.cxx
  7. //
  8. // Contents: Stub executable that launches Event Viewer Snapin.
  9. //
  10. // History: 01-14-1999 DavidMun Created
  11. //
  12. //---------------------------------------------------------------------------
  13. #include "headers.hxx"
  14. #pragma hdrstop
  15. #define THIS_FILE L"main.cxx"
  16. #define MAX_START_WAIT 500
  17. const WCHAR c_wzBaseCommand[] = L"mmc.exe /s ";
  18. const WCHAR c_wzEventMsc[] = L"eventvwr.msc";
  19. const WCHAR c_wzComputerSwitch[] = L" /COMPUTER=";
  20. //+--------------------------------------------------------------------------
  21. //
  22. // Function: NextSpace
  23. //
  24. // Synopsis: Return a pointer to the first space character found,
  25. // starting at [pwz], or to the end of the string, whichever
  26. // is first.
  27. //
  28. // History: 01-14-1999 DavidMun Created
  29. //
  30. //---------------------------------------------------------------------------
  31. PWSTR
  32. NextSpace(
  33. PWSTR pwz)
  34. {
  35. while (*pwz && *pwz != L' ')
  36. {
  37. pwz++;
  38. }
  39. return pwz;
  40. }
  41. //+--------------------------------------------------------------------------
  42. //
  43. // Function: main
  44. //
  45. // Synopsis: Launch the event viewer snapin.
  46. //
  47. // Returns: 0
  48. //
  49. // History: 01-14-1999 DavidMun Created
  50. //
  51. //---------------------------------------------------------------------------
  52. int _cdecl
  53. main(int argc, char * argv[])
  54. {
  55. //
  56. // Process the command line, ignoring /L and /H switches and treating
  57. // anything else as a server name.
  58. //
  59. PWSTR pwzArgs = GetCommandLine();
  60. //
  61. // Skip this executable's name
  62. //
  63. pwzArgs = NextSpace(pwzArgs);
  64. //
  65. // Advance until the server name or end of string, ignoring switches
  66. //
  67. while (*pwzArgs)
  68. {
  69. if (*pwzArgs == L' ')
  70. {
  71. pwzArgs++;
  72. }
  73. else if (*pwzArgs == L'-' || *pwzArgs == L'/')
  74. {
  75. pwzArgs = NextSpace(pwzArgs);
  76. }
  77. else
  78. {
  79. PWSTR pwzFileEnd = NextSpace(pwzArgs);
  80. *pwzFileEnd = L'\0';
  81. break;
  82. }
  83. }
  84. //
  85. // pwzArgs either points to a server name or end of string.
  86. // Build up a commandline for the CreateProcess call.
  87. WCHAR wzCommandLine[MAX_PATH
  88. + ARRAYLEN(c_wzBaseCommand) - 1
  89. + MAX_PATH
  90. + ARRAYLEN(c_wzComputerSwitch) - 1];
  91. lstrcpy(wzCommandLine, c_wzBaseCommand);
  92. //
  93. // Build the path to the eventvwr.msc file, which is installed
  94. // in system32.
  95. //
  96. WCHAR wzMscPath[MAX_PATH] = L"";
  97. UINT cchSysDir = GetSystemDirectory(wzMscPath, ARRAYLEN(wzMscPath));
  98. if (!cchSysDir || cchSysDir > ARRAYLEN(wzMscPath))
  99. {
  100. PopupMessage(IDS_NO_SYSTEMDIR);
  101. return 0;
  102. }
  103. if (wzMscPath[cchSysDir - 1] != L'\\')
  104. {
  105. wzMscPath[cchSysDir++] = L'\\';
  106. wzMscPath[cchSysDir] = L'\0';
  107. }
  108. lstrcat(wzMscPath, c_wzEventMsc);
  109. //
  110. // Verify that the .msc file exists, because mmc doesn't give a good
  111. // error message if it doesn't.
  112. //
  113. HANDLE hMscFile;
  114. hMscFile = CreateFile(wzMscPath,
  115. 0, // no access, just checking for existence
  116. FILE_SHARE_READ | FILE_SHARE_WRITE,
  117. NULL,
  118. OPEN_EXISTING,
  119. FILE_ATTRIBUTE_NORMAL,
  120. NULL);
  121. if (hMscFile == INVALID_HANDLE_VALUE)
  122. {
  123. PopupMessage(IDS_EVENTVWR_MSC_NOT_FOUND, wzMscPath);
  124. return 0;
  125. }
  126. CloseHandle(hMscFile);
  127. //
  128. // Add eventvwr.msc to command line.
  129. //
  130. lstrcat(wzCommandLine, wzMscPath);
  131. //
  132. // Append the server name, if supplied
  133. //
  134. if (*pwzArgs)
  135. {
  136. lstrcat(wzCommandLine, c_wzComputerSwitch);
  137. lstrcat(wzCommandLine, pwzArgs);
  138. }
  139. //
  140. // Try to launch mmc
  141. //
  142. BOOL fOk;
  143. STARTUPINFO si;
  144. PROCESS_INFORMATION pi;
  145. ZeroMemory(&si, sizeof si);
  146. si.cb = sizeof si;
  147. si.dwFlags = STARTF_USESHOWWINDOW;
  148. si.wShowWindow = SW_SHOWDEFAULT;
  149. ZeroMemory(&pi, sizeof pi);
  150. fOk = CreateProcess(NULL,
  151. wzCommandLine,
  152. NULL,
  153. NULL,
  154. FALSE,
  155. NORMAL_PRIORITY_CLASS,
  156. NULL,
  157. NULL,
  158. &si,
  159. &pi);
  160. if (fOk)
  161. {
  162. WaitForInputIdle(pi.hProcess, MAX_START_WAIT);
  163. CloseHandle(pi.hThread);
  164. CloseHandle(pi.hProcess);
  165. }
  166. else
  167. {
  168. HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
  169. PopupMessageAndCode(THIS_FILE, __LINE__, hr, IDS_MMC_LAUNCH_FAILED);
  170. }
  171. return 0;
  172. }