Tutorials to .com

Tutorials to .com » Software » Asm » WIN32 compilation: 14. The process of

WIN32 compilation: 14. The process of

Print View , by: iSee ,Total views: 19 ,Word Count: 2727 ,Date: Fri, 8 May 2009 Time: 10:36 AM

The process of the Fourteenth Division


We will learn this lesson: What is the process? How to generate and terminate a process?

Preliminary Knowledge:

What is the process? Here is my guide in the WIN32 API from the interpretation of an excerpt:
"A process is an ongoing application that includes: private virtual address space, code, data and other operating system resources, such as access to the pipeline process, files, and synchronization objects and so on."

From the above definition you can see that a process has a number of objects: address space, the implementation of other modules and the implementation of procedures to open or create any objects or resources. At the very least, a process must include the executable module, the private address space and more than one thread. What is this thread? A thread is an executive unit. WINDOWS generated when a process, it automatically generates a process for the main thread. The thread is usually from the first module start the implementation of directives. If the process needs more threads, which can then generate explicit.
When WINDWOS to have a process to receive the message, it will generate for the process private memory address space, then the executable file is mapped to the space. In the process of WIN32 for the main process, you can also call the CreateProcess function to process your thread more.

CreateProcess prototype is as follows:

CreateProcess proto lpApplicationName: DWORD, \
lpCommandLine: DWORD, \
lpProcessAttributes: DWORD, \
lpThreadAttributes: DWORD, \
bInheritHandles: DWORD, \
dwCreationFlags: DWORD, \
lpEnvironment: DWORD, \
lpCurrentDirectory: DWORD, \
lpStartupInfo: DWORD, \
lpProcessInformation: DWORD

Do not be intimidated by such a large number of parameters, in fact, which you can ignore most of the parameters (so that they have default values).

lpApplicationName -> executable file name (with or without the path). If the parameter is NULL, it must be passed in the parameter file name lpCommandLine.
lpCommandLine -> passed to the document For the implementation of the command-line parameters. If lpApplicationName for NULL, it must be specified in the parameters, such as: "notepad.exe readme.txt".
lpProcessAttributes and lpthreadAttributes -> designation process and the main thread of the security attributes. You can have them set up as a NULL, so that set up the default security attributes.
bInheritHandles -> flag. Used to set up a new process to create the process of succession of all the open handles.
dwCreationFlags -> There are several signs set up in here want to create a process to determine the behavior, for example: You may want to create a process does not want it immediately after the operation, so it really can be used for a number of pre-operational inspection and modification. You can also set up a new process here in the priority of all threads, most often it is set to NORMAL_PRIORITY_CLASS.
lpEnvironment -> point to the environment block pointer, the environment generally contains several pieces of the environment string. If the parameter is NULL, then create a new process to inherit the process environment block.
lpCurrentDirectory -> point to the current directory as well as the son of the process of setting the "current directory" path. If NULL, then the process of succession to create the "current directory" path.
lpStartupInfo -> point to start a new process structure pointer STARTUPINFO. WINDOWS told STARTUPINFO how to display the appearance of the new process. The parameters of a member of many variables, if you do not want the new process is so special about what can be called function GetStartupInfo process used to create start-up parameters to fill the structure STARTUPINFO variables.
lpProcessInformation -> point to the structure of PROCESS_INFORMATION pointer, the structure contains a number of variables in the process of identification of some members of the only variables:

PROCESS_INFORMATION STRUCT
hProcess HANDLE?; handle to the child process
hThread HANDLE?; handle to the primary thread of the child process
dwProcessId DWORD?; ID of the child process
dwThreadId DWORD?; ID of the primary thread of the child process
PROCESS_INFORMATION ENDS

Process handle and process ID are two different concepts. Process ID if a unique value, and the process is to handle calls related to the WINDOWS API get a return value. Process handle can not be used to identify the uniqueness of a process, because this value is not the only. In the CreateProcess call a new process, the process was created, and immediately returned to CerateProcess function. GetExitCodeProcess function you can call to verify whether or not the end of the process. The function prototype is as follows:

GetExitCodeProcess proto hProcess: DWORD, lpExitCode: DWORD

If the call successfully, lpExitCode included in the query process status code. If STILL_ACTIVE equivalent to show that the process is still on there. TerminateProcess function you can call to force the termination of a process. The function prototype is as follows:

TerminateProcess proto hProcess: DWORD, uExitCode: DWORD

You can specify the value of any withdrawal. By the end of a process does not function well, because the process of loading the dynamic link library and will not be out of the news process.

Examples:

In the example below, when the user selects menu item "crate process" when we create a new process. It will be to implement "" msgbox.exe ". If the user wants to terminate the new process, you can choose menu item" terminate process ". At this time, the application checks whether it wishes to terminate the process still exist, if there is TerminateProcess function call to terminate the.

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

. const
IDM_CREATE_PROCESS equ 1
IDM_TERMINATE equ 2
IDM_EXIT equ 3

. data
ClassName db "Win32ASMProcessClass", 0
AppName db "Win32 asm Process Example", 0
MenuName db "FirstMenu", 0
processInfo PROCESS_INFORMATION <>
programname db "msgbox.exe", 0

. data?
hInstance HINSTANCE?
CommandLine LPSTR?
hMenu HANDLE?
ExitCode DWORD?; Contains the process exitcode status from GetExitCodeProcess call.

. code
start:
invoke GetModuleHandle, NULL
mov hInstance, eax
invoke GetCommandLine
mov CommandLine, eax
invoke WinMain, hInstance, NULL, CommandLine, SW_SHOWDEFAULT
invoke ExitProcess, eax

WinMain proc hInst: HINSTANCE, hPrevInst: HINSTANCE, CmdLine: LPSTR, 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_WINDOW +1
mov wc.lpszMenuName, OFFSET MenuName
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, CW_USEDEFAULT, \
CW_USEDEFAULT, 300,200, NULL, NULL, \
hInst, NULL
mov hwnd, eax
invoke ShowWindow, hwnd, SW_SHOWNORMAL
invoke UpdateWindow, hwnd
invoke GetMenu, hwnd
mov hMenu, 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 startInfo: STARTUPINFO
. IF uMsg == WM_DESTROY
invoke PostQuitMessage, NULL
. ELSEIF uMsg == WM_INITMENUPOPUP
invoke GetExitCodeProcess, processInfo.hProcess, ADDR ExitCode
. if eax == TRUE
. if ExitCode == STILL_ACTIVE
invoke EnableMenuItem, hMenu, IDM_CREATE_PROCESS, MF_GRAYED
invoke EnableMenuItem, hMenu, IDM_TERMINATE, MF_ENABLED
. else
invoke EnableMenuItem, hMenu, IDM_CREATE_PROCESS, MF_ENABLED
invoke EnableMenuItem, hMenu, IDM_TERMINATE, MF_GRAYED
. endif
. else
invoke EnableMenuItem, hMenu, IDM_CREATE_PROCESS, MF_ENABLED
invoke EnableMenuItem, hMenu, IDM_TERMINATE, MF_GRAYED
. endif
. ELSEIF uMsg == WM_COMMAND
mov eax, wParam
. if lParam == 0
. if ax == IDM_CREATE_PROCESS
. if processInfo.hProcess! = 0
invoke CloseHandle, processInfo.hProcess
mov processInfo.hProcess, 0
. endif
invoke GetStartupInfo, ADDR startInfo
invoke CreateProcess, ADDR programname, NULL, NULL, NULL, FALSE, \
NORMAL_PRIORITY_CLASS, \
NULL, NULL, ADDR startInfo, ADDR processInfo
invoke CloseHandle, processInfo.hThread
. elseif ax == IDM_TERMINATE
invoke GetExitCodeProcess, processInfo.hProcess, ADDR ExitCode
. if ExitCode == STILL_ACTIVE
invoke TerminateProcess, processInfo.hProcess, 0
. endif
invoke CloseHandle, processInfo.hProcess
mov processInfo.hProcess, 0
. else
invoke DestroyWindow, hWnd
. endif
. endif
. ELSE
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
ret
. ENDIF
xor eax, eax
ret
WndProc endp
end start

Analysis:

Applications to create the main window menu handle preserved for later use. When the user in the main menu, select the "Process" menu item, the information processing WM_INITMENUPOPUP receive news, we here modify the pop-up menu of the menu item "Enable" and "non-enabled" in order to the same menu display different.

. ELSEIF uMsg == WM_INITMENUPOPUP
invoke GetExitCodeProcess, processInfo.hProcess, ADDR ExitCode
. if eax == TRUE
. if ExitCode == STILL_ACTIVE
invoke EnableMenuItem, hMenu, IDM_CREATE_PROCESS, MF_GRAYED
invoke EnableMenuItem, hMenu, IDM_TERMINATE, MF_ENABLED
. else
invoke EnableMenuItem, hMenu, IDM_CREATE_PROCESS, MF_ENABLED
invoke EnableMenuItem, hMenu, IDM_TERMINATE, MF_GRAYED
. endif
. else
invoke EnableMenuItem, hMenu, IDM_CREATE_PROCESS, MF_ENABLED
invoke EnableMenuItem, hMenu, IDM_TERMINATE, MF_GRAYED
. endif

We have to deal with the purpose of news is to make the menu display from time to time to facilitate the appearance of different users. For example; the new process has not yet been run, we changed Liang (enable) "menu item" start process ", which is shaded (non-enabled) menu item" terminate process ". When the new process up and running, the menu's appearance on the should be the opposite.
First of all, we call GetExitCodeProcess function, which returns into the handle CreateProcess. If GetExitCodeProcess return FALSE, that process has not yet been run, we let the menu item "terminate process" is shaded; if returned to TRUE, said the new process has been launched, we will test is running, this by comparing the ExitCode does it mean to complete STILL_ACTIVE If equal, that process is still running, we let the menu item "start process" is shaded, as in our simple application at the same time do not provide the ability to run multiple processes.

. if ax == IDM_CREATE_PROCESS
. if processInfo.hProcess! = 0
invoke CloseHandle, processInfo.hProcess
mov processInfo.hProcess, 0
. endif
invoke GetStartupInfo, ADDR startInfo
invoke CreateProcess, ADDR programname, NULL, NULL, NULL, FALSE, \
NORMAL_PRIORITY_CLASS, \
NULL, NULL, ADDR startInfo, ADDR processInfo
invoke CloseHandle, processInfo.hThread

When the user selects the menu item "start process", we first detect the structure of PROCESS_INFORMATION members have been shut down hPRocess variables. If this is the first time you start the application, then the variable is 0, because we are. Data sub-structure has been defined to initialize the value to 0. If the value is 0, then that the new process has come to an end, but we have yet to close the process handle (in order to reduce the reference count process), we are here to complete the action.
We call GetStartupInfo start function to fill the structure of information variables, the variables will be passed to the CreateProcess function to. Call CreateProcess to create a new process, we do not check the return value of this function in order to simplify the issues so that some, in practical applications, the work must be done. In the CreateProcess call, we immediately shut down the information in the process parameters of the structure to return to the main thread in the handle to close the thread handle for the core target is to reduce the reference count, otherwise, even if the thread exits, its inside still suffer the existence of nuclear objects is not the release of the kernel, which can cause resource leaks. In fact, the process is the same reason we do not handle the closure of the process is because later we have to use the handle to get some relevant information and processes, as threads, we do not need the application of its relevant information.

. elseif ax == IDM_TERMINATE
invoke GetExitCodeProcess, processInfo.hProcess, ADDR ExitCode
. if ExitCode == STILL_ACTIVE
invoke TerminateProcess, processInfo.hProcess, 0
. endif
invoke CloseHandle, processInfo.hProcess
mov processInfo.hProcess, 0

When the user selects the menu item "terminate process", we call GetExitCodeProcess function to check whether there is a new process, if there are still TerminateProcess we call the function to end it. In addition, we closed it off the handle because we no longer have it.



Assembly Language Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.