Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It depends on your platform and the commands you are going to use. The usage of <a href="http://msdn.microsoft.com/en-us/library/277bwbdz%28v=vs.71%29.aspx" rel="nofollow"><code>system()</code></a> for calling console commands is by the way strongly discouraged by most people (it's way to heavy for most purposes).<br /> I would suggest to you using <code>CreateProcess()</code> with the CREATE_NO_WINDOW flag and waiting for the process to exit with a call to <code>WaitForSingleObject()</code> and <code>GetExitCodeProcess()</code>.<br /> This approach utilizes the fact, that most CMD command are executables, located somewhere in <code>C:/Windows/...</code>.</p> <pre><code>/* * Executes a program and returns it's exit code. * * TODO: Error checking should be added for * CreateProcess() * WaitForSingleObject() * GetExitCodeProcess() */ DWORD ExecCmdLine(TCHAR const* cmdline) { STARTUPINFO si; memset(&amp;si, 0, sizeof(si)); si.cb = sizeof(si); PROCESS_INFORMATION pi; memset(&amp;pi, 0, sizeof(pi)); ::CreateProcess(NULL, cmdline, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &amp;si, &amp;pi); ::CloseHandle(pi.Thread); ::WaitForSingleObject(pi.hProcess, INFINITE); DWORD exitcode; ::GetExitCodeProcess(pi.hProcess, &amp;exitcode); ::CloseHandle(pi.hProcess); return exitcode; } </code></pre> <p>If you want to retrieve the output of the command you could also provide <code>hStdOutput</code>, <code>hStdError</code> in the <code>STARTUPINFO</code> structure and set <code>STARTF_USESTDHANDLES</code> in <code>STARTUPINFO.dwFlags</code>. You can even do other things in your own program while the command is executing (especially as you mentioned file copy). This one is done the C++ way:</p> <pre><code>/* * TODO: Error checking should be added for * CreateProcess() * WaitForSingleObject() * GetExitCodeProcess() */ class AsyncCmd { public: AsyncCmd(std::string const&amp; cmdline) : cmdline(cmdline), processHandle(NULL) { } ~AsyncCmd() { if (this-&gt;processHandle != NULL) ::CloseHandle(this-&gt;processHandle); } // Starts the execution of the commandline. void Start(HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE), HANDLE hErr = GetStdHandle(STD_ERROR_HANDLE)) { STARTUPINFO si; memset(&amp;si, 0, sizeof(si)); si.cb = sizeof(si); si.dwFlags = STARTF_USESTDHANDLES; si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); si.hStdOutput = hOut; si.hStdError = hErr; PROCESS_INFORMATION pi; memset(&amp;pi, 0, sizeof(pi)); ::CreateProcess(NULL, this-&gt;cmdline.c_str(), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &amp;si, &amp;pi); ::CloseHandle(pi.hThread); this-&gt;processHandle = pi.hProcess; } // Blocks until execution is finished and returns the exit code. DWORD ExitCode() { ::WaitForSingleObject(this-&gt;processHandle, INFINITE); DWORD exitcode; ::GetExitCodeProcess(this-&gt;processHandle, &amp;exitcode); return exitcode; } private: AsyncCmd(AsyncCmd const&amp;); AsyncCmd&amp; operator=(AsyncCmd const&amp;); std::string cmdline; HANDLE processHandle; } </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. 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.
 

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