WINDOWS自带的大量控制台工具,是从DOS继承过来的,应用广泛,功能强大.如果写程序能对其加以利用,有时能够收到事倍功半的效果.使用管道机制能很容易的实现这一点.当然也可以自己创建一个DOS工具,再利用管道机制做一个GUI,无疑也是很爽的.
下面以调用netstat程序为例利用管道创建一个应用.
其核心函数是下面这个函数,从一个网友那里学来的:
void CGetProcessDlg::function()
{
//使用下面三个句柄来对第三方程序做输入输出操作
HANDLE hSTDINWrite, hSTDINRead; // 用于重定向子进程输入的句柄
HANDLE hSTDOUTWrite, hSTDOUTRead; // 用于重定向子进程输出的句柄
HANDLE hSTDERRWrite, hSTDERRRead; // 用于重定向子进程输出错误的句柄
CString tmp;
SECURITY_ATTRIBUTES sa;
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof(sa);
// 创建子进程输出匿名管道,下面会通过 hSTDOUTRead来获取控制台输出的内容
if( !CreatePipe(&hSTDOUTRead, &hSTDOUTWrite, &sa, 0) )
{
AfxMessageBox("Create STDOUT pipe failed");
return;
}
// 创建子进程输入匿名管道
if( !CreatePipe(&hSTDINRead, &hSTDINWrite, &sa, 0) )
{
AfxMessageBox("Create STDIN pipe failed");
return;
}
//下面是创建进程,如果不使用管道,也是可以创建进程的,不过那样我们无法与进程交互
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
STARTUPINFO si;
GetStartupInfo(&si);
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW; //SW_HIDE; //SW_SHOW;
si.hStdInput = hSTDINRead; //重定向子进程输入
si.hStdOutput = hSTDOUTWrite; //重定向子进程输入
si.hStdError = hSTDOUTWrite; //GetStdHandle( STD_ERROR_HANDLE );
char cmd[256] = {0};
::strcpy(cmd, "netstat.exe -nao");
if( !::CreateProcess(NULL, cmd, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi) )
{
int errorNo = ::GetLastError();
AfxMessageBox("create process failed");
return;
}
::CloseHandle(hSTDOUTWrite);
::CloseHandle(hSTDINRead);
char strData[1024] = {0};
DWORD dwBytes;
int i = 0;
//由于是流,不知道其长短,也没有EOF结束符,所以只好分段来获取
while(::ReadFile(hSTDOUTRead, strData, sizeof(strData), &dwBytes, NULL))
{
tmp.Format(strData);
m_value += tmp;
}
m_value += '/r';
m_value += '/n';
DWORD uExitCode;
::TerminateProcess(pi.hProcess,uExitCode);
::WaitForSingleObject(pi.hProcess, INFINITE);
::CloseHandle(hSTDOUTRead);
::CloseHandle(hSTDINWrite);
::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
}
下面是结合上面函数添加对话框和EDIT控件后的完整程序.本人新学VC,高手就不要看了.
下面主要是创建了一个对话框,对话框的OK键改成执行键,对话框上加了一个EDIT控件,EDIT绑定变量m_value,用来输出获取的数据。EDIT控制属性要加上自动换行。
// getProcessDlg.cpp : implementation file
//
#include "stdafx.h"
#include "getProcess.h"
#include "getProcessDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CGetProcessDlg dialog
CGetProcessDlg::CGetProcessDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGetProcessDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGetProcessDlg)
m_value = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CGetProcessDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGetProcessDlg)
DDX_Text(pDX, IDC_EDIT1, m_value);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGetProcessDlg, CDialog)
//{{AFX_MSG_MAP(CGetProcessDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CGetProcessDlg message handlers
BOOL CGetProcessDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CGetProcessDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CGetProcessDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CGetProcessDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//这里是添加的一个方法,创建进程
void CGetProcessDlg::function()
{
HANDLE hSTDINWrite, hSTDINRead; // 用于重定向子进程输入的句柄
HANDLE hSTDOUTWrite, hSTDOUTRead; // 用于重定向子进程输出的句柄
HANDLE hSTDERRWrite, hSTDERRRead; // 用于重定向子进程输出的句柄
CString tmp;
SECURITY_ATTRIBUTES sa;
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof(sa);
// 创建子进程输出匿名管道
if( !CreatePipe(&hSTDOUTRead, &hSTDOUTWrite, &sa, 0) )
{
AfxMessageBox("Create STDOUT pipe failed");
return;
}
// 创建子进程输入匿名管道
if( !CreatePipe(&hSTDINRead, &hSTDINWrite, &sa, 0) )
{
AfxMessageBox("Create STDIN pipe failed");
return;
}
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
STARTUPINFO si;
GetStartupInfo(&si);
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW; //SW_HIDE; //SW_SHOW;
si.hStdInput = hSTDINRead; //重定向子进程输入
si.hStdOutput = hSTDOUTWrite; //重定向子进程输入
si.hStdError = hSTDOUTWrite; //GetStdHandle( STD_ERROR_HANDLE );
char cmd[256] = {0};
::strcpy(cmd, "netstat.exe -nao");
if( !::CreateProcess(NULL, cmd, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi) )
{
int errorNo = ::GetLastError();
AfxMessageBox("create process failed");
return;
}
::CloseHandle(hSTDOUTWrite);
::CloseHandle(hSTDINRead);
char strData[1024] = {0};
DWORD dwBytes;
int i = 0;
while(::ReadFile(hSTDOUTRead, strData, sizeof(strData), &dwBytes, NULL))
{
tmp.Format(strData);
m_value += tmp;
}
m_value += '/r';
m_value += '/n';
DWORD uExitCode;
::TerminateProcess(pi.hProcess,uExitCode);
::WaitForSingleObject(pi.hProcess, INFINITE);
::CloseHandle(hSTDOUTRead);
::CloseHandle(hSTDINWrite);
::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
}
void CGetProcessDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData(TRUE);
function();
UpdateData(FALSE);
//CDialog::OnOK();
}
void CGetProcessDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}