在 Windows 下启动外部程序

Managing C++ libs with CMake ExternalProject and Git

CMake has a functionality called ExternalProject_add, which can use, to automatically check out a git repository, and build that as part of the normal build process.

Of course, the same functionality would be possible with git submodules, but honestly, I always found this part of git rather cumbersome, and unnecessarily complex, for what I want to do.

Git 子模块

启动外部程序

在 windows 下如何使用 C++ 语言启动外部程序?

函数 system()

使用 CMD.exe 命令行窗口解释输入的字符串命令,引用来自 system, _wsystem

The system function passes command to the command interpreter, which executes the string as an operating-system command.

system uses the COMSPEC and PATH environment variables to locate the command-interpreter file CMD.exe.

函数 WinExec

This function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function.

函数 CreateProcess

创建新的进程。

Creates a new process and its primary thread.

[in, optional] lpApplicationName

The name of the module to be executed. This module can be a Windows-based application. It can be some other type of module (for example, MS-DOS or OS/2) if the appropriate subsystem is available on the local computer.

函数 ShellExecute

对指定文件执行某项操作。比如针对目录浏览、查找、打开;针对文件打开、编辑、打印等;打开网址;发送邮件等

Performs an operation on a specified file.

命令 start

启动一个单独的窗口以运行指定的程序或命令。

start /B "标题" "chrome.exe"

命令 taskkill

按照进程 ID 或映像名称终止任务。

taskkill /f /im chrome.exe

进程是否存在

用到的命令: tasklist|findstr /i "chrome.exe">nul && echo "存在" || echo "不存在" ,引用自 bat脚本判断windows服务,判断windows进程

  • 进程列表 tasklist
  • 在文件中查找字符串 find 或者 findstr
  • 服务列表 sc query
  • 端口列表 netstat -ano
  • The IF ERRORLEVEL n test succeeds if the error level is n or more.