In vbs, start two applications until one of them ends, and then close the other?

Source: Internet
Author: User

Q:
Hi, script expert! This is the task I want to complete: I want to use a script to start two executable files. First Application Program After closing, I want this script to close the second application and then exit. How to complete the preceding tasks?

-- Mk

A:
Hello, MK. You know, this is what we like. Why? It sounds complicated and tricky. If someone is looking for us to do something, we can say, "You know, I'm trying to write a script that can start two applications. After the first one is closed, then the second one is automatically disabled." Then they will say, "oh, sorry. Obviously, you are very busy. "Then we will not be looking for us.

Of course, they don't know. It just sounds very difficult. In fact, the difficulty is equivalent to the following script:

CopyCode The Code is as follows: strcomputer = "."
Set ob1_miservice = GetObject ("winmgmts: \" & strcomputer & "\ Root \ cimv2: win32_process ")

Errresult = obw.miservice. Create ("calc.exe", null, null, intcalcid)
Errresult = obw.miservice. Create ("notepad.exe", null, null, intnotepadid)

Set ob1_miservice = GetObject ("winmgmts: \" & strcomputer & "\ Root \ cimv2 ")

Set colprocesses = obw.miservice. execnotificationquery _
("Select * from _ instancedeletionevent "_
& "Within 1 where targetinstance ISA 'win32 _ process '")

Do until I = 999
Set objprocess = colprocesses. nextevent
If objprocess. targetinstance. processid = intcalcid then
Exit do
End if
Loop

Set colprocesses = obw.miservice. execquery _
("Select * From win32_process where processid =" & intnotepadid)

For each objprocess in colprocesses
Objprocess. Terminate ()
Next

Really, please believe us: After you understand the work done by the script, this is actually quite simple. First, we connect to the WMI Service on the computer. Specifically, we bind it to the win32_process class. This is what we want to do now:

Set ob1_miservice = GetObject ("winmgmts: \" & strcomputer & "\ Root \ cimv2: win32_process ")

Then, we use the Create method to create two new processes: calc.exe and notepad.exe. For each new process, we use code similar to the following line of code:

Errresult = obw.miservice. Create ("calc.exe", null, null, intcalcid)

All we have to do is call the create method with the following content:

• Name of the executable file (you may need to specify the full path name of the application, depending on your computer settings ).

• A pair of null parameters. With these two parameters, we can specify different parameters for the ApplicationCompositionFolder and configure some other startup options. In this sample code, we do not need to consider these things, so we just set the parameter value to null.

• Variables acting as "Output Parameters" (named intcalcid ). After these processes are created, the processid number assigned to the process is also allocated to these output parameter variables.

The final result is that we start the calculator and the variable intcalcid contains the process ID assigned to the calculator instance. Then, we start notepad and the variable intnotepadid contains the processid allocated to the notepad instance. This is how to start two applications and trace them.

What we need to do next is: Well, there is nothing left: We need to pause the script until the "Calculator" is disabled ". To complete this task, we re-connect to the WMI Service and use execnotificationquery to monitor any deleted processes. We need to re-connect to the WMI Service because we only connect to the win32_process class at the beginning of the script; therefore, Object Reference (ob1_miservice) only references this class. We need to connect to the "General" WMI Service, so we just re-use the object to reference obw.miservice and perform a new connection:

Set colprocesses = obw.miservice. execnotificationquery _
("Select * from _ instancedeletionevent "_
& "Within 1 where targetinstance ISA 'win32 _ process '")

Why? Each time a process is deleted, A _ instancedeletionevent class instance is generated. Check each instance to see whether the process ID of these instances is the target ID, that is, the ID assigned to intcalcid. If the deleted process has different IDs, it is not a "Calculator" instance; in this case, the script resumes monitoring. If the deleted process has the same ID as intcalcid, it must be a "Calculator" instance (because the process ID must be unique ). In this case, we need to stop monitoring and then close notepad ".

The following is the actual code for monitoring:

Do until I = 999
Set objprocess = colprocesses. nextevent
If objprocess. targetinstance. processid = intcalcid then
Exit do
End if
Loop

Here we set a loop, which runs until the variable I equals 999. Now, the fact is that variable I will never be equal to 999; this is just a trick to make sure that the loop continues until the calculator is closed. (How do we know that variable I will never be 999? Yes, we didn't assign a value to I. Therefore, it takes the default value 0. Because we have never changed this value, I is always 0, so it will never be equal to 999 .)

In the loop, we use this line of code to wait for the next deletion process:

Set objprocess = colprocesses. nextevent

Each time a process is deleted, we check whether the processid matches the process ID assigned to the calculator. If yes, run the exit do command to disconnect the loop and continue the script. If it does not have the same ID, we only need to continue the loop and wait for the next deletion process. (As we mentioned above, I will never be equal to 999, but it doesn't matter: Use the exit do command to exit the loop .)

Note. We found that we had a rough grasp of the whole idea of event monitoring. If you are confused about the content such as _ instancedeletionevent and colprocesses. nextevent, see web broadcast protection by the scripting experts before WMI event introduction ).

Now, we only need to terminate the "Notepad" instance we started. To complete this task, we use this WMI query to retrieve a set of all processes with the process ID assigned to notepad:

Set colprocesses = obw.miservice. execquery _
("Select * From win32_process where processid =" & intnotepadid)

After this set is obtained, we use this code block to loop through the entire process set (with only one process), and then use the terminate method to close the application:

For each objprocess in colprocesses
Objprocess. Terminate ()
Next

By the way, this method applies to both remote and local computers. You only need to change the value of the variable strcomputer to the name of the remote computer. However, remember that in Windows XP and Windows Server 2003, processes started on remote computers run in invisible windows; they are invisible on the screen. This means that this method is useful for applications that do not require any user interaction when processing remote computers. For applications that do require user intervention, this method is far less useful than other methods (actually completely useless ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.