C++ 执行cmd命令 并读取输出

出处:https://blog.csdn.net/weixin_43903378/article/details/105297406

头文件

#ifndef _PIPE_CMD_H_
#define _PIPE_CMD_H_


#include <Windows.h>


// 执行 cmd 命令, 并获取执行结果数据
BOOL PipeCmd(char *pszCmd, char *pszResultBuffer, DWORD dwResultBufferSize);


#endif

 

源文件

#include "stdafx.h"
#include "PipeCmd.h"


void ShowError(char *pszText)
{
    char szErr[MAX_PATH] = {0};
    ::wsprintf(szErr, "%s Error[%d]\n", pszText, ::GetLastError());
    ::MessageBox(NULL, szErr, "ERROR", MB_OK);
}


// 执行 cmd 命令, 并获取执行结果数据
BOOL PipeCmd(char *pszCmd, char *pszResultBuffer, DWORD dwResultBufferSize)
{
    HANDLE hReadPipe = NULL;
    HANDLE hWritePipe = NULL;
    SECURITY_ATTRIBUTES securityAttributes = {0};
    BOOL bRet = FALSE;
    STARTUPINFO si = {0};
    PROCESS_INFORMATION pi = {0};

    // 设定管道的安全属性
    securityAttributes.bInheritHandle = TRUE;
    securityAttributes.nLength = sizeof(securityAttributes);
    securityAttributes.lpSecurityDescriptor = NULL;
    // 创建匿名管道
    bRet = ::CreatePipe(&hReadPipe, &hWritePipe, &securityAttributes, 0);
    if (FALSE == bRet)
    {
        ShowError("CreatePipe");
        return FALSE;
    }
    // 设置新进程参数
    si.cb = sizeof(si);
    si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    si.wShowWindow = SW_HIDE;
    si.hStdError = hWritePipe;
    si.hStdOutput = hWritePipe;
    // 创建新进程执行命令, 将执行结果写入匿名管道中
    bRet = ::CreateProcess(NULL, pszCmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
    if (FALSE == bRet)
    {
        ShowError("CreateProcess");
    }
    // 等待命令执行结束
    ::WaitForSingleObject(pi.hThread, INFINITE);
    ::WaitForSingleObject(pi.hProcess, INFINITE);
    // 从匿名管道中读取结果到输出缓冲区
    ::RtlZeroMemory(pszResultBuffer, dwResultBufferSize);
    ::ReadFile(hReadPipe, pszResultBuffer, dwResultBufferSize, NULL, NULL);
    // 关闭句柄, 释放内存
    ::CloseHandle(pi.hThread);
    ::CloseHandle(pi.hProcess);
    ::CloseHandle(hWritePipe);
    ::CloseHandle(hReadPipe);

    return TRUE;
}

 

posted on 2021-01-25 15:45  liujx2019  阅读(4426)  评论(0编辑  收藏  举报

导航