[笔记]Windows核心编程《番外篇》几种常见的执行命令行方法

前言

几种常见的执行命令行方法:

  • winExec函数
  • CreateProcess函数
  • System函数
  • pipopen管道函数
  • ShellExecute函数

WinExec

微软文档 WinExec

作用

运行指定应用程序。

UINT WinExec(
  [in] LPCSTR lpCmdLine,
  [in] UINT   uCmdShow
);

lpCmdLine:命令行(文件名+可选参数)为应用程序执行。
如果没有包含文件路径将会从以下目录按顺序搜索:

  1. 目录的应用程序加载。
  2. 当前目录。
  3. Windows系统目录。
  4. GetSystemDirectory函数检索该目录的路径。
  5. Windows目录。
  6. GetWindowsDirectory函数检索该目录的路径。
  7. 环境变量中列出的目录路径。

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方法调用后是否阻塞
winExec1.使用简单1.老函数 会有调用兼容问题uCmdShow参数设置为SW_HIDE
System1.简单易用需要使用bat命令隐藏
pipopen
ShellExecute1. 不仅可以运行EXE文件,也可以运行已经关联的文件
CreateProcess1.使用复杂,功能强大
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二进制怪兽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值