Ch 03 ø»s¦r¦ê»P±µ¨ü«öÁä


³o¤@³¹ùØ¡A¤p¤ì°¸±N¼¶¼g¤@­Óµ{¦¡¡ADRAWTXT.ASM¡A°õ¦æ¦¹µ{¦¡®É·|²£¥Í¤@­Óµøµ¡¡A°²¦p¨Ï¥ÎªÌ«ö¤U¤@­ÓÁä®É¡A¥¦·|§â¨Ï¥ÎªÌ©Ò«ö¤UÁä©Ò¥Nªíªº ASCII ¦r¤¸Åã¥Ü¦bµøµ¡ùØ¡C¨Ò¦p¡A±z«ö¤U¡yP¡zÁä¡A¨º»ò¥¦·|Åã¥Ü¡G

«öÁä»P¦^À³


­ì²z

GDI »P DC ( ³]³Æ¤º®e )

GDI ¬O Windows ¨t²Î¥Î¨Ó§@¬°À³¥Îµ{¦¡»P¿Ã¹õ¡B¦Lªí¾÷¤§¶¡ªº¬É­±¡AºÙ¬°¹Ï§Î¸Ë¸m¬É­± ( graphics device interface¡AÁY¼g¬° GDI )¡C³o­Ó¬É­±¤@ºÝ³s±µ¿Ã¹õ¡B¦Lªí¾÷µ¥µwÅé©PÃä³]³Æ¡A¥t¤@ºÝ³s±µµÛ§Ú­Ì©Ò¼¶¼gªºÀ³¥Îµ{¦¡¡C

ÁöµM¨C¤@®a¼t°Ó©Ò¥Í²£ªº²£«~¤£¦P¡A¨äÅX°Êµ{¦¡¤]¤£¤@¼Ë¡A¤£¹L¨C®a¼t°ÓªºÅX°Êµ{¦¡³£¯àª½±µ¨Ï³o¨Ç²£«~¥¿±`¤u§@¡C¦P®É³o¨ÇÅX°Êµ{¦¡¤]¥²¶·²Å¦X GDI ©Ò³W©wªº³W½d¡C¥t¤@¤è­±¡AÀ³¥Îµ{¦¡¨Ï¥Î GDI ¨ç¦¡¤]¥²¶·¿í¦u¤@©wªº³W«h¡A¤]´N¬O«ü¿í¦u¦U°Ñ¼Æªº³W©w¤Îµy«á´£¨ìªº³W«h¡C

´«¥y¸Ü»¡¡A¥u­n§Ú­Ì©Ò¼¶¼gªºµ{¦¡¿í¦u GDI ªº³W«h¡A´N¯à«Ü²³æªº¦b¿Ã¹õ©Î¦Lªí¾÷Åã¥Ü¥X¹Ï§Î¨Ó¡C¥t¤@¤è­±¡AGDI ¦P®É¤]­n¨DµwÅé¼t°Ó©Ò´£¨ÑªºÅX°Êµ{¦¡¤]¥²¶·¿í¦u¤@©wªº³W½d¡A³o¼Ëµ{¦¡³]­p®v¤£¥²¥hÃö¤ß¨Ï¥ÎªÌ¬O¨Ï¥Î¨º¤@®a¼t°Óªº¿Ã¹õ¡B¦Lªí¾÷¡C©Ò¥H GDI ¬O¤@­Ó»PµwÅéµLÃöªºµ{¦¡¬É­±¡C

Windows ¨t²Î¬O¤@­Ó¦h¤u¨t²Î¡A¦P¤@®É¶¡ùØ¡A¥i¯à¦³¦n´X­Óµ{¦¡¦@¥Î¤@­Ó¿Ã¹õ©Î¦Lªí¾÷¡A©Ò¥H·íµ{¦¡¦V¿Ã¹õ¡B¦Lªí¾÷¿é¥X®É¡AGDI ÁÙ¥²¶·°µ¨ó½Õ¤u§@§_«h¥²·|²£¥Í¤j¶Ã¡CGDI ªº°µªk¬O¨Ï³o¨Çµ{¦¡¥ý¿é¥X¨ì¤@­ÓµêÀÀ¸Ë¸m¡A³o­ÓµêÀÀ¸Ë¸m´N¬O³]³Æ¤º®e( device context¡AÁY¼g¬° DC )¡A¦Ó¨C¤@­ÓÀ³¥Îµ{¦¡­n¿é¥X¸ê®Æ¨ì¿Ã¹õ©Î¦Lªí¾÷®É¥²¶·¦V Windows ¨t²Î¨ú±o³\¥i¡A°²¦p¦¨¥\ªº¸Ü¨t²Î«K·|¶Ç¦^¤@­Ó³]³Æ¤º®e¥N½X¡C

³]³Æ¤º®e¬O¤@­Ó«Ü¤jªº¸ê®Æµ²ºc¡A¥]§t¤F¤@²Õ¹Ï§Îª«¥ó ( graphic objects¡A¹Ï§Îª«¥ó¥]§tµ§¡Bµ§¨êµ¥µ¥¡A±z¥i¥H§â¥¦­Ì·Q¹³¦¨¬O¤@²Õµ{¦¡¡A­t³dµe¥XÂI½u¼g¥X¦r¡B¶ñ¤WÃC¦âªº¤u¨ãµ¥µ¥ ) ¥H¤Î¬ÛÃöÄÝ©Ê¡A´«¥y¸Ü»¡¡A¹ïÅã¥Ü¾¹¨ÓÁ¿¡A±z¥i¥H§â³]³Æ¤º®e·Q¹³¦¨Àx¦s³o­Óµøµ¡ùتº¹Ï§Î¤º®e¤Îµe¥X¹Ï§Îªºµ{¦¡¡C

¸Õ·Q¡A¤@­Ó 800*600 ªºµøµ¡¦³¦h¤Ö¸ê®Æ¡A°²¦p¦P®É¶}±Ò¦n´X­Óµøµ¡¡A¨º»ò©Ò®ø¯Óªº¸ê·½¥²©w«Ü¥iÆ[¡C¦]¬°³]³Æ¤º®e©Ò¦û¥ÎªºªÅ¶¡¤Î¸ê·½¬O¦p¦¹¥¨¤j¡A©Ò¥HÀ³¥Îµ{¦¡¦¬¨ì­n¿é¥X¸ê®Æ¨ì¿Ã¹õªº°T®§ ( §Y WM_PAINT¡Aµy«á»¡©ú ) «á¡A¶}±Ò¤@­Ó³]³Æ¤º®e¡A·í§¹¦¨«á¡A¥²¶·¦b¦^¨ì°T®§´`Àô¤§«eÄÀ©ñ³o­Ó³]³Æ¤º®e¡C

¦b Windows §@·~¨t²Î¤U¡AÅã¥Ü¤@­Ó¦r¦ê¡A¨Ã¤£¹³¦b DOS ¤U³o¼Ë²³æ¡C­º¥ý DOS ¬O¤å¦r¼Ò¦¡¡Aµ{¦¡¬O¥HÃþ¦ü¥´¦r¾÷ªº¤è¦¡¡A¤@­Ó¦r¤@­Ó¦r¦L¥X¨Óªº¡F¦Ó Windows ¨t²Î¬O¦b¹Ï§Î¼Ò¦¡¡A¨C­Ó¦r¬O¥Ñ³\¦hªºÂI²Õ¦¨¡A¬O¤@ÂI¤@ÂI´yø¡C©¯¦n³o­Ó²Ó¸`¬O¥Ñ¨t²ÎÀ°§Ú­Ì§¹¦¨¡A·íµM±z¦pªG·Q±±¨î¨C¤@­ÓÂI¡A¤]¦³ API ¥i¥HÀ°±z¹F¦¨¥Øªº¡C

²Ä¤G¡ADOS ¨t²Î¬O¤@¦¸¥u¦³¤@­Óµ{¦¡°õ¦æ¡A¿Ã¹õ³Q³o­Óµ{¦¡¿W¦û¡A·í­n¦L¥X¦r¦ê®É¤£¥Î¦Ò¼{¨ä¥Lµ{¦¡¡A¦ý¬O Windows ¨t²Î¬O¦h¤u§@·~¨t²Î¡A¦b¿Ã¹õ¤W¥i¯à¦P®ÉÅã¥Ü³\¦hµøµ¡¡A³o¨Çµøµ¡¦@¥Î¤@­Ó¿Ã¹õ¡A©Ò¥H·í¬Y¤@­Óµ{¦¡­nÅã¥Ü¦r¦ê®É¡A¥²¶·¦Ò¼{³o­Óµ{¦¡ªºµøµ¡¬O§_³Q¨ä¥Lµøµ¡¾B»\¡A°²¦p³Q¨ä¥Lµøµ¡¾B»\¬O§_¶È¾B»\³¡¥÷¦Ó¥uÅã¥Ü³¡¥÷¦r¦êµ¥µ¥¡C

WM_PAINT °T®§»PµL®Ä°Ï°ì ( invalid rectangle )

Ãö©ó²Ä¤G­Ó°ÝÃD¡AGDI ¬O§Q¥Î­«Ã¸ªº¤è¦¡¨Ó¸Ñ¨M¡C¨C·í¨Ï¥ÎªÌ§â¤@­Óµøµ¡²¾¨ì¥t¤@­Óµøµ¡¤W­±¡A©ÎªÌ§ïÅܵøµ¡ªº¤j¤p ( ¥]¬AÁY¨ì³Ì¤p©Î©ñ¨ì³Ì¤j¡B©ÎªÌ§ïÅܵøµ¡ªø«×¡B¼e«× )¡A©ÎªÌ«ö¤U¤u¨ã¦Cªº¿ï³æ¡A©ÎªÌ·Æ¹«´å¼Ð²¾°Ê¬ï¹L¤u§@°Ï¡A¥Î±²¶b±²°Ê¤u§@°Ï®Éµ¥µ¥¡A³o¨Ç±¡§Îµo¥Í®É Windows ¨t²Î³£¯à¹îı¡A¨Ã¥Bµo¥X¤@­Ó WM_PAINT µ¹¸Óµøµ¡¡A§i¶D¸Óµøµ¡»Ý­n­«·sø»s¤F¡C·íµMµ{¦¡¤]¥i¥H¹ï¦Û¤vµo¥X WM_PAINT °T®§¡A±j­¢¦Û¤v­«Ã¸¡A¨Ò¦p©I¥s InvalidateRect API¡C¨ä¥LÁÙ¦³¤@¨Ç API ¤]·|²£¥Í WM_PAINT¡A¹³ UpdateWindow¡A³q±`µøµ¡ªº²Ä¤@­Ó WM_PAINT °T®§¬O UpdateWindow µo¥Xªº¡C

·íµøµ¡¨ç¦¡¦¬¨ì WM_PAINT ®É¡A¥²¶·­«·sø»sµøµ¡¡A¦ý¦³®É§Ú­Ì¨Ã¤£§Æ±æ­«Ã¸¾ã­Óµøµ¡¡A¦]¬°¨º¼Ë¤Ó®ø¯Ó¸ê·½¤S¤Ó®ö¶O®É¶¡¤F¡C©Ò¥H Windows ¥u­«·sø»s³¡¥÷³Q¾B»\©ÎªÌ»Ý­n­«·sø»sªº³¡¥÷´N¥i¥H¤F¡A³o­Ó°Ï°ìºÙ¬°¡yµL®Ä°Ï°ì¡z¡C¨º»òµL®Ä°Ï°ì¦³¦h¤j¤S¦ì©ó¤u§@°Ïªº¨ºùØ©O¡H³o­Ó°ÝÃD«Ü½ÆÂø¡A¦]¬°¦³³\¦h¦UºØ¤£¦Pªº±¡ªp¡A¦ý¬O³o¨Ç³£¥æ¥Ñ¨t²Î¸Ñ¨M¤F¡C·í¨t²Îµo¥X WM_PAINT «á³Qµøµ¡¨ç¦¡©Ò³B²z®É¡A¤@¯ë¦Ó¨¥¬O¥ý©I¥s BeginPaint API ¨ç¼Æ¡A¦¹¨ç¼Æ·|¶Ç¦^¤@­Óµ²ºcÅé¡A¦¹µ²ºcÅé§t¦³µL®Ä°Ï°ì¡C©ÎªÌ¤]¥i¥H©I¥s GetUpdateRect API ª½±µ¨ú±o¦¹µøµ¡ªºµL®Ä°Ï°ì¡C

·íµ{¦¡©I¥s BeginPaint API ®É¡ABeginPaint ·|§â¾ã­Ó¤u§@°Ï³]¬°¦³®Ä¡C°²¦p±z¤£·Q³B²z WM_PAINT °T®§¡A¨º»ò´N¥æ¥Ñ DefWindowProc ³B²z¡A¤d¸U¤£¯à¼g¦¨

        cmp     uMsg,WM_PAINT
        jnz     next_WM1
        jmp     exit
        ¡K¡K¡K
next_WM1:                       ;¨ä¥L°T®§

exit:   xor     eax,eax
        ret

³o¼Ëªí¥Ü§AÁöµM³B²z WM_PAINT¡A¦ý¹ê»Ú¤W¤u§@°Ï¤´¦³µL®Ä°Ï°ì¡A©Ò¥H¨t²Î·|µo²{³o­ÓµL®Ä°Ï°ì¥¼³B²z¡A©ó¬O¦A°e¤@¦¸ WM_PAINT¡A¦ýµ{¦¡¤´µLªk§âµL®Ä°Ï°ì²M°£¡A¨t²Î·|¤@ª½¹ï§Aªºµøµ¡µo¥X WM_PAINT ªº°T®§¡C

«öÁä¡GWM_CHAR °T®§

¦b¿Ã¹õ¤W¦P¤@®É¶¡ùØ¡A¥i¯à¦³¦n´X­Óµøµ¡¡A¥i¬O¤@¯ë¹q¸£¥u¦³¤@­ÓÁä½L¡A©Ò¥H¥u¦³¥¿¦b¨Ï¥Îªºµøµ¡¯à°÷±µ¦¬¨ìÁä½Lªº«öÁä¡C©Ò¿×¥¿¦b¨Ï¥Îªºµøµ¡¡A¨ä¹ê¬O«Ü®e©ö¤À¿ëªº¡A¥¦ªº¼ÐÃDÄæ¬O°ª«G«×ªº¡A©ÎªÌ±z¤]¥i¥H»¡¬O¿Ã¹õ³Ì¤W¼hªºµøµ¡¡C·í¨Ï¥ÎªÌ«ö¤UÁä½L¤¤ªº¤@­ÓÁä®É¡A¨t²Î·|¤À¿ë¨Ï¥ÎªÌ©Ò«ö¤UÁä¬O¤@¯ë«öÁäÁÙ¬O¨t²Î«öÁä¡A©Ò¿×¨t²Î«öÁä¬O«ü¹³ F10 Áä¡BAlt Áäµ¥¡A¦¹®É¨t²Î·|µo¥X WM_SYSKEYDOWN¡A©ñ¶}¨t²ÎÁä®É¡A·|µo¥X WM_SYSKEYUP °T®§¡C¦ý°£«D¦³¯S§Oªº¥Øªº¡A¤@¯ë«Ü¤Ö³B²z¨t²Î«öÁä¡A¦Ó³o­Óµ{¦¡§Ú­Ì¥u³B²z¤@¯ë«öÁä¡C

°²¦p¨Ï¥ÎªÌ«ö¤U¤@¯ë«öÁ䪺¸Ü¡A¨t²Î·|µo¥X WM_KEYDOWN °T®§µ¹¥¿¦b¨Ï¥Îªºµøµ¡¡A·í¨Ï¥ÎªÌ©ñ¶}¸ÓÁä®É¡A¨t²Î·|µo¥X WM_KEYUP °T®§¡AWM_KEYDOWN¡BWM_KEYUP °T®§¤¤ªº wParam ¤¤¦s¦³µêÀÀÁä½X ( Virtual-Key Codes )¡C©Ò¿×µêÀÀÁä½X¬O Windows ¨t²Î¤º©Ò©w¸qªº¡AÁä½L¤Wªº¨C­Ó«öÁä³£¦³¿W¤@µL¤Gªº¼Æ­È¡A§Y¬°µêÀÀÁä½X¡C´«¥y¸Ü»¡¡AWindows ´N¬O¾aµêÀÀÁä½X¨Ó§PÂ_¨Ï¥ÎªÌ«ö¤U¨º¤@­ÓÁä¡A©Î©ñ¶}¨º¤@­ÓÁä¡C­^¤å¦r¥À«öÁ䪺µêÀÀÁä½X¨ä¹ê´N¬O­^¤å¤j¼gªº ASCII ½X¡AÁä½L¤W¤èªº¼Æ¦rÁ䪺µêÀÀÁä½X´N¬Oªü©Ô§B¼Æ¦rªº ASCII ½X¡A¦Ü©ó¨ä¥L«öÁä¥i¥H°Ñ¦Ò Win32 Programmer's Reference¡C

ÁöµM Windows ½á¤©¨C­ÓÁä¿W¤@µL¤GªºµêÀÀÁä½X¡A¦ý¬OµêÀÀÁä½X¨Ã¥¼¦Ò¼{¨Ï¥ÎªÌ¿é¤J¤j¤p¼gªº±¡§Î¡C¨Ò¦p¡A¦pªG¨Ï¥ÎªÌ¦b CapsLock ¿Oº¶·Àªº±¡§Î¤U«ö¤U¡yA¡zÁä¡Aªí¥Ü¿é¤Jªº¬O¡ya¡z¦r¤¸¡A¦ý¬O¦b CapsLock ¿O«Gªº±¡§Î¤U«ö¤U¡yA¡zÁä©Î¬O¦b CapsLock º¶·À¤U¡A¦P®É«ö¤U Shift Áä»P¡yA¡zÁä¡Aªí¥Ü¿é¤Jªº¬O¡yA¡z¦r¤¸¡C³o¨âºØ±¡§Î¡A¦b¨t²Î¤º³¡³£¬Oªí¥Ü¨Ï¥ÎªÌ«ö¤U¡yA¡zÁä¡A¤£ºÞ¬O§_¦P®É«ö¤U Shift Áä¡A¤]¤£ºÞ CapsLock ¿O¸¹¬O§_«GµÛ¡AWM_KEYDOWN ©Î WM_KEYUP ªº wParam ³£·|¶Ç¨Ó¡yA¡zÁ䪺µêÀÀÁä½X¡A©Ò¥Hª½±µ³B²z WM_KEYDOWN ©M WM_KEYUP ÁÙ±o°Ñ¦Ò¨ä¥L¸ê®Æ¤~¯àª¾¹D±z«öªº¬O¤j¼g©Î¤p¼g¡C

¦ý¥u¦b¯S®í±¡§Î®É¡A§Ú­Ì¤~ª½±µ³B²z WM_KEYDOWN ©M WM_KEYUP¡A¤@¯ë§Ú­Ì¥i¥H¸g¥Ñ TranslateMessage API §â WM_KEYDOWN »P WM_KEYUP °T®§Â½Ä¶¦¨ WM_CHAR °T®§¡ATranslateMesage ·|¦Û°Ê°t¦X CapsLock »P Shift Á䪺ª¬ªp¥h§PÂ_«öÁä¬O¤j¼g©Î¤p¼g¡AµM«á¦s¦b WM_CHAR ùØ¡A³o¼Ë¦b WM_CHAR ùØ­±ªº wParam ´N¬O±z«öÁä®É­Ôªºª¬ªp¬O¤j¼g©Î¤p¼g¡C


­ì©lµ{¦¡

        .386
        .model  flat,stdcall
        option  casemap:none

include         windows.inc
include         user32.inc
include         kernel32.inc 
includelib      user32.lib
includelib      kernel32.lib

WndProc         proto   :HWND,:UINT,:WPARAM,:LPARAM
DrawStr         proto   :DWORD,:DWORD           ;12 «Å§i DrawStr ¨ç¦¡­ì«¬

        .DATA
ClassName       db      'SimpleWinClass',0
AppName         db      '«öÁä»P¦^À³',0          ;16 ¼ÐÃDÄ檺¦r¦ê
MyText          db      '±z«ö¤U¤F   Áä¡C',0     ;17 ±N©ó¤u§@°Ïø»sªº¦r¦ê
hInstance       HINSTANCE       ?
hwnd            HWND            ?
CommandLine     LPSTR           ?
wc      WNDCLASSEX      <30h,?,?,0,0,?,?,?,?,0,offset ClassName,?>
msg     MSG             <?>

        .CODE
start:  invoke  GetModuleHandle,NULL
        mov     hInstance,eax
        invoke  GetCommandLine
        mov     wc.style,CS_HREDRAW or CS_VREDRAW
        mov     wc.lpfnWndProc,offset WndProc
        mov     eax,hInstance
        mov     wc.hInstance,eax
        mov     wc.hbrBackground,COLOR_WINDOW+1
        invoke  LoadIcon,NULL,IDI_APPLICATION
        mov     wc.hIcon,eax
        mov     wc.hIconSm,eax
        invoke  LoadCursor,NULL,IDC_ARROW
        mov     wc.hCursor,eax
        invoke  RegisterClassEx,offset wc
        invoke  CreateWindowEx,NULL,offset ClassName,offset AppName,\ 
                WS_OVERLAPPEDWINDOW,0,0,200,100,0,0,hInstance,NULL 
        mov     hwnd,eax
        invoke  ShowWindow,hwnd,SW_SHOWDEFAULT
        invoke  UpdateWindow,hwnd

gt_msg: invoke  GetMessage,offset msg,NULL,0,0
        or      eax,eax
        jz      wm_qut
        invoke  TranslateMessage,offset msg
        invoke  DispatchMessage,offset msg
        jmp     gt_msg
wm_qut: mov     eax,msg.wParam
        invoke  ExitProcess,eax

WndProc proc    hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
        cmp     uMsg,WM_PAINT           ;55 Àˬd¬O§_ WM_PAINT °T®§
        jne     not_paint               ;56 §_¡A¸õ¨ì²Ä 61 ¦æ
        mov     eax,offset MyText
        invoke  DrawStr,eax,hWnd        ;58 ©I¥s°Æµ{¦¡
        jmp     exit

not_paint:                              ;61
        cmp     uMsg,WM_CHAR            ;62 Àˬd¬O§_«ö¤UÁä
        jne     not_keyinput            ;63 §_¡A¸õ¨ì²Ä 70 ¦æ
        mov     eax,wParam              ;64 ±o¨ì«öÁä ASCII ¦r¤¸
        mov     edx,offset MyText+9     ;65 ¨ú±o¦r¦ê¤¤«öÁä¦r¤¸ªº¦ì§}
        mov     [edx],al                ;66 ¶ñ¤J ASCII ¦r¤¸
        invoke  InvalidateRect,hWnd,0,1 ;67 ³]©w¬°µL®Ä°Ï°ì
        jmp     exit

not_keyinput:                           ;70
        cmp     uMsg,WM_DESTROY
        jne     default
        invoke  PostQuitMessage,NULL
        jmp     exit                    ;74

default:
        invoke  DefWindowProc,hWnd,uMsg,wParam,lParam
        ret
exit:   xor     eax,eax
        ret                             ;80 ¦^¨ì¥Dµ{¦¡
WndProc endp

DrawStr proc    AddrSt:DWORD,hWin:DWORD ;83 DrawStr °Æµ{¦¡¶}©l
        LOCAL   hDevCont:HDC            ;84 °Ï°ìÅܼơA¦s DC ¥N½X
        LOCAL   PS:PAINTSTRUCT          ;85 °Ï°ìÅܼơA¦s
        LOCAL   rectagl:RECT            ;86
        invoke  BeginPaint,hWin,ADDR PS ;87
        mov     hDevCont,eax            ;88
        invoke  GetClientRect,hWin,ADDR rectagl ;89
        invoke  DrawText,hDevCont,AddrSt,-1,\   ;90
                ADDR rectagl,DT_SINGLELINE      ;91
        invoke  EndPaint,hWin,ADDR PS           ;92
        ret
DrawStr endp

end     start

µ{¦¡»¡©ú

³o­Óµ{¦¡©M¤W¤@³¹®t¤£¦h¡A¥u¬O¦bµøµ¡¨ç¦¡ ( window procedure ) ùئh¤F¨â­Ó°T®§¬O³o­Óµ{¦¡¸û¦³¿³½ì¦Ó­n³B²zªº¡A³o¨â­Ó°T®§¬O WM_CHAR ©M WM_PAINT¡C²Ä 55 ¦æ¨ì²Ä 59 ¦æÀˬd¬O§_»Ý­n­«Ã¸¡A­Y»Ý­n­«Ã¸«h°õ¦æ²Ä 58 ¦æ CALL DrawStr¡ADrawStr ¬O­«·sø»s¦r¦êªº°Æµ{¦¡¡A¨ä°Æµ{¦¡¦b²Ä 83 ¦æ¨ì²Ä 94 ¦æ¡C

²Ä 61 ¦æ¨ì²Ä 68 ¦æ¬OÀˬd¬O§_«ö¤UÁä¡A­Y¬°¬O«h³B²z¤§¡F²Ä 70 ¦æ¨ì²Ä 74 ¦æ«h¬O³B²z°h¥Xµ{¦¡ªº°T®§¡F²Ä 76 ¦æ¨ì²Ä 80 ¦æ¬O¤º©w³B²z°T®§ªº DefWindowProc ³¡¥÷¡C¦]¬°¤j³¡¤À³£©M¤W¤@³¹¬Û¦P¡A©Ò¥H¤p¤ì°¸¶È´N·s¼W©Î­«­nªº³¡¥÷»¡©ú¡C

µ{¦¡²Ä 62 ¦æÀˬd°T®§¬O§_¬° WM_CHAR¡A¦pªG¬Oªº¸Ü¡A¦³¨â¶µ¤u§@­n§¹¦¨¡C²Ä¤@¬O¨ú±o«öÁ䪺 ASCII ¦r¤¸¨Ã¶ñ¤J¦r¦êùØ¡A«Ý¤U¤@¦¸°T®§°j°é®É¦L¥X©ó¿Ã¹õ¡A³o¤u§@¥Ñµ{¦¡²Ä 64 ¨ì 66 ¦æ§¹¦¨¡C²Ä¤G¬O§âµøµ¡³]¬°µL®Ä°Ï¡A¥Ñµ{¦¡²Ä 67 ¦æªº InvalidateRect API §¹¦¨¡C

InvalidateRect API

³o­Ó API ¬O¥Î¨Ó³]©wµL®Ä°Ï°ìªº¡A¨ä¥Îªk¦p¤U¡G

BOOL InvalidateRect(
    HWND        hWnd,       // handle of window with changed update region
    CONST RECT  *lpRect,    // address of rectangle coordinates
    BOOL        bErase      // erase-background flag
   );

hWnd ¬O«ü­n³]©wµL®Ä°Ïªº¨º­Óµøµ¡ªºµøµ¡¥N½X¡AlpRect ¬O«ü¸Óµøµ¡¤¤­n³]©w¨º¤@¶ô°Ï°ì¬°µL®Ä°Ï°ì¡A lpRect ¬O¤@­Ó¦ì§}«ü¼Ð¡A¥¦«ü¦V¤@­ÓºÙ¬° RECT ªºµ²ºcÅé¡A¸Óµ²ºcÅé¬O«ü©w¤@¶ôªø¤è§Î°Ï°ì¡A´y­z¦p¤U¡G

RECT    struc
left    DD      ?       ;ªø¤è§Î¥ª¤W¨¤ªº x ®y¼Ð
top     DD      ?       ;ªø¤è§Î¥ª¤W¨¤ªº y ®y¼Ð
right   DD      ?       ;ªø¤è§Î¥k¤U¨¤ªº x ®y¼Ð
bottom  DD      ?       ;ªø¤è§Î¥k¤U¨¤ªº y ®y¼Ð
RECT    ends

lpRect ¤]¥i¥H¬O¹s¡A°²¦p¬O¹sªº¸Ü¡Aªí¥Ü¤u§@°Ïªº©Ò¦³½d³ò³£¬OµL®Ä°Ï°ì¡C³Ì«á¤@­Ó°Ñ¼Æ bErase ¬Oªí¥Ü¬O§_²M°£­I´º¡A¹sªº¸Üªí¥Ü¤£²M°£¡A«D¹sªí¥Ü­n²M°£­I´º¡A¦b©I¥s BeginPaint ®É¡A­I´º´N·|³Q²M°£¡CInvalidateRect ¦³¶Ç¦^­È¡A¦pªG³]©w¦¨¥\¶Ç¦^­È¬°«D¹s¡A¥¢±Ñ¶Ç¦^­È¬°¹s¡C

·íµ{¦¡³]©w¤@¶ôµL®Ä°Ï°ì®É¡A¨t²Î·|¹îı¦Óµo¥X¤@­Ó WM_PAINT °T®§¶ñ¤J¸Óµøµ¡ªº°T®§¦î¦CùØ¡Aµ¥«Ý¸Óµøµ¡ªº°T®§°j°é´£¨ú¡C¦Ó«áµ{¦¡Ä~Äò°õ¦æ¡A¸õ¨ì²Ä 79 ¦æªð¦^ Windows ¨t²Î¡Aµ¥¨ì°õ¦æ¤U¤@¦¸ªº°T®§´`Àô®É´N¯à¦¬¨ì WM_PAINT °T®§¡AµM«á¸g¥Ñ¦^©I¾÷¨î¦A¦¸¶i¤Jµøµ¡¨ç¦¡¡A¦bµ{¦¡²Ä 59 ¦æÀˬd¨ì°T®§¬O WM_PAINT ®É¡A´N°õ¦æ DrawStr °Æµ{¦¡¡C

©Ò¥HÅo¡A¦b¦¬¨ì WM_PAINT ¤§«á¤£¥i¦A©I¥s InvalidateRect¡A³o¼Ë·|¤£Â_¦a²£¥Í WM_PAINT °T®§¡A¥H­P©ó¶^¤J¤@­ÓµL½a°j°é¡C

DrawStr °Æµ{¦¡

·í§Ú­Ìªºµ{¦¡¦¬¨ì WM_PAINT ®É¡Aªí¥Üµøµ¡­n­«·sø»s¡A³o¬qµ{¦¡³£¶°¤¤¦b DrawStr °Æµ{¦¡ùØ¡CDrawStr ªº­ì«¬«Å§i¦b²Ä 12 ¦æ¡A²Ä¤@­Ó°Ñ¼Æ¬O­n¦L¥Xªº¦r¦ê¦ì§}¡A²Ä¤G­Ó°Ñ¼Æ¬Oµøµ¡¥N½X¡C©³¤U¨Ó¤ÀªR DrawStr °Æµ{¦¡¡C

«e¤T¦æ¬O©w¸q¤T­Ó°Ï°ìÅܼơA³o¤T­Ó°Ï°ìÅܼƬO hDevCont¡BPS¡Brectagl¡A¥¦­Ì¬O¦b DrawStr °Æµ{¦¡¤¤·|¨Ï¥Î¨ìªºÅܼơA¦³Ãö°Ï°ìÅܼƪºÆ[©À½Ð¬Ý©³¤U LOCAL °²«ü¥Oªº»¡©ú¡C

LOCAL °²«ü¥O

LOCAL ¬O¥Î¨Ó¦b°Æµ{¦¡ùØ©w¸qÅܼƪº°²«ü¥O¡A©Ò¿×¦b°Æµ{¦¡ùØ©w¸qªºÅܼƬO«ü³o­ÓÅܼƶȦb°Æµ{¦¡¤¤¥i¥H¨Ï¥Î¥¦¡A¦b°Æµ{¦¡¥H¥~«h¤£¯à¨Ï¥Î¡A§_«h·|¥X²{

error A2006: undefined symbol : ÅܼƦW

ªº¿ù»~¡C¹³³oºØ¥u¦³¦b°Æµ{¦¡½d³ò¤º¤~¥i¥H¨Ï¥ÎªºÅܼƺ٤§¬°¡y°Ï°ìÅܼơz¡A»P°Ï°ìÅܼƬ۹諸ºÙ¬°¡y¥þ°ìÅܼơz¡A¥þ°ìÅܼƬO¾ã­Óµ{¦¡¤¤³£¥i¥H¦s¨úªº¡C¦Ü©ó¬°¦ó¥u¯à°÷¦b°Æµ{¦¡ªº½d³ò¨Ï¥Î¥H¤Î¬°¦ó¨Ï¥Î°Ï°ìÅܼƪº²z¥Ñ¡A¤p¤ì°¸µy«á¦A»¡©ú¡C¦]¬°¦b°Æµ{¦¡¥~ªº°Ï°ìÅܼƬOµL®Äªº¡A©Ò¥H¤£¦P°Æµ{¦¡ªº°Ï°ìÅܼƦWºÙ¥i¥H¬O¬Û¦Pªº¡C²{¦b¥ý¨Ó¬Ý¥¦ªº»yªk¡G

LOCAL   ÅܼƦW[­«½Æ¦¸¼Æ]:¸ê®Æ«¬ºA

­«½Æ¦¸¼Æ¦³ÂI¹³ dup¡A¥u¤£¹L dup ¬O¥Î¦b©w¸q¥þ°ìÅܼơA°²¦p¤£­«½Æªº¸Ü¡A[­«½Æ¦¸¼Æ]¥i¬Ù²¤¡C¸ê®Æ«¬ºA¬O¥u³o­ÓÅܼƪºªø«×¡A¨Ò¦pÂù¦r²Õ¥Î DWORD¡A¦r²Õ¥Î WORD¡A¦ì¤¸²Õ¥Î BYTE¡A¤]¥i¥H¨Ï¥Îµ²ºcÅé¡CLOCAL °²«ü¥O¥u¯à©ñ¦b PROC «Å§i¤§«á¡A¨ä¥L«ü¥O¶}©l¤§«e¡C©³¤U¨Ó¬Ý¬Ý´X­Ó¨Ò¤l¡A°²¦p¦³­Óµ{¦¡¤ù¬q¼g¦¨

        .code
start:  invoke  MyProc
        invoke  ExitProcess,0
MyProc  proc
        local   p:dword
        local   q:dword
        local   r:word
        mov     p,01234567h
        mov     q,89abcdefh
        mov     r,400h
        ret
MyProc  endp

³o¼Ë¡A²ÕĶ«á¦A¥Î Soft-ICE ¸ü¤J«á«ö¤U F8 Áä¡AµM«áÆ[¹îµ{¦¡½Xªº¼Ë¤l¦p¤U¡G

00401000 E807000000     CALL   0040100C ¡÷©I¥s MyProc
00401005 6A00           PUSH   00
00401007 E81C000000     CALL   KERNEL32!ExitProcess
0040100C 55             PUSH   EBP      ¡÷MyProc ¶}©l³B
0040100D 8BEC           MOV    EBP,ESP
0040100F 83C4F4         ADD    ESP,-0C
00401012 C745FC67452301 MOV    DWORD PTR [EBP-04],01234567
00401019 C745F8EFCDAB89 MOV    DWORD PTR [EBP-08],89ABCDEF
00401020 66C745F60004   MOV    WORD PTR [EBP-0A],400
00401026 C9             LEAVE
00401027 C3             RET             ¡÷MyProc µ²§ô³B

§Ú­Ì¥ý§ä¨ì MyProc °Æµ{¦¡±q¦ì§} 40100C ¨ì 401027¡A¦A¤ñ¸û­ì©lµ{¦¡»P¾÷±ñ½X¡Aµo²{²ÕĶ¾¹·|¦b²ÕĶ®É§â LOCAL °²«ü¥Oªº±Ô­z¦AÃB¥~¥[¤W

PUSH    EBP
MOV     EBP,ESP
ADD     ESP,-XXXX

³o¤T±ø«ü¥O¡C«e¨â±ø«ü¥O¬O«O¦s EBP ¼È¦s¾¹¡A¦]¬°¦b°Æµ{¦¡ùØ¡AEBP ­n°µ¬°¦s¨ú°Ñ¼Æ¤§¥Î ( °Ñ¦Ò²Ä¤@³¹ªºµù¤C )¡C²Ä¤T±ø«ü¥O«h¬O¦b°ïÅ|¤¤¹w¯d¤@¨ÇªÅ¶¡¡Aµ¹°Ï°ìÅܼƦs©ñ¤§¥Î¡CESP «ü¦V°ïÅ|³Ì°ª¦ì§}ªº¦a¤è¡A¤§«á¦³¼Æ­È³Q±À¤J°ïÅ|¡AESP ´N´î¤Ö¡C°²¦p¨Ï ESP ´î¥h¬Y¤@¼Æ­È¡A´N·|¨Ï¤U¤@¦¸ªº PUSH ©Î CALL «ü¥O¡A¦s¤J¬Y¼Æ¨ì°ïÅ|®É¡A¦b¸û§C¦ì§}¦s¤J¦¹¼Æ¡C³o¼Ë¤@¨Ó¡A·|¨Ï°ïÅ|ªÅ¤U¤@¨ÇªÅ¶¡¡A³o¨ÇªÅ¶¡´Nµ¹°Ï°ìÅܼƨϥΡC·í­n¦s¨ú°Ï°ìÅܼƮɡA´N¾a EBP ¨Ó·í«ü¼Ð¡A¦¹¥~¡A¦b 80386 CPU ¬O¥H 32 ¦ì¤¸¨Ó§@¬°¦s¨ú³æ¦ì¡A¬GÁöµM¤W­z°Æµ{¦¡¥u¥Î¥h 10 ­Ó¦ì¤¸²Õ ( p¡Bq ¦U¦û 4 ­Ó¦ì¤¸²Õ¡Ar ¦û¨â­Ó¦ì¤¸²Õ ) ªº°Ï°ìÅܼơA¦ý¬O°ïÅ|ÁÙ¬O«O¯d¤F 12 ­Ó¦ì¤¸²Õªº¤j¤pµ¹°Ï°ìÅܼơAÁöµM®ö¶O¤F¦ý¬O«o¯à¥[§Ö³t«×¡C¦³Ãö LOCAL ªº¸Ô²Ó¹Lµ{½Ð¤Î°ïÅ|¾Þ§@½Ð°Ñ¦Ò¤U¹Ï¡C

°Ï°ìÅܼƻ¡©ú¤@
·í¶i¤J Soft-ICE «á«ö¤U²Ä¤@¦¸ F8 Áä«á¡A´N¥ß¨è°õ¦æ²Ä¤@­Ó«ü¥O¡ACALL MyProc¡A©Ò¥H°ïÅ|ùؤw¸g¦³¤@­Ó CALL MyProc «ü¥O¤U¤@­Ó«ü¥O ( PUSH 00 ) ªº¦ì§}¡A00401005¡A¦Ó ESP ¤§­ÈÅܬ° 0063FE38¡A¤]´N¬O«ü¦Vªð¦^¦ì§}¡A¦p¤W¹Ï¤@¡CµM«á¦A°õ¦æ¨â­Ó©R¥O¡A¥ý«O¦s EBP ¤§­È¡A¦A¨Ó¨Ï EBP ¤§­Èµ¥©ó ESP¡A©ó¬O ESP¡BEBP ³£µ¥©ó 0063FE34¡A¦Ó¸Ó¦ì§}¤§­È¡A0063FF78¡A¬O­ì¨Óªº EBP¡A¦p¹Ï¤T¡C±µ¤U¨Ó¬O°õ¦æ¡AADD ESP,¡Ð0C¡A³o­Ó«ü¥O¡A¬Û·í©ó¨Ï ESP ´î¤Ö 12 ªº·N«ä¡A©ó¬O¦b°ïÅ|ùØ«O¯d¤F 12 ­Ó¦ì¤¸²Õµ¹°Ï°ìÅܼƨϥΡA·í§Ú­Ì­n¦s¨ú³o¨Ç°Ï°ìÅܼƮɡA«K¥H EBP §@¬°°Ñ¦ÒÂI¡A¨Ò¦p²Ä¤@­ÓÅܼơAp¡A¥H [EBP¡Ð4] ¥N´À¡F²Ä¤G­ÓÅܼơAq¡A¥H [EBP¡Ð8]¡A¥N´À¡F²Ä¤T­ÓÅܼơAr¡A«h¥H [EBP¡Ð0AH] ¥N´À¡C«e¨âªÌ¦]¬° p¡Bq ªø«×¬OÂù¦r²Õ¡A¬G¹j 4 ­Ó¦ì¤¸²Õ¡F¦ý¬O r ªºªø«×¬°¦r²Õ¡A¬G¥u´î¤Ö¨â­Ó¦ì¤¸²Õ¡A©Ò¥H [EBP¡ÐA] ¦s r ¤§­È¡A[EBP¡Ð8] «h¤£¨Ï¥Î¡C¨£¹Ï¥|¡C

±µ¤U¨Óªº¤T­Ó«ü¥O¬O§â p¡Bq¡Br ¤T­Ó°Ï°ìÅܼƶñ¤J¹ïÀ³ªº°ïÅ|°Ï¡A¦p¤U¹Ï¤­¡B¤»¡B¤C¡C±µ¤U¨Ó¬O LEAVE «ü¥O¡ALEAVE ¥ý¨Ï ESP ¤§­Èµ¥©ó EBP¡A©ó¬O ESP ­Èµ¥©ó 0063FE34¡AµM«á¦A±q°ïÅ|¼u¥X¤@­ÓÂù¦r²Õ¦s©ó EBP¡A©ó¬O EBP «ì´_­ì­È¡A¦Ó ESP ¤]«ü¦V¦^¨ì¥Dµ{¦¡±N°õ¦æªº«ü¥O¦ì§}¡A¦p¤U¹Ï¤K¡A³o®É­ì¨Ó¦b°ïÅ|ªº°Ï°ìÅܼƴN³Q©ß±ó¤F¡C

°Ï°ìÅܼƻ¡©ú¤G
±µ¤U¨Ó¡A¬O RET «ü¥O¡A¥¦±q°ïÅ|¨ú¥X¥Dµ{¦¡¤U¤@­Ó­n°õ¦æªº¦ì§}¡A©ó¬Oµ{¦¡ªð¦^¥Dµ{¦¡¡A¦p¤W¹Ï¤E¡C©Ò¥H³o¼Ë¬Ý°_¨Ó¡A°Ï°ìÅܼƩM¶Ç»¼¸ê®Æ¨ì¨ç¦¡ªº°Ñ¼Æ¨ä¹ê¹B§@¤è¦¡®t¤£¦h ( °Ñ¦Ò²Ä¤@³¹µù¤C )¡A¥¦­Ì³£³QÀx¦s¦b°ïÅ|ùØ¡A¨Ã¥B·í°Æµ{¦¡©Î¨ç¦¡ªð¦^®É³£§Q¥Î LEAVE «ü¥O©ß±ó¥¦­Ì¡Cºî¦X²Ä¤@³¹µù¤C¤Î¤W­z»¡©ú¡A§Ú­Ì¥i¥Hª¾¹D¡A¦b Win32 ùØ­±ªº°ïÅ|®Ø¡A¬O¥Î¨ÓÀx¦s°Ñ¼Æ¡B°Ï°ìÅܼƤΪð¦^¦ì§}¤§¥Î¡C°Æµ{¦¡ªº²Ä¤@­Ó°Ñ¼Æ¥H [EBP¡Ï08H] ªí¥Ü¡B²Ä¤G­Ó°Ñ¼Æ¥H [EBP¡Ï0CH] ªí¥Ü¡B²Ä¤T­Ó°Ñ¼Æ¥H [EBP¡Ï10H] ªí¥Ü¡K¡K¡F¦Óªð¦^¦ì§}«hÀx¦s¦b [EBP¡Ï04H] ¤§³B¡F¦Ü©ó²Ä¤@­Ó°Ï°ìÅܼƥH [EBP¡Ð04H] ªí¥Ü¡B²Ä¤G­Ó°Ï°ìÅܼƥH [EBP¡Ð08H] ªí¥Ü¡K¡K¡C

¬°¦ó¨Ï¥Î°Ï°ìÅܼƩO¡H°Ï°ìÅܼƦ³¤@¨Ç­­¨î¡A¥¦¤£¯à¦A°Æµ{¦¡¥H¥~ªº¦a¤è¨Ï¥Î¡A³o¼Ë¤£¬Oµ¹µ{¦¡³\¦h­­¨î¶Ü¡H­ì¦]¬O³o¼Ëªº¡G·íµ{¦¡¤pªº®É­Ô¡A±zÀ³¸Ó¥i¥H¬Ý±o¥Xµ{¦¡¬O¦p¦ó¹B§@ªº¡A§Y¨Ï¤é«á­nºûÅ@¡A¤]¤£ºâ¤Ó§xÃø¡C¦ý¬O·í¦¨¬OÅܱoÃe¤j¥B½ÆÂø®É¡A­n«Ü§Ö¬Ý¥Xµ{¦¡¯ßµ¸´N¤£®e©ö¤F¡A³o®É¦³¨Çµ{¦¡³]­p®v´£¥X¤@­ÓÆ[©À¡A¥L­Ì§Æ±æ¯à°÷§â¨ã¦³¬Y¨Ç¥\¯àªºµ{¦¡¤ù¬q¼g¦¨°Æµ{¦¡ªº§Î¦¡¡A¤£¶È¦p¦¹ÁÙ¶i¤@¨B­n¨D³o­Ó°Æµ{¦¡³Ì¦n¯à¥u¦³¿é¤J°Ñ¼Æ§â¸ê®Æ¶Ç»¼¶i¨Ó¡AµM«á¸g¹L°Æµ{¦¡³B²z§âµ²ªG¶Ç¦^¥Dµ{¦¡´N¦n¡A¤£­n¦A¦³¨ä¥L³¡¥÷©M¥Dµ{¦¡¦³²o³s¡A¤]´N¬O»¡¡A¦b³o­Ó°Æµ{¦¡¤¤¦pªG¥²¶·¥Î¨ìÅܼơA³o­ÓÅܼƳ̦n¥u¯à³Q³o­Ó°Æµ{¦¡¦s¨ú¡A°²¦p¦¹ÅܼƳQ°Æµ{¦¡¥H¥~ªºµ{¦¡¦s¨ú·|³y¦¨³\¦h¤zÂZ¡A°£¿ù¤]¤£©ö¡A³o¤]´N¬O¬°¤°»ò§Ú­Ì¨Ï¥Î°Ï°ìÅܼƪº­ì¦]¡C³o­ÓÆ[©À´N¬O¼Æ¦~«e ( ¬ù 1990 ¦~¥N ) ¬y¦æªºµ²ºc¤ÆÆ[©À¡C

©³¤U¬Ý¬Ý²Ä 87 ¦æªº invoke BeginPaint¡C¦ý¦b³o¤§«e¡A¥ý¬Ý¬Ý ADDR °²«ü¥O¡C

ADDR °²«ü¥O

ADDR ©M OFFSET ³£¬O¨ú±o¦ì§}ªº°²«ü¥O¡A¨âªÌµy¦³¤£¦P¡COFFSET ¬O¥Î¦b²ÕĶ®É¤w¸g½T©w¦ì§}ªº¦a¤è¡A¨Ò¦p¨ú±o¦b¸ê®Æ¬q¤¤ªºÅܼƦì§}¡C¦ý¬O¦b°ïÅ|ùتº°Ï°ìÅܼơA²ÕĶ®ÉÁÙ¤£ª¾¹D¦ì§} ( ¦]¬°¨º®ÉÁÙ¤£ª¾¹D¦b¦¹¤§«e°ïÅ|³Q¨Ï¥Î¤F¦h¤Ö )¡A³o®É´N¤£¯à¥Î OFFSET ¨Ó¨ú±o¦ì§}¡A¥²¶·¥Î ADDR¡C¨º»ò ADDR ¬O«ç»ò¨ú±o°Ï°ìÅܼƪº¦ì§}©O¡H

­ì¨Ó²ÕĶ¾¹·|§â ADDR ½Ķ¦¨

LEA     EAX,ÅܼÆ

©Ò¥H EAX «K¦s¦³¸Ó°Ï°ìÅܼƪº¦ì§}¡A¤§«á«K¥Î EAX ¥h¨ú¥N ¡yADDR Åܼơz¡C¨Ò¦pµ{¦¡²Ä 87 ¦æªº

invoke  BeginPaint,hWin,ADDR PS

¸g¹L²ÕĶ«á´NÅܦ¨¡G

LEA     EAX,[EBP-44]
PUSH    EAX
PUSH    DWORD PTR [EBP+0C]
CALL    USER32!BeginPaint

¤W­±¯»¬õ¦âªº³¡¥÷¬O ADDR ²ÕĶ«áªºµ²ªG¡A²ÕĶ«á PS µ²ºcÅéÅܼƪº¦ì§}«K©ñ¦b EAX ùØ¡A©Ò¥H¦A§â EAX ±À¤J°ïÅ|ùØ¡A´N§¹¦¨¨ú±o PS Åܼƪº¦ì§}¤Î¬° BeginPaint API ¶Ç»¼°Ñ¼Æ¡C

BeginPaint API

BeginPaint API ¬O¥Î¨Ó·Ç³Æø¹Ï®É¨Ï¥Î¡A¨ä»yªk¦p¤U¡G

HDC BeginPaint(
    HWND            hwnd,       // handle to window
    LPPAINTSTRUCT   lpPaint     // pointer to structure for paint information  
   );

BeginPaint ¦³¨â­Ó°Ñ¼Æ¡A²Ä¤@­Ó¬Oµøµ¡¥N½X¡A²Ä¤G­Ó¬O¤@Âù¦r²Õ¤j¤pªº¼Æ­È¡A¦¹¼Æ«ü¦Vµ²ºcÅé¡APAINTSTRUCT¡Aªº¦ì§}¡C°²¦p©I¥s¦¨¥\¡A¦b eax ·|¶Ç¦^³]³Æ¤º®e¥N½X¡A¤p¤ì°¸§â¥¦¦s©ó°Ï°ìÅÜ¼Æ hDevCont ùØ¡C¦Ü©ó PAINTSTRUCT µ²ºcÅ骺¤º®e¦p¤U¡G

PAINTSTRUCT     STRUC
  hdc           dd      ?
  fErase        dd      ?
  rcPaint       RECT    <?>
  fRestore      dd      ?
  fIncUpdate    dd      ?
  rgbReserved   db      32 dup (?) 
PAINTSTRUCT     ENDS

hdc ¬O³]³Æ¤º®e¥N½X»P BeginPaint ªº¶Ç¦^­È¬Û¦P¡CfErase ¬O¥Î¨Óªí¥Ü¬O§_¹ïµL®Ä°Ï°ìªº­I´º¤W¦â¡A­n¤W¦âªº¸Ü¥Î TRUE¡A¤£¤W¦â¥Î FALSE¡CrcPaint ¬O¥t¤@­Óµ²ºcÅé¡ARECT¡A¦¹µ²ºcÅé«ü©w¤F¤@¶ô¯x§Î½d³ò¡A¦b©I¥s BeginPaint ¤§«á¡A¨t²Î·|§âµL®Ä°Ïªº½d³ò¶ñ¤J rcPaint µ²ºcÅé¡A¦A¶Ç¦^µ¹§Ú­Ìªºµ{¦¡¡CfRestore¡BfIncUpdate¡BrgbReserved ³£«O¯dµ¹¨t²Î¨Ï¥Î¡C

EndPaint API

BeginPaint ·|¶}±Ò¤@­Ó³]³Æ¤º®e¡A·íø¹Ï§¹²¦¡AÀ³¸Ó§â¥¦Ãö³¬¡A¤@¯ë¬O¨Ï¥Î EndPaint ¨Ó³B²z¡A´«¥y¸Ü»¡ BeginPaint »P EndPaint À³¸Ó¦¨¹ï·f°t¡CEndPaint »yªk¬O

BOOL EndPaint(
    HWND                hWnd,       // handle to window
    CONST PAINTSTRUCT   *lpPaint    // pointer to structure for paint data
   );

EndPaint ªº°Ñ¼Æ©M BeginPaint ¬Û¦P¡C¤@¯ëµ{¦¡­nø¹Ï®Éªº°µªk¦³¨âºØ¡A²Ä¤@ºØ¬O¹³¤U­±¦b WM_PAINT °T®§¤¤¨Ï¥Î¡G

        invoke  BeginPaint,hWin,ADDR PS
        ¡K¡K    ¡K¡K                    ;ø¹Ïµ{¦¡
        invoke  EndPaint,hWin,ADDR PS

BeginPaint ©M EndPaint ¨â­Ó API ¨Ó¶}±Ò©MÄÀ©ñ³]³Æ¤º®e¡C¤]¥i¥H¨Ï¥Î²Ä¤GºØ¤èªk¡A¥Î GetDC ©M ReleaseDC ¨Ó³B²z¡C

GetClientRect API

GetClientRect ¬O¥Î¨Ó¨ú±o¤u§@°Ïªº¤j¤p¡A¨ä¨ç¦¡­ì«¬¬°

BOOL GetClientRect(
    HWND    hWnd,       // handle of window
    LPRECT  lpRect      // address of structure for client coordinates
   );

hWnd ¬O­n¨ú±o¤u§@°Ïªºµøµ¡¥N½X¡AlpRect ¬O«ü¦V¤@­Ó RECT µ²ºcÅé¦ì§}¡A·í GetClientRect °õ¦æ§¹«á·|¶Ç¦^¨Ó¥ª¤W¨¤©M¥k¤U¨¤ªº®y¼Ð¡A¨Ã¦s©ó lpRect ©Ò«ü¦ì§}ªºµ²ºcÅ餺¡C¤@¯ë¦Ó¨¥¡A¤u§@°Ïªº¥ª¤W¨¤®y¼Ð¬O (0¡A0)¡A©Ò¥H¥k¤U¨¤®y¼Ð¨ä¹ê´N¬O¤u§@°Ïªºªø»P¼e¡C

DrawText API

³o­Ó API ¬O¥Î¨Ó§â¦r¦ê¥H«ü©wªº¤è¦¡Åã¥Ü¦b¬Y­Óªº°Ï°ì¤º¡A¨ä­ì«¬¬°¡G

int DrawText(
    HDC     hDC,        // handle to device context
    LPCTSTR lpString,   // pointer to string to draw
    int     nCount,     // string length, in characters
    LPRECT  lpRect,     // pointer to structure with formatting dimensions
    UINT    uFormat     // text-drawing flags
   );
  1. hDC¡G¬O³]³Æ¤º®e¥N½X¡C

  2. lpString¡G¬O­nÅã¥Üªº¦r¦ê¦ì§}¡C

  3. nCount¡G¬O¦r¦êªø«×¡A¥H¦ì¤¸²Õ¬°³æ¦ì¡C°²¦p¦¹¼Æ­È¬°¡Ð1ªº¸Ü¡A¨º»ò DrawText ·|¦Û°Êªº§ä¨ì NULL §@¬°¦r¦êªºµ²§À¡C

  4. lpRect¡G¬O¦r¦êÅã¥Ü¦b¨º¤@¶ô°Åµô¯x§Î°Ï°ì¡A¥¦«ü¦V¤@­Ó RECT µ²ºcÅé¦ì§}¡C( ¨£²Ä 17 ³¹ªº»¡©ú )

  5. uFormat¡G¬OÅã¥Ü¦r¦êªº®æ¦¡¡ADT_SINGLELINE ¬O«ü§â¦r¦êÅã¥Ü¦b¤@¦æ¡A¥¦·|©¿²¤´«¦æ¦r¤¸¡A¦Ü©ó¨ä¥Lªº®æ¦¡¡A½Ð°Ñ¦Ò Win32 Programmer's Reference¡C

¨ä¹ê³o­Óµ{¦¡¡A¨Ã¨S¦³¥²­n¨Ï¥Î GetClientRect¡A¦]¬°·í©I¥s BeginPaint ®É¡A¨t²Î´N¤w¸gª¾¹DµL®Ä°Ïªº½d³ò¡A§Ú­Ì¥u»Ý­n­«Ã¸µL®Ä°Ï´N¥i¥H¤F¡CÁÙ¦³¤@ÂI²z¥Ñ¡A°²¨Ï¨C¦¸³£­«Ã¸¾ã­Ó¤u§@°Ï¡A¨º»ò¬O«Ü¯Ó¶O¸ê·½ªº¡A³oÂI¦b¹ê»Ú¼¶¼gµ{¦¡®ÉÀ³¸Ó®É®É°O±o¡C§ïÅܫ᪺ DrawStr °Æµ{¦¡¦p¤U¡G

DrawStr proc    AddrSt:DWORD,hWin:DWORD
        LOCAL   hDevCont:HDC
        LOCAL   PS:PAINTSTRUCT

        invoke  BeginPaint,hWin,ADDR PS
        mov     hDevCont,eax
        invoke  DrawText,hDevCont,AddrSt,-1,ADDR PS.rcPaint,DT_SINGLELINE
        invoke  EndPaint,hWin,ADDR PS
        ret
DrawStr endp

¥Î Sofit¡ÐICE °£¿ù

·í§Ú­Ì¹ê»Ú¼¶¼gµ{¦¡®É¡A°£¿ù¤j·§¬O¤£¥iÁקKªº¤@Àô¡A¤p¤ì°¸±N¥H³o­Ó DRAWTXT.EXE ¬°¨Ò»¡©ú¦p¦ó¦b DRAWTXT.EXE ùتºµøµ¡¨ç¦¡³]©w¤¤Â_ÂI¡C¬°¦ó­n§ä¨ìµøµ¡¨ç¦¡©O¡A³o¬O¦]¬°¥Dµ{¦¡ªº¼gªk³£¬O©T©wªº¡A©Ò¥H·|²£¥Í¿ù»~ªº¾÷·|´X¥G¨S¦³¡A¦Óµøµ¡¨ç¦¡¦]¨C­Óµ{¦¡¤£¦P¡A©Ò¥H¥X¿ùªº¾÷·|¤j¡A¦]¦¹°£¿ùªº­«ÂIÀ³¸Ó©ñ¦bµøµ¡¨ç¦¡¡C

§Ú­Ì©Ò­nÃö¤ßªº¬O°T®§¶Ç¤Jµøµ¡¨ç¦¡ªº®É¾÷¥H¤Îµøµ¡¨ç¦¡¹ï°T®§³B²z¤è¦¡¡C­n¹F¨ì³o­Ó¥Øªº¡A¤@¯ë¬O¦bµøµ¡¨ç¦¡ªº¶i¤JÂI©Î¬O¨C­Ó°T®§¶i¤JÂI³]©w¤¤Â_ÂI¡A³o¼Ë¤~¯àÀˬd°T®§¬O§_¥¿½T³B²z¡C©³¤U»¡©ú¥Î Sofit¡ÐICE ªº Symbol Loader ¸ü¤J DRAWTXT.EXE¡G

­º¥ý¥Ñ¨t²Î¥ª¤U¨¤ªº¡y¶}©l¡z«ö¶s¡A°õ¦æ Symbol Loader¡A¿ï¾Ü¿ï³æ¡yFile¡z¡÷¡yOpen Module¡K¡z¡A¤Á´«¨ì DRAWTXT.EXE ©Ò¦b¸ê®Æ§¨¡A¶}±Ò¥¦¡C

²Ä¤G¡A¦A¿ï¾Ü Symbol Loader ¿ï³æ¤Wªº¡yModule¡z¡÷¡yLoad¡z¡A«ö¤U¡y¬O¡zªº«ö¶s¡A©¿²¤§ä¤£¨ì²Å¸¹ÀɪºÄµ§i¡A¶i¤J Soft¡ÐICE °£¿ùµe­±¡C

²Ä¤T¡A«ö¤U¡yF8¡zÁä³æ¨B°lÂܨì§Ú­Ìªº²Ä¤G¦æµ{¦¡½X¡AµM«á¿é¤J¡yu¡z«ü¥O¡A´M§ä DRAWTXT.EXE ªºµøµ¡¨ç¦¡¡C¨º»ò¡A­n¦p¦ó¤~¯à§ä¨ìµøµ¡¨ç¦¡¶i¤JÂI©O¡H±zÆ[¹î¤@¤U­ì©lµ{¦¡¤¤¥Dµ{¦¡ªºµ²§À¬O

        invoke  ExitProcess,eax

µøµ¡¨ç¦¡ªº¶i¤JÂI´N¬O¤U¤@¦æ«ü¥O¡AÀ³¸Ó¤£Ãø§ä¨ì¡C±z¥i¥H¿é¤J¡yu¡z«ü¥O¥hÆ[¹î¡A³o­Ó«ü¥O¬O Sofit¡ÐICE ªº«ü¥O¡A·N«ä¬O¤Ï²ÕĶ ( unassemble )¡A¥Îªk©M DEBUG/SYMDEB Ãþ¦ü¡C

Sofit¡ÐICE «ü¥O¡GU ( ¤Ï²ÕĶ«ü¥O )

¨ä»yªk¬O

U [¦ì§} Lªø«×]

¡y¦ì§}¡z©M¡yLªø«×¡z³£¥i¥H¬Ù²¤¡A¦pªG¬Ù²¤¦ì§}ªº¸Ü¡A¨º»ò Sofit¡ÐICE ±N±qµ{¦¡½Xµøµ¡³Ì«á¤@­Ó«ü¥Oªº¤U¤@­Ó«ü¥O¶}©l¤Ï²ÕĶ¡A¡yLªø«×¡z¦pªG¬Ù²¤ªº¸Ü¡ASofit¡ÐICE ±N¤Ï²ÕĶ¾ã­Óµ{¦¡½Xµøµ¡¡C¨Ì·Ó­è¤~©ÒÁ¿ªº¤èªk§ä invoke ExitProcess,eax ¤U¤@¦æ«ü¥O¡A´N¬Oµøµ¡¨ç¦¡¡AÀ³¸Ó·|§ä¨ìµøµ¡¨ç¦¡¦b¦ì§} 004010E9 ³B¡G

004010E9  55                PUSH    EBP
004010EA  8BEC              MOV     EBP,ESP
004010EC  837D0C0F          CMP     DWORD PTR [EBP+0C],0F
004010F0  7510              JNZ     00401102
004010F2  B81A304000        MOV     EAX,0040301A  ¡÷³B²z WM_PAINT
004010F7  FF7508            PUSH    DWORD PTR [EBP+08]
004010FA  50                PUSH    EAX
004010FB  E84D000000        CALL    0040114D
00401100  EB45              JMP     00401147
00401102  817D0C02010000    CMP     DWORD PTR [EBP+0C],00000102
00401109  7518              JNZ     00401123
0040110B  8B4510            MOV     EAX,[EBP+10]  ¡÷³B²z WM_CHAR
0040110E  BA23304000        MOV     EDX,00403023
00401113  8802              MOV     [EDX],AL
00401115  6A01              PUSH    01
00401117  6A00              PUSH    00
00401119  FF7508            PUSH    DWORD PTR [EBP+08]
0040111C  E8A1000000        CALL    USER32!InvalidateRect
00401121  EB24              JMP     00401147
00401123  837D0C02          CMP     DWORD PTR [EBP+0C],02
00401127  7509              JNZ     00401132
00401129  6A00              PUSH    00            ¡÷³B²zWM_DESTROY
0040112B  E8A4000000        CALL    USER32!PostQuitMessage
00401130  EB15              JMP     00401147
00401132  FF7514            PUSH    DWORD PTR [EBP+14] ¡÷¤º©wªº³B²z°T®§µ{¦¡
00401135  FF7510            PUSH    DWORD PTR [EBP+10]
00401138  FF750C            PUSH    DWORD PTR [EBP+0C]
0040113B  FF7508            PUSH    DWORD PTR [EBP+08]
0040113E  E85B000000        CALL    USER32!DefWindowProcA
00401143  C9                LEAVE
00401144  C21000            RET     0010
00401147  33C0              XOR     EAX,EAX
00401149  C9                LEAVE
0040114A  C21000            RET     0010

§ä¨ìµøµ¡¨ç¦¡¤§«á¡A¤U¤@¨BÀ³¸Ó¬O¦b¨C¤@­Ó³B²z°T®§ªº¦a¤è³]¥ß¤¤Â_ÂI¡C¦b Sofit¡ÐICE ¦³Ãö¤¤Â_ÂIªº«ü¥O¦³¤@¸s¡A³£¬O¥H¡yB¡z¶}©l¡A¥]¬A³]¥ß¤¤Â_ÂI¡B²M°£¤¤Â_ÂI¡B¦C¥X¤¤Â_ÂIµ¥¡A¦Ó³]©wªº¤¤Â_ÂI¤S¦³¦]¬°°õ¦æ¨ì¬Y¤@¦ì§}¦Ó°±¤U¨Ó¡B¦s¨ú¬Y¤@¦ì§}¦Ó°±¤î¡B¦s¨ú¬Y¤@¿é¥X¤J°ð¦Ó°±¤îµ¥µ¥¡A°±¤U¨Ó«á Soft¡ÐICE ·|Åã¥Ü¥X·í®É«áªº¼È¦s¾¹¡B°O¾ÐÅ餺®eµ¥±¡§Î¥H¨Ñµ{¦¡³]­p®v°£¿ùªº°Ñ¦Ò¡A¥i»¡¥\¯à±j¤j¡C¦¹³B¤p¤ì°¸¤¶²Ð BPX¡C

Sofit¡ÐICE «ü¥O¡GBPX ( ³]¥ß°õ¦æ¤¤Â_ÂI )

BPX ¬O«üµ{¦¡°õ¦æ¨ì¬Y¦ì§}´N·|°±¤U¨Ó¨Ã¶i¤J Sofit¡ÐICE °£¿ùµe­±¡A¨ä»yªk¬O

BPX [¦ì§}]

DRAWTXT.EXE ªºµøµ¡¨ç¦¡¶È³B²z¤T­Ó°T®§¡AWM_CHAR¡BWM_PAINT¡BWM_DESTROY¡A¦Ó WM_DESTROY À³¸Ó¤£»Ý­n°£¿ù¡A°²¨Ï¤p¤ì°¸¦b 004010F2 ³o­Ó¦ì§}³]¥ß¤¤Â_ÂI¡A¦b Soft¡ÐICE ªº©R¥Oµøµ¡¤U¿é¤J

bpx 4010f2 [Enter]

¦¹®É±z·|¬Ý¨ì¦b Soft¡ÐICE µ{¦¡½Xµøµ¡ªº

004010F2  B81A304000        MOV     EAX,0040301A

Åܦ¨¤ÑÂŦâ¡Aªí¥Ü¤¤Â_ÂI¤w³]¥ß¦n¤F¡C±µ¤U¨Ó¦b Soft¡ÐICE ¤¤¿é¤J¡yg¡z«ü¥O¡A±z·|¬Ý¨£¿Ã¹õ¤W°h¥X Soft¡ÐICE µe­±¡A¥X²{ DRAWTXT.EXE µøµ¡¡A¦ý°¨¤W¤S¦^¨ì Soft¡ÐICE µe­±¡C³o¬O¦]¬° UpdateWindow ¤]·|°e¥X WM_PAINT °T®§¡C

µM«á±z¦A¿é¤J¡yg¡z«ü¥O¡A¤S·|°h¥X Soft¡ÐICE¡A¦ý³o®É¨Ã¤£·|¥ß§Y¦^¨ì Soft¡ÐICE¡A¦ý¬O·í±z«ö¤U¥ô·NÁä®É¡A¤S·|¦^¨ì Soft¡ÐICE µe­±¡A³o¬O¦]¬°¦b«ö¤UÁ䤧«e¡Aµ{¦¡¤£·|¦¬¨ì WM_PAINT °T®§¡A©Ò¥H¤£·|¤¤Â_¡A¦ý«ö¤U¥ô·NÁä¡Aµ{¦¡·|¦Û¤v°e¥X¤@­Ó WM_PAINT µ¹¦Û¤v¡A©Ò¥H·|¶i¤J Soft¡ÐICE °£¿ùµe­±¡C

¦¹«á¨C¦¸¦¬¨ì WM_PAINT °T®§¡A³£·|¶i¤J Soft¡ÐICE °£¿ùµe­±¡C¨Ò¦p¡A§ïÅܵøµ¡¤j¤p¡BÁY¨ì³Ì¤p«áÁÙ­ìµ¥¡A¤]·|¦]¬°¨t²Î¹ï DRAWTXT.EXE µo¥X WM_PAINT¡A©Ò¥H¤]·|¶i¤J Soft¡ÐICE °£¿ùµe­±¡C

Sofit¡ÐICE «ü¥O¡GBC ( ²M°£¤¤Â_ÂI )

·Q­n²M°£¤¤Â_ÂI¡A¥i¥H°õ¦æ BC «ü¥O¡C¨ä»yªk¬O

BC ¤¤Â_ÂI½s¸¹¦Cªí©Î¡y*¡z

¨C·í§Ú­Ì«Ø¥ß¤@­Ó¤¤Â_ÂI®É¡ASoft¡ÐICE ·|·Ó«Ø¥ß¥ý«áµ¹¨C­Ó¤¤Â_ÂI½s¸¹¡A¦¹½s¸¹¥Ñ 0 ¶}©l¡A·í¤£»Ý­n¦¹¤¤Â_ÂI®É¡A¥i¥H¥Î¡yBC ¤¤Â_ÂI½s¸¹¡z²M°£¤§¡C°²¦p±z¥Î¡yBC *¡z¡A«h·|²M°£©Ò¦³¤¤Â_ÂI¡C¨Ò¦p¡A¤p¤ì°¸²{¦b¤£­n°»´ú³B²z WM_PAINT ªºµ{¦¡¡A´N¤U

bc 0

¦]¬°¶È³]¥ß¤@­Ó¤¤Â_ÂI¡A©Ò¥H¸Ó¤¤Â_ÂI½s¸¹¬°¹s¡C°²¦p¤w¸g«Ø¥ßªº¤¤Â_ÂI¤Ó¦h¡A¥i¥Î BL ¬d¸ß¡C

Sofit¡ÐICE «ü¥O¡GBL ( ¦C¥X¤¤Â_ÂI¸ê®Æ )

BL ¬O§â©Ò¦³ªº¤¤Â_ÂIªº½s¸¹¤¤Â_¦ì§}¦C¥X¨Ó¡C

Sofit¡ÐICE «ü¥O¡GG ( °õ¦æ«ü¥O )

³o­Ó«ü¥O©M DEBUG/SYMDEB ¬Û¦ü¡A°õ¦æ«ü¥Oªº·N«ä¡A¨ä»yªk¬O

G [=¦ì§}] [¦ì§}]

¦³¡y=¡zªº¦ì§}¬O«ü±q³o­Ó¦ì§}¶}©l°õ¦æ¡A°²¦p¦¹¦ì§}¬Ù²¤ªº¸Üªí¥Ü±q²{¦bªº EIP ¦ì§}¶}©l°õ¦æ¡C¦Ó«á­±¨S¦³µ¥©óªº¦ì§}¬O«ü°õ¦æ¨ì¦¹³B°±¤î¡A¦pªG¬Ù²¤ªº¸Ü´N·|¨ì³]©w¤¤Â_ÂI³B°±¤î¡A°²¦p¨S³]©w¤¤Â_ÂIªº¸Ü¡A´NµLªk°±¤î¤F¡C


¨ì²Ä¤G³¹¡A ¦^¨ì­º­¶¡A ¨ì²Ä¥|³¹