开发者

system() to c++ without calling cmd.exe

how can I run system("") without showing cmd.exe?

I use cstdlib header code开发者_如何学JAVA::blocks 10.5

I saw this question for c# but I don't know c# ;)


I believe you'll have to go with CreateProcess instead.


I must say, the existing answer wasn't particularly descriptive. Here's a way to execute commands without a new cmd.exe window.

Based on an answer by Roland Rabien and MSDN, I've written a working function:

int windows_system(const char *cmd)
{
  PROCESS_INFORMATION p_info;
  STARTUPINFO s_info;
  LPSTR cmdline, programpath;

  memset(&s_info, 0, sizeof(s_info));
  memset(&p_info, 0, sizeof(p_info));
  s_info.cb = sizeof(s_info);

  cmdline     = _tcsdup(TEXT(cmd));
  programpath = _tcsdup(TEXT(cmd));

  if (CreateProcess(programpath, cmdline, NULL, NULL, 0, 0, NULL, NULL, &s_info, &p_info))
  {
    WaitForSingleObject(p_info.hProcess, INFINITE);
    CloseHandle(p_info.hProcess);
    CloseHandle(p_info.hThread);
  }
}

Works on all Windows platforms. Call just like you would system().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜