简体   繁体   English

将system()转换为c ++而不调用cmd.exe

[英]system() to c++ without calling cmd.exe

how can I run system("") without showing cmd.exe? 如何在不显示cmd.exe的情况下运行system(“”)?

I use cstdlib header code::blocks 10.5 我使用cstdlib标头代码:: blocks 10.5

I saw this question for c# but I don't know c# ;) 我看到了C#的这个问题 ,但我不知道C#;)

我相信您必须改用CreateProcess

I must say, the existing answer wasn't particularly descriptive. 我必须说,现有答案并不是特别描述性的。 Here's a way to execute commands without a new cmd.exe window. 这是一种无需新cmd.exe窗口即可执行命令的方法。

Based on an answer by Roland Rabien and MSDN , I've written a working function: 基于Roland RabienMSDN 的回答 ,我编写了一个工作函数:

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. 在所有Windows平台上均可使用。 Call just like you would system() . 就像调用system()一样进行调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM