📄 lion-tutorial14.htm
字号:
invoke GetMessage, ADDR msg,NULL,0,0</b> <br>
<b>
.BREAK .IF (!eax)</b> <br>
<b>
invoke TranslateMessage, ADDR msg</b> <br>
<b>
invoke DispatchMessage, ADDR msg</b> <br>
<b> .ENDW</b> <br>
<b> mov eax,msg.wParam</b> <br>
<b> ret</b> <br>
<b>WinMain endp</b>
<p><b>WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM</b> <br>
<b> LOCAL startInfo:STARTUPINFO</b> <br>
<b> .IF uMsg==WM_DESTROY</b> <br>
<b> invoke PostQuitMessage,NULL</b>
<br>
<b> .ELSEIF uMsg==WM_INITMENUPOPUP</b> <br>
<b> invoke GetExitCodeProcess,processInfo.hProcess,ADDR
ExitCode</b> <br>
<b> .if eax==TRUE</b> <br>
<b> .if ExitCode==STILL_ACTIVE</b>
<br>
<b>
invoke EnableMenuItem,hMenu,IDM_CREATE_PROCESS,MF_GRAYED</b> <br>
<b>
invoke EnableMenuItem,hMenu,IDM_TERMINATE,MF_ENABLED</b> <br>
<b> .else</b>
<br>
<b>
invoke EnableMenuItem,hMenu,IDM_CREATE_PROCESS,MF_ENABLED</b> <br>
<b>
invoke EnableMenuItem,hMenu,IDM_TERMINATE,MF_GRAYED</b> <br>
<b> .endif</b>
<br>
<b> .else</b> <br>
<b> invoke
EnableMenuItem,hMenu,IDM_CREATE_PROCESS,MF_ENABLED</b> <br>
<b> invoke
EnableMenuItem,hMenu,IDM_TERMINATE,MF_GRAYED</b> <br>
<b> .endif</b> <br>
<b> .ELSEIF uMsg==WM_COMMAND</b> <br>
<b> mov eax,wParam</b> <br>
<b> .if lParam==0</b> <br>
<b> .if ax==IDM_CREATE_PROCESS</b>
<br>
<b>
.if processInfo.hProcess!=0</b> <br>
<b>
invoke CloseHandle,processInfo.hProcess</b> <br>
<b>
mov processInfo.hProcess,0</b> <br>
<b>
.endif</b> <br>
<b>
invoke GetStartupInfo,ADDR startInfo</b> <br>
<b>
invoke CreateProcess,ADDR programname,NULL,NULL,NULL,FALSE,\</b> <br>
<b>
NORMAL_PRIORITY_CLASS,\</b> <br>
<b>
NULL,NULL,ADDR startInfo,ADDR processInfo</b> <br>
<b>
invoke CloseHandle,processInfo.hThread</b> <br>
<b> .elseif
ax==IDM_TERMINATE</b> <br>
<b>
invoke GetExitCodeProcess,processInfo.hProcess,ADDR ExitCode</b> <br>
<b>
.if ExitCode==STILL_ACTIVE</b> <br>
<b>
invoke TerminateProcess,processInfo.hProcess,0</b> <br>
<b>
.endif</b> <br>
<b>
invoke CloseHandle,processInfo.hProcess</b> <br>
<b>
mov processInfo.hProcess,0</b> <br>
<b> .else</b>
<br>
<b>
invoke DestroyWindow,hWnd</b> <br>
<b> .endif</b>
<br>
<b> .endif</b> <br>
<b> .ELSE</b> <br>
<b> invoke DefWindowProc,hWnd,uMsg,wParam,lParam</b>
<br>
<b> ret</b> <br>
<b> .ENDIF</b> <br>
<b> xor eax,eax</b> <br>
<b> ret</b> <br>
<b>WndProc endp</b> <br>
<b>end start</b>
<h3> <b>Analysis:</b></h3>
The program creates the main window and retrieves the menu handle for future use.
It then waits for the user to select a command from the menu. When the user selects
"Process" menu item in the main menu, we process WM_INITMENUPOPUP message to modify
the menu items inside the popup menu before it's displayed.
<p><b> .ELSEIF uMsg==WM_INITMENUPOPUP</b> <br>
<b> invoke GetExitCodeProcess,processInfo.hProcess,ADDR
ExitCode</b> <br>
<b> .if eax==TRUE</b> <br>
<b> .if ExitCode==STILL_ACTIVE</b>
<br>
<b>
invoke EnableMenuItem,hMenu,IDM_CREATE_PROCESS,MF_GRAYED</b> <br>
<b>
invoke EnableMenuItem,hMenu,IDM_TERMINATE,MF_ENABLED</b> <br>
<b> .else</b>
<br>
<b>
invoke EnableMenuItem,hMenu,IDM_CREATE_PROCESS,MF_ENABLED</b> <br>
<b>
invoke EnableMenuItem,hMenu,IDM_TERMINATE,MF_GRAYED</b> <br>
<b> .endif</b>
<br>
<b> .else</b> <br>
<b> invoke
EnableMenuItem,hMenu,IDM_CREATE_PROCESS,MF_ENABLED</b> <br>
<b> invoke
EnableMenuItem,hMenu,IDM_TERMINATE,MF_GRAYED</b> <br>
<b> .endif</b>
<p>Why do we want to process this message? Because we want to prepare the menu
items in the popup menu before the user can see them. In our example, if the
new process is not started yet, we want to enable the "start process" and gray
out the "terminate process" menu items. We do the reverse if the new process
is already active. <br>
We first check if the new process is still running by calling GetExitCodeProcess
function with the process handle that was filled in by CreateProcess function.
If GetExitCodeProcess returns FALSE, it means the process is not started yet
so we gray out the "terminate process" menu item. If GetExitCodeProcess returns
TRUE, we know that a new process has been started, but we have to check further
if it is still running. So we compare the value in ExitCode to the value <b>STILL_ACTIVE</b>,
if they're equal, the process is still running: we must gray out the "start
process" menu item since we don't want to start several concurrent processes.
<p><b> .if ax==IDM_CREATE_PROCESS</b>
<br>
<b>
.if processInfo.hProcess!=0</b> <br>
<b>
invoke CloseHandle,processInfo.hProcess</b> <br>
<b>
mov processInfo.hProcess,0</b> <br>
<b>
.endif</b> <br>
<b>
invoke GetStartupInfo,ADDR startInfo</b> <br>
<b>
invoke CreateProcess,ADDR programname,NULL,NULL,NULL,FALSE,\</b> <br>
<b>
NORMAL_PRIORITY_CLASS,\</b> <br>
<b>
NULL,NULL,ADDR startInfo,ADDR processInfo</b> <br>
<b>
invoke CloseHandle,processInfo.hThread</b> <br>
<br>
When the user selects "start process" menu item, we first check if hProcess
member of PROCESS_INFORMATION structure is already closed. If this is the first
time, the value of hProcess will always be zero since we define PROCESS_INFORMATION
structure in .data section. If the value of hProcess member is not 0, it means
the child process has exited but we haven't closed its process handle yet. So
this is the time to do it. <br>
We call GetStartupInfo function to fill in the startupinfo structure that we
will pass to CreateProcess function. After that we call CreateProcess function
to start the new process. Note that I haven't checked the return value of CreateProcess
because it will make the example more complex. In real life, you should check
the return value of CreateProcess. Immediately after CreateProcess, we close
the primary thread handle returned in processInfo structure. Closing the handle
doesn't mean we terminate the thread, only that we don't want to use the handle
to refer to the thread from our program. If we don't close it, it will cause
a resource leak.
<p><b> .elseif
ax==IDM_TERMINATE</b> <br>
<b>
invoke GetExitCodeProcess,processInfo.hProcess,ADDR ExitCode</b> <br>
<b>
.if ExitCode==STILL_ACTIVE</b> <br>
<b>
invoke TerminateProcess,processInfo.hProcess,0</b> <br>
<b>
.endif</b> <br>
<b>
invoke CloseHandle,processInfo.hProcess</b> <br>
<b>
mov processInfo.hProcess,0</b>
<p>When the user selects "terminate process" menu item, we check if the new process
is still active by calling GetExitCodeProcess function. If it's still active,
we call TerminateProcess function to kill the process. Also we close the child
process handle since we have no need for it anymore.
<hr size="1">
<div align="center"> This article come from Iczelion's asm page, Welcom to <a href="http://asm.yeah.net">http://asm.yeah.net</a></div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -