为什么readfile后程序自动退出?
程序思路:开一个管道,将cmd提示符的输出重定向到文本框中:
.386
.model flat, stdcall ;32 bit memory model
option casemap :none ;case sensitive
include pipe.inc
.data
errorpipe db 'error on create pipe!',0
errorpro db 'error on create process!',0
msg db 'msg',0
hRead HANDLE ?
hWrite HANDLE ?
cmdline db 'cmd.exe',0
buffer db 4096 dup(0)
bytesRead DWORD ?
.code
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke InitCommonControls
invoke DialogBoxParam,hInstance,IDD_DIALOG1,NULL,addr DlgProc,NULL
invoke ExitProcess,0
;########################################################################
DlgProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
LOCAL hedit:HWND
LOCAL sa:SECURITY_ATTRIBUTES
LOCAL asi:STARTUPINFO
LOCAL pi:PROCESS_INFORMATION
;4096 dup(0);[4096] = {0};
mov eax,uMsg
.if eax==WM_INITDIALOG
.elseif eax==WM_COMMAND
mov ebx,wParam
.if bx==IDC_BTN2
invoke SendMessage,hWin,WM_CLOSE,NULL,NULL
.elseif bx==IDC_BTN1
mov sa.nLength,sizeof(SECURITY_ATTRIBUTES)
mov sa.lpSecurityDescriptor,NULL;sa.lpSecurityDescriptor=NULL;
mov sa.bInheritHandle,TRUE;sa.bInheritHandle=TRUE;
invoke CreatePipe,offset hRead,offset hWrite,addr sa,0;CreatePipe(&hRead,&hWrite,&sa,0)
.if eax==FALSE
invoke MessageBox,hWin,offset errorpipe,offset msg,MB_OK
ret
.endif
mov asi.cb,sizeof(STARTUPINFO) ;si.cb=sizeof(STARTUPINFO)
invoke GetStartupInfo,addr asi ;GetStartupInfo(&si)
mov ecx,hWrite
mov asi.hStdError,ecx
mov asi.hStdOutput,ecx
mov ecx,SW_HIDE
mov dword ptr asi.wShowWindow,ecx
mov ecx,STARTF_USESHOWWINDOW
or ecx,STARTF_USESTDHANDLES
invoke CreateProcess,NULL,offset cmdline,NULL,NULL,TRUE,NULL,NULL,NULL,addr asi,addr pi
.if eax==FALSE
invoke MessageBox,hWin,offset errorpro,offset msg,MB_OK
ret
.endif
invoke CloseHandle,hWrite
.while TRUE
invoke ReadFile, hRead, buffer,4095,offset bytesRead,NULL ;问题所在
.if eax==NULL
.break
.endif
invoke GetDlgItem,hWin,IDC_EDT1 ; out put
mov hedit,eax
mov ebx,offset buffer
invoke SendMessage,hedit,WM_SETTEXT,0,ebx
invoke Sleep,200
.endw
.endif
.elseif eax==WM_CLOSE
invoke EndDialog,hWin,0
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
DlgProc endp
end start