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.

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