WIN32API

Posted on April 18, 2010, 4:21 pm UTC by Iago (over 2 years ago)

Code (highlighted for ASM)

  1. ; NASM: nasmw -f obj nasmpad.asm
  2. ; ALINK: alink -c -oPE -subsys gui nasmpad
  3.  
  4. extern CreateWindowExA
  5. import CreateWindowExA user32.dll
  6. extern RegisterClassExA
  7. import RegisterClassExA user32.dll
  8. extern GetModuleHandleA
  9. import GetModuleHandleA kernel32.dll
  10. extern LoadIconA
  11. import LoadIconA user32.dll
  12. extern LoadCursorA
  13. import LoadCursorA user32.dll
  14. extern ShowWindow
  15. import ShowWindow user32.dll
  16. extern UpdateWindow
  17. import UpdateWindow user32.dll
  18. extern GetMessageA
  19. import GetMessageA user32.dll
  20. extern TranslateMessage
  21. import TranslateMessage user32.dll
  22. extern DispatchMessageA
  23. import DispatchMessageA user32.dll
  24. extern ExitProcess
  25. import ExitProcess kernel32.dll
  26. extern DefWindowProcA
  27. import DefWindowProcA user32.dll
  28.  
  29. struc WNDCLASSEX
  30. .cbSize        resd 1
  31. .style         resd 1
  32. .lpfnWndProc   resd 1
  33. .cbClsExtra    resd 1
  34. .cbWndExtra    resd 1
  35. .hInstance     resd 1
  36. .hIcon         resd 1
  37. .hCursor       resd 1
  38. .hbrBackground resd 1
  39. .lpszMenuName  resd 1
  40. .lpszClassName resd 1
  41. .hIconSm       resd 1
  42. endstruc
  43.  
  44. struc POINT
  45. .x resd 1
  46. .y resd 1
  47. endstruc
  48.  
  49. struc MSG
  50. .hwnd    resd 1
  51. .message resd 1
  52. .wParam  resd 1
  53. .lParam  resd 1
  54. .time    resd 1
  55. .pt      resb POINT_size
  56. endstruc
  57.  
  58. segment .data use32
  59. CS_VREDRAW equ 1h
  60. CS_HREDRAW equ 2h
  61. WinClass:
  62. istruc WNDCLASSEX
  63. at WNDCLASSEX.cbSize,        dd  WNDCLASSEX_size
  64. at WNDCLASSEX.style,         dd  CS_VREDRAW + CS_HREDRAW
  65. at WNDCLASSEX.lpfnWndProc,   dd  Windowprocedure
  66. at WNDCLASSEX.cbClsExtra,    dd  0
  67. at WNDCLASSEX.cbWndExtra,    dd  0
  68. at WNDCLASSEX.hInstance,     dd  0
  69. at WNDCLASSEX.hIcon,         dd  0
  70. at WNDCLASSEX.hCursor,       dd  0
  71. at WNDCLASSEX.hbrBackground, dd  COLOR_BACKGROUND
  72. at WNDCLASSEX.lpszMenuName,  dd  0
  73. at WNDCLASSEX.lpszClassName, dd  szClassName
  74. at WNDCLASSEX.hIconSm,       dd  0
  75. iend
  76. WinMSG:
  77. istruc MSG
  78. at MSG.hwnd,    dd 0
  79. at MSG.message, dd 0
  80. at MSG.wParam,  dd 0
  81. at MSG.lParam,  dd 0
  82. at MSG.time,    dd 0
  83. at MSG.pt,      dd 0
  84. iend
  85. Handle                 dd 0
  86. Button                 dd 0
  87. BtnClass               db "Button"
  88. BtnT                   db "Titulo"
  89. Icon                   dd 0
  90. Cursor                 dd 0
  91. szClassName            db "nasmpad_0001"
  92. szTitle                db "Nasmpad"
  93. IDI_APPLICATION       equ 32512
  94. IDC_ARROW             equ IDI_APPLICATION
  95. CW_USEDEFAULT         equ 80000000h
  96. WS_EX_WINDOWEDGE       equ 00000100h
  97. WS_EX_CLIENTEDGE       equ 00000200h
  98. WS_OVERLAPPED         equ 0h
  99. WS_CAPTION             equ 0C00000h
  100. WS_SYSMENU             equ 80000h
  101. WS_VISIBLE             equ 268435456d
  102. WS_CHILD               equ 1073741824d
  103. WS_CHILDWINDOW         equ WS_CHILD
  104. WS_THICKFRAME         equ 40000h
  105. WS_MINIMIZEBOX         equ 20000h
  106. WS_MAXIMIZEBOX         equ 10000h
  107. WS_EX_OVERLAPPEDWINDOW equ WS_EX_WINDOWEDGE|WS_EX_CLIENTEDGE
  108. WS_OVERLAPPEDWINDOW   equ WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX
  109. SW_SHOWNORMAL         equ 1
  110. WM_DESTROY             equ 2h
  111. COLOR_BACKGROUND       equ 1
  112. segment .code use32
  113. global ..start
  114. ..start:
  115.  
  116. push dword 0
  117. call [GetModuleHandleA]
  118. mov [WinClass+WNDCLASSEX.hInstance], eax
  119.  
  120. push dword IDI_APPLICATION
  121. push dword 0
  122. call [LoadIconA]
  123.  
  124. mov [WinClass+WNDCLASSEX.hIcon],eax
  125.  
  126. push dword IDC_ARROW
  127. push dword 0
  128. call [LoadCursorA]
  129.  
  130. mov [WinClass+WNDCLASSEX.hCursor],eax
  131.  
  132. push dword WinClass
  133. call [RegisterClassExA]
  134.  
  135. push dword 0
  136. push dword [WinClass+WNDCLASSEX.hInstance]
  137. push dword 0
  138. push dword 0
  139. push dword 480
  140. push dword 640
  141. push dword CW_USEDEFAULT
  142. push dword CW_USEDEFAULT
  143. push dword WS_OVERLAPPEDWINDOW
  144. push dword szTitle
  145. push dword szClassName
  146. push dword WS_EX_OVERLAPPEDWINDOW
  147. call [CreateWindowExA]
  148.  
  149. mov [Handle], eax
  150.  
  151. push dword SW_SHOWNORMAL
  152. push dword [Handle]
  153. call [ShowWindow]
  154.  
  155. MsgLoop:
  156.  
  157. push dword 0
  158. push dword 0
  159. push dword 0
  160. push dword WinMSG
  161. call [GetMessageA]
  162.  
  163. push dword WinMSG
  164. call [TranslateMessage]
  165.  
  166. push dword WinMSG
  167. call [DispatchMessageA]
  168.  
  169. jmp MsgLoop
  170.  
  171. Windowprocedure:
  172.  
  173. %define hWnd ebp+8
  174. %define Msg ebp+0ch
  175. %define wParam ebp+10h
  176. %define lParam ebp+14h
  177.  
  178. push ebp
  179. mov ebp,esp
  180.  
  181. cmp dword [Msg],WM_DESTROY
  182. je Finish
  183.  
  184. .DefMsgHandler:
  185.  
  186. push dword [lParam]
  187. push dword [wParam]
  188. push dword [Msg]
  189. push dword [hWnd]
  190. call [DefWindowProcA]
  191.  
  192. .Exit:
  193.  
  194. mov esp, ebp
  195. pop ebp
  196. ret
  197.  
  198. Finish:
  199.  
  200. push dword 0
  201. call [ExitProcess]
  202.  
  203.  
  204. jmp Windowprocedure.DefMsgHandler