每一次在捕捉控制台输出之前,应该先把命令回显的地方清空比较好点
丢个汇编版本滴:
.386
.model flat, stdcall
option casemap :none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Equ 等值定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ICO_MAIN equ 1000h ;图标
DLG_CMD equ 911
IDC_CmdShell equ 9011
IDC_CreateCmdShell equ 9012
IDC_JieGuo equ 9013
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 数据段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data?
hInstance dd ?
szBuff db 1024 dup(?)
sat SECURITY_ATTRIBUTES<?>
startupinfo STARTUPINFO<?>
pinfo PROCESS_INFORMATION<?>
lpBuff db 1024 dup(?)
bytesRead DWORD ?
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ProcDlgCmd proc uses ebx edi esi hWnd,wMsg,wParam,lParam
local hRead,hWrite,hwndEdit
mov eax,wMsg
.if eax == WM_CLOSE
invoke EndDialog,hWnd,NULL
.elseif eax == WM_INITDIALOG
invoke LoadIcon,hInstance,ICO_MAIN
invoke SendMessage,hWnd,WM_SETICON,ICON_BIG,eax
.elseif eax == WM_COMMAND
mov eax,wParam
.if ax == IDC_CreateCmdShell
invoke RtlZeroMemory,addr szBuff,sizeof szBuff
invoke GetDlgItemText,hWnd,IDC_CmdShell,addr szBuff,sizeof szBuff
invoke GetDlgItem,hWnd,IDC_JieGuo
mov hwndEdit,eax
mov sat.nLength,sizeof SECURITY_ATTRIBUTES
mov sat.bInheritHandle,TRUE
mov sat.lpSecurityDescriptor,NULL
invoke CreatePipe,addr hRead,addr hWrite,addr sat,0
.if eax==NULL
mov eax,FALSE
ret
.endif
mov startupinfo.cb,sizeof STARTUPINFO
invoke GetStartupInfo,addr startupinfo
push hWrite
pop startupinfo.hStdOutput ;用管道的写端代替控制台程序的输出端以便得到输出的信息
push hWrite
pop startupinfo.hStdError
mov startupinfo.dwFlags,STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES
mov startupinfo.wShowWindow,SW_HIDE;
invoke CreateProcess,NULL,addr szBuff,NULL,NULL,TRUE,NULL,NULL,NULL,addr startupinfo,addr pinfo
.if eax==NULL
mov eax,FALSE
ret
.endif
invoke CloseHandle,hWrite
.while TRUE
invoke RtlZeroMemory,addr lpBuff,sizeof lpBuff
invoke ReadFile,hRead,addr lpBuff,1023,addr bytesRead,NULL
.if eax==NULL
ret
.endif
invoke SendMessage,hwndEdit,EM_SETSEL,-1,0
invoke SendMessage,hwndEdit,EM_REPLACESEL,FALSE,addr lpBuff
.endw
invoke CloseHandle,hWrite
.endif
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
_ProcDlgCmd endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,DLG_CMD,NULL,offset _ProcDlgCmd,NULL
invoke ExitProcess,NULL
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end start