VB omnipotent 6: monitor program running
-- Author: bell
Blog: http://blog.csdn.net/useway
In a twinkling of an eye, this series of articles has been written to the sixth one. What do you want to write today? I still have to write something that does not seem to be done by VB. So let's write VB's control over the operating system from today on. First, let's talk about the VB monitoring method for the called program.
Introduction to prototype functions:
Waitforsingleobject
Location:Kernel32.dll
VB statement:Declare function waitforsingleobject lib "Kernel32" (byval hhandle as long, byval dwmilliseconds as long) as long
Note:
The hhandle parameter is the handle of an event, and the second parameter dwmilliseconds is the time interval. Wait_object_0 is returned if the event is in a signal State. If the time exceeds the dwmilliseconds value but the time event is still in a non-signal state, wait_timeout is returned.
Hhandle can be the handle of the following objects:
Change Notification
Console input
Event
Job
Memory resource notification
Mutex
Process
Semaphore
Thread
Waitable Timer
The waitforsingleobject function is used to detect the signal status of the hhandle event. When the function execution time exceeds dwmilliseconds, it returns. However, if the dwmilliseconds parameter is infinite, the function returns only when the corresponding time event changes to a signal state, otherwise, wait until waitforsingleobject has a returned value before executing the subsequent code. In addition, when dwmilliseconds is set to a special value of 0, test whether the hhandle core object is fired and the function returns immediately.
Return Value:
Wait_abandoned 0x00000080: When hhandle is mutex, this return value is returned if the thread with mutex does not release the core object at the end.
Wait_object_0 0x00000000: the core object has been activated.
Wait_timeout 0x00000102: wait for timeout
Wait_failed 0 xffffffff: an error occurs. You can get the error code through getlasterror.
Closehandle
Location:Kernel32.dllVB statement:Declare function closehandle lib "Kernel32" alias "closehandle" (byval hobject as long) as long
Note:
Closes a kernel object. This includes file, file ing, process, thread, security, and synchronization objects. When file processing is involved, this function is usually similar to the close command of VB. Close should be used whenever possible because it supports VB error control. Note that the file handle used by this function is different from the file number of VB.
Return Value: long. If the value is not zero, the operation is successful. If the value is zero, the operation fails. Getlasterror is set.
Parameter table
Parameter type and description
Hobject long, the handle of an object to be closed
Annotation
This object will not be deleted unless all references to the kernel object are disabled.OpenProcess
Location:Kernel32.dll
VB statement:Public declare function OpenProcess lib "Kernel32" (byval dwdesiredaccess as long, byval binherithandle as long, byval dwprocessid as long) as long
Note:The OpenProcess function is used to open an existing process object and return the process handle.
Return Value:
If the process succeeds, the return value is the handle of the specified process.
If the return value fails and the return value is null, call getlasterror to obtain the error code. Okay. Let's do the experiment:
Step 1:Create a project;
Step 2:Create a button in the window;
Step 3:Add the following code to the default form:
Private
Declare FunctionWaitforsingleobject lib "Kernel32" (byval hhandle as long, byval dwmilliseconds as long) as long
Private
Declare FunctionClosehandle lib "Kernel32" (byval hobject as long) as long
Private
Declare FunctionOpenProcess lib "Kernel32" (byval dwdesiredaccess as long, byval binherithandle as long, byval dwprocessid as long) as long
ConstInfinite =-1 &
ConstSynchronize = & h100000
Private
SubCommandmediaclick ()
Dim I as long
Dim R as long
Dim P as long
I = shell ("Notepad. EXE", vbnormalfocus)
P = OpenProcess (synchronize, false, I)
R = waitforsingleobject (p, infinite)
R = closehandle (P)
Msgbox"Notepad closed"
End sub
Run the program to get the following screen: When you click "command1", the notepad is opened. When you close the notepad, the prompt box "Notepad is closed" is displayed ". This program demonstrates how to use APIs to control the simple applications opened by VB. '''
A book written by Bell on Java:
Java programmers:
Preface,
Directory
Zhuo Yue network sales Link
China-Pub sales Link
Dangdang sales Link
Commemorative post on "Java programmer, work day"
Download an electronic version of "Java programmer, work day"