前言
几种常见的执行命令行方法:
- winExec函数
- CreateProcess函数
- System函数
- pipopen管道函数
- ShellExecute函数
WinExec
作用
运行指定应用程序。
UINT WinExec( [in] LPCSTR lpCmdLine, [in] UINT uCmdShow );
lpCmdLine
:命令行(文件名+可选参数)为应用程序执行。
如果没有包含文件路径将会从以下目录按顺序搜索:
- 目录的应用程序加载。
- 当前目录。
- Windows系统目录。
- GetSystemDirectory函数检索该目录的路径。
- Windows目录。
- GetWindowsDirectory函数检索该目录的路径。
- 环境变量中列出的目录路径。
uCmdShow
:显示选项。可接受的值的列表,请参阅nCmdShow的描述参数的显示窗口函数。
SW_HIDE、SW_NORMAL、SW_SHOWMINIMIZED、SW_SHOWMAXIMIZED等
实例
隐藏控制台弹窗 执行vb弹窗脚本
WinExec("mshta vbscript:msgbox(\"提示内容\",64,\"提示框Title\")(window.close)", SW_HIDE);
注意:
这个函数只提供是为了与16位Windows兼容。
应用程序应该使用CreateProcess函数。
CreateProcess
作用
实例
LPCSTR lpCmdLine = "mshta vbscript:msgbox(\"提示内容\",64,\"提示框Title\")(window.close)"; UINT uCmdShow = 0; PROCESS_INFORMATION pi; STARTUPINFO si; // 建立新进程 ZeroMemory(&si, sizeof(STARTUPINFO)); BOOL b = CreateProcess(NULL, const_cast<LPTSTR>(lpCmdLine), NULL,NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi); if (!b) { return FALSE; } // 阻塞调用者线程 WaitForSingleObject(pi.hProcess, INFINITE); return TRUE;
System
System C++自带的 会阻塞
popen
// crt_popen.c /* This program uses _popen and _pclose to receive a * stream of text from a system process. */ #include <stdio.h> #include <stdlib.h> int main( void ) { char psBuffer[128]; FILE *pPipe; /* Run DIR so that it writes its output to a pipe. Open this * pipe with read text attribute so that we can read it * like a text file. */ if( (pPipe = _popen( "dir *.c /on /p", "rt" )) == NULL ) exit( 1 ); /* Read pipe until end of file, or an error occurs. */ while(fgets(psBuffer, 128, pPipe)) { puts(psBuffer); } /* Close pipe and print return value of pPipe. */ if (feof( pPipe)) { printf( "\nProcess returned %d\n", _pclose( pPipe ) ); } else { printf( "Error: Failed to read the pipe to the end.\n"); } }
ShellExecute
ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件、打开一个目录、打印一个文件等等),并对外部程序有一定的控制。
ShellExecute
ShellExecute(NULL, "mshta", "vbscript:msgbox(\"提示内容\",64,\"提示框Title\")(window.close)", NULL, NULL, SW_SHOW);
ShellExecuteEx
管理员权限运行命令
SHELLEXECUTEINFO shExecInfo; shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); shExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; shExecInfo.hwnd = NULL; shExecInfo.lpVerb = L"runas"; shExecInfo.lpFile = L"mshta.exe"; shExecInfo.lpDirectory = L""; shExecInfo.lpParameters = L"vbscript:msgbox(\"提示内容\",64,\"提示框Title\")(window.close)"; shExecInfo.lpClass = NULL; shExecInfo.nShow = SW_HIDE; shExecInfo.hInstApp = NULL; DWORD err = ERROR_SUCCESS; if (!ShellExecuteEx(&shExecInfo)) { return GetLastError(); }
区别比较
c++ system()和WinExec()的区别
https://blog.csdn.net/qq_26591517/article/details/80513985
1.CreateProcess因为使用复杂,比较少用。
2.WinExec主要运行EXE文件。如:WinExec(‘Notepad.exe Readme.txt’, SW_SHOW);
3.ShellExecute不仅可以运行EXE文件,也可以运行已经关联的文件。
winExec是不同步的进程调用,就是调用起来了就返回了,不会等调用起来的程序结束
system是同步调用进程,调用进程不结束,它就不返回,它可以获取调用进程所返回的值
函数 | 优点 | 缺点 | 隐藏cmd方法 | 调用后是否阻塞 |
winExec | 1.使用简单 | 1.老函数 会有调用兼容问题 | uCmdShow参数设置为SW_HIDE | 否 |
System | 1.简单易用 | 需要使用bat命令隐藏 | 是 | |
pipopen | ||||
ShellExecute | 1. 不仅可以运行EXE文件,也可以运行已经关联的文件 | 否 | ||
CreateProcess | 1.使用复杂,功能强大 | 否 |