Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It might not work correctly when using a program that is not attached to the console, because that app might still be running while you think you have the exit code. A solution to do it in C++ looks like below:</p> <pre class="lang-c prettyprint-override"><code>#include "stdafx.h" #include "windows.h" #include "stdio.h" #include "tchar.h" #include "stdio.h" #include "shellapi.h" int _tmain( int argc, TCHAR *argv[] ) { CString cmdline(GetCommandLineW()); cmdline.TrimLeft('\"'); CString self(argv[0]); self.Trim('\"'); CString args = cmdline.Mid(self.GetLength()+1); args.TrimLeft(_T("\" ")); printf("Arguments passed: '%ws'\n",args); STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &amp;si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &amp;pi, sizeof(pi) ); if( argc &lt; 2 ) { printf("Usage: %s arg1,arg2....\n", argv[0]); return -1; } CString strCmd(args); // Start the child process. if( !CreateProcess( NULL, // No module name (use command line) (LPTSTR)(strCmd.GetString()), // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &amp;si, // Pointer to STARTUPINFO structure &amp;pi ) // Pointer to PROCESS_INFORMATION structure ) { printf( "CreateProcess failed (%d)\n", GetLastError() ); return GetLastError(); } else printf( "Waiting for \"%ws\" to exit.....\n", strCmd ); // Wait until child process exits. WaitForSingleObject( pi.hProcess, INFINITE ); int result = -1; if(!GetExitCodeProcess(pi.hProcess,(LPDWORD)&amp;result)) { printf("GetExitCodeProcess() failed (%d)\n", GetLastError() ); } else printf("The exit code for '%ws' is %d\n",(LPTSTR)(strCmd.GetString()), result ); // Close process and thread handles. CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload