When you use shell functions, it is easy to run another executable program in the VB program. In this article, I will show you how to improve Shell functions in a simple way.
Shell accepts two independent variables. The first is the name of the EXE file to be run, including its path. The second independent variable is used to specify the window style of the program. This independent variable is optional. If it is omitted, the program runs in the form of minimization. The technology shown here does not care about the window style of the program during work, or even the program does not have a window. (For more information about window style independent variables, see the help of VB .)
Shell runs the target program asynchronously, which means that the execution can return the vbprogram before the second program is executed. In many cases, this is okay; but in some cases, this may be a problem. One example is when your vbprogram depends on the shell program to complete its operation results to some extent. In this case, you need to pause the vbprogram in some way until another program ends.
The first step is to obtain the shell handle. The shell function returns the program id or PID. Once you have this, you can use the OpenProcess API function to obtain the handle:
Public declare function OpenProcess lib "Kernel32 "_
(Byvaldwdesiredaccess as long ,_
Byvalbinherithandle as long ,_
Byvaldwprocessid as long) as long
Pass the value & h100000 (usually expressed by the constant synchronize) to the first independent variable, and pass 0 (as a long type) to the second independent variable. The third independent variable is the PID returned by the shell function. The value returned by the function is the handle of the Windows Process of the shell program. With this handle, you can use the waitforsingleobject API function to pause the vbprogram until the shell program is terminated. Statement:
Private declare function waitforsingleobject lib _
"Kernel32" (byvalhhandle as long ,_
Byvaldwmilliseconds as long) as long
The first independent variable is the handle that the program waits for. It is obtained from the OpenProcess function. The second independent variable is the waiting time. If you pass a millisecond value, the function will return at the end of the shell program or when the specified interval is used up, depending on which situation occurs first. If you want the function to wait until the shell ends, you need to pass the & hffff (often expressed by the constant infinite) value.
The following sample code shows how to use these APIs to set the shell for the program and wait for it to complete.
Const synchronize = & h100000
Const infinite = & hffff
Private sub shellprogramandwait (programname as string)
Dim hhandle as long, PID as long
Txtstatus. Text = "processing"
Txtstatus. Refresh
PID = shell (programname, vbnormalfocus)
If PID <> 0 then
Hhandle = OpenProcess (synchronize, 0 &, pid)
Waitforsingleobjecthhandle, infinite
Txtstatus. Text = "finished"
Else
Txtstatus. Text = "error shelling" & programname
End if
End sub
The waitforsingleobject function will effectively freeze your vbprogram. This is something you need to pay attention to, so that users will not think of a problem. For example, in this code, a text box control displays a message to the user. Note that the refresh (refresh) method ensures that the message is displayed before the program enters the waiting state. Then, when the shell program is terminated, a "finished" message is displayed.
Peter Aitken, author of this article: Since Visual Basic 1.0, Peter Aitken has been using VB programming. He has written many books and magazine articles about Visual Basic and its computer and programming.