📄 demodlg.cpp
字号:
// DemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Demo.h"
#include "DemoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HANDLE hRead_OutPipe,hWrite_OutPipe;
HANDLE hRead_InPipe, hWrite_InPipe;
HANDLE hProcess=NULL;
/////////////////////////////////////////////////////////////////////////////
// CDemoDlg dialog
CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDemoDlg)
m_szAppPath = _T("");
m_szArgument = _T("");
m_szInput = _T("");
m_szOutput = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDemoDlg)
DDX_Text(pDX, IDC_EDIT1, m_szAppPath);
DDX_Text(pDX, IDC_EDIT2, m_szArgument);
DDX_Text(pDX, IDC_EDIT3, m_szInput);
DDX_Text(pDX, IDC_OUTPUT, m_szOutput);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
//{{AFX_MSG_MAP(CDemoDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnBrowser)
ON_BN_CLICKED(IDOK, OnLaunchApp)
ON_BN_CLICKED(IDC_BUTTON2, OnInput)
ON_BN_CLICKED(IDCANCEL, OnQuit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDemoDlg message handlers
BOOL CDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
GetDlgItem(IDOK)->EnableWindow(FALSE);
// 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
}
// 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 CDemoDlg::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 CDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDemoDlg::OnBrowser()
{
CFileDialog dlg(TRUE,_T("*.exe"),NULL,NULL,_T("exe Files(*.exe)|*.exe|com Files(*.com)|*.com||"));
if(dlg.DoModal()== IDOK)
{
this->m_szAppPath = dlg.GetPathName();
GetDlgItem(IDOK)->EnableWindow(TRUE);
UpdateData(FALSE);
}
// TODO: Add your control notification handler code here
}
void CDemoDlg::OnLaunchApp()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (! CreatePipe(&hRead_OutPipe, &hWrite_OutPipe, &sa, 0))
{
TRACE("out_pipe creation failed\n");
return ;
}
if (! CreatePipe(&hRead_InPipe, &hWrite_InPipe, &sa, 0))
{
TRACE("out_pipe creation failed\n");
return ;
}
TRACE("\n**************\ntwo pipe creation success\n******************\n");
ZeroMemory( &pi, sizeof(pi) );
ZeroMemory( &si, sizeof(si) );
GetStartupInfo(&si);
si.cb = sizeof(si);
si.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
si.hStdOutput=hWrite_OutPipe;
si.hStdError=hWrite_OutPipe;
si.hStdInput=hRead_InPipe;
si.wShowWindow=SW_HIDE;
TCHAR szCommandLine[1024];
GetDlgItemText(IDC_EDIT1,szCommandLine,sizeof(szCommandLine));
_tcscat(szCommandLine," ");
UpdateData(false);
_tcscat(szCommandLine,m_szArgument.GetBuffer(0));
// Start the child process.
if( !CreateProcess(
NULL,
(char*)szCommandLine,
NULL, // Thread handle not inheritable.
NULL,
TRUE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
TRACE( "CreateProcess failed.\n" );
return ;
}
GetDlgItem(IDOK)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE);
char buf[1024];
memset(buf,'\0',sizeof(buf));
DWORD readlen;
ReadFile(hRead_OutPipe,buf,sizeof(buf),&readlen,NULL);
//MessageBox(NUbLL,buf,NULL,MB_OK);
SetDlgItemText(IDC_OUTPUT,buf);
// Wait until child process exits.
TRACE( "CreateProcess ok. \n" );
// Close process and thread handles.
}
void CDemoDlg::OnInput()
{
char buf[1024];
DWORD readlen=0;
BOOL read_ret=1;
UpdateData(TRUE);
m_szInput+="\r\n";
DWORD dwWritten= 0;
WriteFile(hWrite_InPipe,m_szInput.GetBuffer(0),m_szInput.GetLength(),&dwWritten,0);
SetDlgItemText(IDC_OUTPUT,"");
memset(buf,'\0',sizeof(buf));
Sleep(100);
m_szInput.MakeLower();
if (m_szInput.Find("exit")>=0||m_szInput.Find("quit")>=0||m_szInput.Find("bye")>=0)
{
GetDlgItem(IDOK)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
SetDlgItemText(IDC_OUTPUT,"程序退出");
return;
}
read_ret =ReadFile(hRead_OutPipe,buf,sizeof(buf),&readlen,NULL);
//MessageBox(NUbLL,buf,NULL,MB_OK);
SetDlgItemText(IDC_OUTPUT,buf);
}
void CDemoDlg::OnQuit()
{
CloseHandle(hWrite_OutPipe);
CloseHandle(hRead_InPipe);
CloseHandle(hWrite_InPipe);
CloseHandle(hRead_OutPipe);
OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -