void prompt(LPWSTR szCmd) {
    TCHAR szModuleName[MAX_PATH];
    STARTUPINFO si = { 0 };
    PROCESS_INFORMATION pi = { 0 };

    GetModuleFileName(NULL, szModuleName, MAX_PATH);

    CreateProcess(NULL, szCmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);

    WaitForSingleObject(pi.hProcess, INFINITE);

    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
}

使用方法:

WCHAR cmd[] = { L"ping www.baidu.com"  };
prompt(cmd);

注意事项:阻塞方法

 

////////////////////////////////////关于CreateProcess////////////////////////////////////

BOOL CreateProcessW(
  [in, optional]      LPCWSTR               lpApplicationName,
  [in, out, optional] LPWSTR                lpCommandLine,
  [in, optional]      LPSECURITY_ATTRIBUTES lpProcessAttributes,
  [in, optional]      LPSECURITY_ATTRIBUTES lpThreadAttributes,
  [in]                BOOL                  bInheritHandles,
  [in]                DWORD                 dwCreationFlags,
  [in, optional]      LPVOID                lpEnvironment,
  [in, optional]      LPCWSTR               lpCurrentDirectory,
  [in]                LPSTARTUPINFOW        lpStartupInfo,
  [out]               LPPROCESS_INFORMATION lpProcessInformation
);
//lpApplicationName: 要执行的应用名称,可以是 完整的路径+应用名称
//例如:
WCHAR  moduleName[] = { L"C:\\Windows\\System32\\cmd.exe" };

//也可以是仅应用名称,如果是应用名称,则系统会使用当前执行的exe的 磁盘盘符+文件目录 来补全路径,且应用名称必须包含后缀名

//lpApplicationName 可以为NULL, 如果为NULL,moduleName必须以 moduleName+空格的形式 添加到lpCommandLine 参数前面

////////////////////////////////////案例 实现git同步代码////////////////////////////////////

#include "Windows.h"
#include "iostream"
#include "vector"
#include <stdio.h>
#include <fstream>
#include <string>


using namespace std;

void run(const WCHAR* moduleName, WCHAR* parameter, WCHAR* dir) {

    TCHAR szModuleName[MAX_PATH];
    STARTUPINFO si = { 0 };
    PROCESS_INFORMATION pi = { 0 };

    GetModuleFileName(NULL, szModuleName, MAX_PATH);

    CreateProcess(moduleName, parameter, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, dir, &si, &pi);

    WaitForSingleObject(pi.hProcess, INFINITE);

    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
}


int main() {

    WCHAR dir[] = { L"C:\\Users\\laremehpe\\Desktop\\test" };
    
    WCHAR parameter1[] = { L"git add ." };

    run(NULL, parameter1, dir);

    WCHAR parameter2[] = { L"git commit -m \"update from cplusplus\"" };

    run(NULL, parameter2, dir);

    WCHAR parameter3[] = { L"git push" };

    run(NULL, parameter3, dir);


	return 0;
}

执行cmd并获取输出:

void run(const WCHAR* moduleName, WCHAR* parameter, WCHAR* dir) {
	BOOL ok = TRUE;
	HANDLE hStdInPipeRead = NULL;
	HANDLE hStdInPipeWrite = NULL;
	HANDLE hStdOutPipeRead = NULL;
	HANDLE hStdOutPipeWrite = NULL;

	// Create two pipes.
	SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
	ok = CreatePipe(&hStdInPipeRead, &hStdInPipeWrite, &sa, 0);
	if (ok == FALSE) return;
	ok = CreatePipe(&hStdOutPipeRead, &hStdOutPipeWrite, &sa, 0);
	if (ok == FALSE) return;

	STARTUPINFO si = { };
	si.cb = sizeof(STARTUPINFO);
	si.dwFlags = STARTF_USESTDHANDLES;
	si.hStdError = hStdOutPipeWrite;
	si.hStdOutput = hStdOutPipeWrite;
	si.hStdInput = hStdInPipeRead;
	PROCESS_INFORMATION pi = { };

	ok = CreateProcess(moduleName, parameter, NULL, NULL, TRUE, 0, NULL, dir, &si, &pi);
	if (ok == FALSE) return;

	// Close pipes we do not need.
	CloseHandle(hStdOutPipeWrite);
	CloseHandle(hStdInPipeRead);

	// The main loop for reading output from the DIR command.
	char buf[1024 + 1] = { };
	DWORD dwRead = 0;
	DWORD dwAvail = 0;
	string logs = "\n";
	try {
		BOOL ok = ReadFile(hStdOutPipeRead, buf, 1024, &dwRead, NULL);
		while (ok == TRUE)
		{
			buf[dwRead] = '\0';
			logs += buf; //也可以在这个地方直接输出内容
			puts(buf);
			ok = ReadFile(hStdOutPipeRead, buf, 1024, &dwRead, NULL);
		}
	}
	catch (...) {
		this->runLog.push_back("catch error at readFile");
	}


	this->runLog.push_back(logs);

	try {
		// Clean up and exit.
		CloseHandle(hStdOutPipeRead);
		CloseHandle(hStdInPipeWrite);

		WaitForSingleObject(pi.hProcess, INFINITE);

		CloseHandle(pi.hThread);
		CloseHandle(pi.hProcess);
	}
	catch (...) {
		this->runLog.push_back("catch error at close handle");
	}
}

//测试
LPWSTR moduleName = (LPWSTR)L"C:\\Windows\\System32\\cmd.exe";
WCHAR parameter1[] = { L"/c git add ." };
run(moduleName, parameter1, dir);

 

 posted on 2023-09-26 15:04  laremehpe  阅读(104)  评论(0编辑  收藏  举报