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.

184 lines
4.4 KiB

  1. /*++
  2. *
  3. * WOW v1.0
  4. *
  5. * Copyright (c) 1993, Microsoft Corporation
  6. *
  7. * WIN.C
  8. * Simple WIN.COM which spawns program given on command line.
  9. * This allows DOS install which run "win appname" to work.
  10. *
  11. * History:
  12. * Created 29-Mar-1993 Dave Hart (davehart)
  13. * 20-Jul-1994 Dave Hart (davehart) Changed from console to windows app.
  14. --*/
  15. #include <windows.h>
  16. //
  17. // Support for debug output disabled for build (no console).
  18. // DPRINTF macro must be used with two sets of parens:
  19. // DPRINTF(("Hello %s\n", szName));
  20. //
  21. #if 0
  22. #include <stdio.h>
  23. #define DPRINTF(args) printf args
  24. #else
  25. #define DPRINTF(args)
  26. #endif
  27. //
  28. // SKIP_BLANKS -- Handy macro to skip blanks.
  29. //
  30. #define SKIP_BLANKS(pch) {while (' ' == *(pch)) { (pch)++; }}
  31. //
  32. // SKIP_NONBLANKS -- Handy macro to skip everything but blanks.
  33. //
  34. #define SKIP_NONBLANKS(pch) {while (*(pch) && ' ' != *(pch)) { (pch)++; }}
  35. //
  36. // WinMain
  37. //
  38. int WinMain(
  39. HINSTANCE hInst,
  40. HINSTANCE hPrevInst,
  41. LPSTR pszCommandLine,
  42. int nCmdShow
  43. )
  44. {
  45. char *psz;
  46. BOOL fSuccess;
  47. DWORD dwExitCode;
  48. STARTUPINFO si;
  49. PROCESS_INFORMATION pi;
  50. DPRINTF(("win.com: Command line is '%s'.\n", pszCommandLine));
  51. //
  52. // Throw away any switches on the command line. The command line
  53. // looks like:
  54. //
  55. // win [/r] [/2] [/s] [/3] [/n] [winapp winapp-args]
  56. //
  57. // So we'll go into a loop of skipping all words that begin with
  58. // "/" or "-" until we hit a word that doesn't start with either,
  59. // which is presumably the winapp name.
  60. //
  61. psz = pszCommandLine;
  62. SKIP_BLANKS(psz);
  63. //
  64. // psz now points to either the first word of our command
  65. // line ("win" not included), or to a null terminator if
  66. // we were invoked without arguments.
  67. //
  68. while ('-' == *psz || '/' == *psz) {
  69. SKIP_NONBLANKS(psz);
  70. //
  71. // psz now points to either a space or the null terminator.
  72. //
  73. SKIP_BLANKS(psz);
  74. //
  75. // psz now points to either the beginning of the next word
  76. // on the command line, or the null terminator.
  77. //
  78. }
  79. if (!(*psz)) {
  80. //
  81. // If psz now points to a null terminator, then win.com was invoked
  82. // either without arguments or all arguments were switches that we
  83. // skipped above. So there's nothing to do!
  84. //
  85. return 0;
  86. }
  87. DPRINTF(("win.com: Invoking '%s'.\n", psz));
  88. //
  89. // Run that program.
  90. //
  91. RtlZeroMemory(&si, sizeof(si));
  92. si.cb = sizeof(si);
  93. si.dwFlags = STARTF_USESHOWWINDOW | STARTF_FORCEONFEEDBACK;
  94. si.wShowWindow = (WORD) nCmdShow;
  95. fSuccess = CreateProcess(
  96. NULL, // image name (in the command line instead)
  97. psz, // command line (begins with image name)
  98. NULL, // lpsaProcess
  99. NULL, // lpsaThread
  100. FALSE, // no handle inheritance
  101. 0, // dwCreateOptions
  102. NULL, // pointer to environment
  103. NULL, // pointer to curdir
  104. &si, // startup info struct
  105. &pi // process information (gets handles)
  106. );
  107. if (!fSuccess) {
  108. dwExitCode = GetLastError();
  109. DPRINTF(("CreateProcess fails with error %d.\n", dwExitCode));
  110. return dwExitCode;
  111. }
  112. //
  113. // Close the thread handle, we're only using the process handle.
  114. //
  115. CloseHandle(pi.hThread);
  116. //
  117. // Wait for the process to terminate and return its exit code as
  118. // our exit code.
  119. //
  120. if (0xffffffff == WaitForSingleObject(pi.hProcess, INFINITE)) {
  121. dwExitCode = GetLastError();
  122. DPRINTF(("WaitForSingleObject(hProcess, INFINITE) fails with error %d.\n",
  123. dwExitCode));
  124. goto Cleanup;
  125. }
  126. if (!GetExitCodeProcess(pi.hProcess, &dwExitCode)) {
  127. dwExitCode = GetLastError();
  128. DPRINTF(("GetExitCodeProcess() fails with error %d.\n", dwExitCode));
  129. goto Cleanup;
  130. }
  131. DPRINTF(("win.com: Returning child's exit code (%d)\n", dwExitCode));
  132. Cleanup:
  133. CloseHandle(pi.hProcess);
  134. return dwExitCode;
  135. }