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

简介: [笔记]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方法 调用后是否阻塞
winExec 1.使用简单 1.老函数 会有调用兼容问题 uCmdShow参数设置为SW_HIDE
System 1.简单易用 需要使用bat命令隐藏
pipopen
ShellExecute 1. 不仅可以运行EXE文件,也可以运行已经关联的文件
CreateProcess 1.使用复杂,功能强大
相关文章
|
3月前
|
缓存 网络协议 数据安全/隐私保护
[运维笔记] - (命令).Windows server常用网络相关命令总结
[运维笔记] - (命令).Windows server常用网络相关命令总结
191 0
|
3月前
|
安全 Windows
windows11 永久关闭windows defender的方法
windows11 永久关闭windows defender的方法
336 2
|
5月前
|
Windows
windows家庭版禁用 hype-v 命令行
windows家庭版禁用 hype-v 命令行
30 0
|
5月前
|
Windows
windows windows10 查看进程的命令行
windows windows10 查看进程的命令行
40 0
|
3月前
|
Ubuntu Linux Windows
两种Ubuntu和Windows互相共享文件夹的方法
两种Ubuntu和Windows互相共享文件夹的方法
|
3天前
|
API C++ Windows
windows编程入门_链接错误的配置
windows编程入门_链接错误的配置
8 0
|
3月前
|
API Python Windows
python3应用windows api对后台程序窗口及桌面截图并保存的方法
python3应用windows api对后台程序窗口及桌面截图并保存的方法
97 1
|
1月前
|
安全 Windows
关闭Windows自动更新的6种方法
关闭Windows自动更新的6种方法
160 0
|
2月前
|
Windows
不让Windows显示语言栏“中”“英”字符的一种方法
【2月更文挑战第6天】本文介绍在Windows 11操作系统中,将任务栏右下角的语言栏的“中”、“英”标识加以隐藏、消除的一种或许可行的方法~
不让Windows显示语言栏“中”“英”字符的一种方法
|
2月前
|
Windows
火山中文编程 -- 第一个windows程序
火山中文编程 -- 第一个windows程序
12 0