c++ 实现远程CMD
#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;
}
int _tmain(int argc, _TCHAR* argv[])
{
char szCmd[] = "ping www.baidu.com";
char szResultBuffer[512] = { 0 };
DWORD dwResultBufferSize = 512;
// 执行 cmd 命令, 并获取执行结果数据
if (FALSE == PipeCmd(szCmd, szResultBuffer, dwResultBufferSize))
{
printf("pipe cmd error.\n");
}
else
{
printf("CMD执行结果为:\n%s\n", szResultBuffer);
}
system("pause");
return 0;
}