WIN32汇编: 21.管道[2]

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 goddragon 的 blog


.386 
.model flat,stdcall 
option casemap:none 
include \masm32\include\windows.inc 
include \masm32\include\user32.inc 
include \masm32\include\kernel32.inc 
include \masm32\include\gdi32.inc 
includelib \masm32\lib\gdi32.lib 
includelib \masm32\lib\user32.lib 
includelib \masm32\lib\kernel32.lib 
winmain proto :dword,:dword,:dword,:dword 

.const 
idr_mainmenu equ 101         ; the id of the main menu 
idm_assemble equ 40001 

.data 
classname            db "pipewinclass",0 
appname              db "one-way pipe example",0 editclass db "edit",0 
createpipeerror     db "error during pipe creation",0 
createprocesserror     db "error during process creation",0 
commandline     db "ml /c /coff /cp test.asm",0 

.data? 
hinstance hinstance ? 
hwndedit dd ? 

.code 
start: 
    invoke getmodulehandle, null 
    mov hinstance,eax 
    invoke winmain, hinstance,null,null, sw_showdefault 
    invoke exitprocess,eax 

winmain proc hinst:dword,hprevinst:dword,cmdline:dword,cmdshow:dword 
    local wc:wndclassex 
    local msg:msg 
    local hwnd:hwnd 
    mov wc.cbsize,sizeof wndclassex 
    mov wc.style, cs_hredraw or cs_vredraw mov wc.lpfnwndproc, offset wndproc 
    mov wc.cbclsextra,null 
    mov wc.cbwndextra,null 
    push hinst 
    pop wc.hinstance 
    mov wc.hbrbackground,color_appworkspace 
    mov wc.lpszmenuname,idr_mainmenu 
    mov wc.lpszclassname,offset classname 
    invoke loadicon,null,idi_application 
    mov wc.hicon,eax 
    mov wc.hiconsm,eax 
    invoke loadcursor,null,idc_arrow 
    mov wc.hcursor,eax 
    invoke registerclassex, addr wc 
    invoke createwindowex,ws_ex_clientedge,addr classname,addr appname,\ ws_overlappedwindow+ws_visible,cw_usedefault,\ cw_usedefault,400,200,null,null,\ hinst,null 
    mov hwnd,eax 
    .while true 
        invoke getmessage, addr msg,null,0,0 
        .break .if (!eax) 
        invoke translatemessage, addr msg 
        invoke dispatchmessage, addr msg 
    .endw 
    mov eax,msg.wparam 
    ret 
winmain endp 

wndproc proc hwnd:hwnd, umsg:uint, wparam:wparam, lparam:lparam 
    local rect:rect 
    local hread:dword 
    local hwrite:dword 
    local startupinfo:startupinfo 
    local pinfo:process_information 
    local buffer[1024]:byte 
    local bytesread:dword 
    local hdc:dword 
    local sat:security_attributes 
    .if umsg==wm_create 
        invoke createwindowex,null,addr editclass, null, ws_child+ ws_visible+ es_multiline+ es_autohscroll+ es_autovscroll, 0, 0, 0, 0, hwnd, null, hinstance, null 
        mov hwndedit,eax 
    .elseif umsg==wm_ctlcoloredit 
        invoke settextcolor,wparam,yellow 
        invoke setbkcolor,wparam,black 
       invoke getstockobject,black_brush 
        ret 
    .elseif umsg==wm_size 
        mov edx,lparam 
        mov ecx,edx 
        shr ecx,16 
        and edx,0ffffh 
        invoke movewindow,hwndedit,0,0,edx,ecx,true 
    .elseif umsg==wm_command 
       .if lparam==0 
            mov eax,wparam 
            .if ax==idm_assemble 
                mov sat.nilength,sizeof security_attributes 
                mov sat.lpsecuritydescriptor,null 
                mov sat.binherithandle,true 
                invoke createpipe,addr hread,addr hwrite,addr sat,null 
                .if eax==null 
                    invoke messagebox, hwnd, addr createpipeerror, addr appname, mb_iconerror+ mb_ok 
                .else 
                    mov startupinfo.cb,sizeof startupinfo 
                    invoke getstartupinfo,addr startupinfo 
                    mov eax, hwrite 
                    mov startupinfo.hstdoutput,eax 
                    mov startupinfo.hstderror,eax 
                    mov startupinfo.dwflags, startf_useshowwindow+ startf_usestdhandles 
                    mov startupinfo.wshowwindow,sw_hide 
                    invoke createprocess, null, addr commandline, null, null, true, null, null, null, addr startupinfo, addr pinfo 
                    .if eax==null 
                        invoke messagebox,hwnd,addr createprocesserror,addr         appname,mb_iconerror+mb_ok 
                    .else 
                        invoke closehandle,hwrite 
                        .while true 
                            invoke rtlzeromemory,addr buffer,1024 
                            invoke readfile,hread,addr buffer,1023,addr bytesread,null 
                            .if eax==null 
                                .break 
                            .endif 
                            invoke sendmessage,hwndedit,em_setsel,-1,0 
                            invoke sendmessage,hwndedit,em_replacesel,false,addr buffer 
                        .endw 
                    .endif 
                    invoke closehandle,hread 
                .endif 
            .endif 
        .endif 
    .elseif umsg==wm_destroy 
        invoke postquitmessage,null 
    .else

本文关键:asm
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top