获取DOS窗口内容方法
1. 将输出转存到文件
- 先将结果保存到文件
dir c:\>d:\out.txt
- 然后程序读取指定文件内容
2. 通用句柄重定向
通过句柄重定向稍复杂一些,需要创建进程前先准备好输入、输出句柄
创建进程执行DOS程序
周期读取输入句柄的数量
示例代码:CString ExecuteExternalFile(CString csExeName, CString csArguments) { CString csExecute; csExecute=csExeName + " " + csArguments; SECURITY_ATTRIBUTES secattr; ZeroMemory(&secattr,sizeof(secattr)); secattr.nLength = sizeof(secattr); secattr.bInheritHandle = TRUE; HANDLE rPipe, wPipe; //Create pipes to write and read data CreatePipe(&rPipe,&wPipe,&secattr,0); // STARTUPINFO sInfo; ZeroMemory(&sInfo,sizeof(sInfo)); PROCESS_INFORMATION pInfo; ZeroMemory(&pInfo,sizeof(pInfo)); sInfo.cb=sizeof(sInfo); sInfo.dwFlags=STARTF_USESTDHANDLES; sInfo.hStdInput=NULL; sInfo.hStdOutput=wPipe; sInfo.hStdError=wPipe; char command[1024]; strcpy(command, csExecute.GetBuffer(csExecute.GetLength())); //Create the process here. CreateProcess(0 command,0,0,TRUE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo); CloseHandle(wPipe); //now read the output pipe here. char buf[100]; DWORD reDword; CString m_csOutput,csTemp; BOOL res; do { res=::ReadFile(rPipe,buf,100,&reDword,0); csTemp=buf; m_csOutput+=csTemp.Left(reDword); }while(res); return m_csOutput; }
参考网页:
3. 通过粘贴板
- 创建DOS进程获取进程ID
- 通过
EnumWindows
函数获取进程ID对应的HWND - 通过系统菜单发送
Ctrl+A
与Ctrl+C
拷贝数据到粘贴板
示例代码:
HWND g_hWnd = NULL;
BOOL CALLBACK _EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
DWORD dwProcessId(0);
GetWindowThreadProcessId(hwnd, &dwProcessId);
if ((DWORD)lParam == dwProcessId)
{
g_hWnd = hwnd;
return FALSE;
}
return TRUE;
}
CString GetConsoleText(HWND hWndConsole)
{
if (!IsWindow(hWndConsole))
{
return _T("");
}
CString strRet;
HMENU hSysMenu = GetSystemMenu(hWndConsole, FALSE);
if (NULL != hSysMenu)
{
TCHAR szMenuText[128];
int nItemCnt = GetMenuItemCount(hSysMenu);
for (int i = 0; i < nItemCnt; ++i)
{
if (GetMenuString(hSysMenu, i, szMenuText, _countof(szMenuText), MF_BYPOSITION))
{
if (_tcsstr(szMenuText, _T("编辑")) != NULL)
{
HMENU hEditMenu = GetSubMenu(hSysMenu, i);
//全选
::SendMessage(hWndConsole, WM_COMMAND, GetMenuItemID(hEditMenu, 3), 0);
//复制
::SendMessage(hWndConsole, WM_COMMAND, GetMenuItemID(hEditMenu, 1), 0);
if (!OpenClipboard(hWndConsole))
{
break;
}
HANDLE hClipboardData = GetClipboardData(CF_UNICODETEXT);
if (hClipboardData)
{
wchar_t *pchData = (wchar_t*)GlobalLock(hClipboardData);
if (pchData)
{
strRet = pchData;
GlobalUnlock(hClipboardData);
}
}
CloseClipboard();
break;
}
}
}
}
return strRet;
}
void CDlgTestDlg::OnBnClickedBtnExec()
{
//启动示例程序作为子进程
STARTUPINFO si = {sizeof si};
si.cb = sizeof(STARTUPINFO);
si.wShowWindow = SW_SHOW;
si.dwFlags = STARTF_USESHOWWINDOW;
PROCESS_INFORMATION pi;
CreateProcess(NULL, "cmd /k @echo off>nul & ftp -i -d -s:c:\\cmd.txt", NULL, NULL, TRUE, CREATE_NEW_PROCESS_GROUP , NULL, NULL, &si, &pi);
Sleep(5*1000);
EnumWindows(_EnumWindowsProc, (LPARAM)pi.dwProcessId);
//发送Ctrl_BREAK消息
if (AttachConsole(pi.dwProcessId))
{
GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pi.dwProcessId);
FreeConsole();
}
Sleep(500);//等等FTP终止输出完毕
SetDlgItemText(IDC_EDIT, GetConsoleText(g_hWnd));
TerminateProcess(pi.hProcess, 0);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
c:\cmd.txt
文件内容
open 127.0.0.1
download
download
get V8Test.zip
bye