End a Program, Given only its Window Caption

Category:
System/API
Type:
Snippets
Difficulty:
Intermediate
Author:
Andrew Chin

Version Compatibility: Visual Basic 6, Visual Basic 5

More information:
This sub illustrates one way to close a program, sending it a WM_CLOSE message. The program will then go through its normal shutdown process. Depending on how the program is written, it might shut down immediately, shut down depending on some other factor, such as user confirmation, or ignore the message completely.

Another function on the site, http://www.freevbcode.com/ShowCode.Asp?ID=935, shows how to immediately and unconditionally terminate an appication. The advantage of using the method presented here is that it allows the application to exit gracefully, without problems such as data loss.

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: