How to "cleanly" terminate an application in Win32

Source: Internet
Author: User
Tags final terminates thread win32

Summary

In an ideal environment, a process might require another process to shut down through some form of interprocess communication. However, if you have no source-level control over the application you want to shut down, you may not have the option to do so. Although there is no way to ensure "cleanly" shutdown of applications in Win32, there are steps you can take to ensure that your application uses the best way to clear resources.

32-bit processes (and 16-bit processes under Windows 95)

Under Win32, the operating system can ensure that the resources owned by the process are purged when the process shuts down. However, this does not mean that the process itself will have the opportunity to perform any final information refresh on the disk or perform any final communication over a remote connection, nor does it mean that the process's DLL will have an opportunity to execute its Process_detach code. This is often the best reason to avoid terminating applications under Windows 95 and Windows NT.

If you must close the process, follow these steps:

Send a WM_CLOSE message to all top-level windows owned by the process you intend to shut down. Many Windows applications respond to this message by shutting itself down.

Note: The response of a console application to wm_close depends on whether it has a control handler installed.

Use EnumWindows () to locate the handle to the target window. In the callback function, check that the process ID of the window matches the process you are shutting down. You can do this by calling GetWindowThreadProcessId (). After the match is determined, use PostMessage () or sendmessagetimeout () to send the WM_CLOSE message to the window.

Use WaitForSingleObject () to wait for the handle of the process. Make sure that you use the timeout value to wait, because in many cases wm_close does not close the application. Keep in mind that the timeout value should be long enough (through WaitForSingleObject () or sendmessagetimeout ()) so that users can respond to any dialog boxes created to handle the WM_CLOSE message.

If the return value is WAIT_OBJECT_0, the application has cleanly closed itself. If the return value is Wait_timeout, you must close the application using TerminateProcess ().

Note: If the return value obtained from WaitForSingleObject () is not wait_object_0 or wait_timeout, you should use GetLastError () to find out why. By performing these steps, you are completely likely to cleanly shut down the application (without IPC or user intervention).

16-bit problem (under Windows NT)

The above steps apply to 16-bit applications under Windows 95, while the 16-bit applications under Windows NT work differently from the 16-bit applications under Windows 95.

Under Windows NT, all 16-bit applications run in a virtual DOS machine (VDM). This VDM is run as a Win32 process (NTVDM) under Windows NT. The NTVDM process has a process ID. You can get the handle of the process by OpenProcess (), just as you would any other Win32 process. However, none of the 16-bit applications running in VDM have a process ID, so you cannot get the process handle from OpenProcess (). Each 16-bit application in VDM has a 16-bit task handle and a 32-bit thread of execution. The task handle and thread ID can be found by calling the function VDMEnumTaskWOWEx (). For additional information about this, see: "How to enumerate application windows and processes with Win32 APIs."

The first and most straightforward way to turn off the 16-bit application under Windows NT is to close the entire NTVDM process. You can do this by performing the steps described earlier. You only need to know the NTVDM process ID, and look for the NTVDM process ID by referring to the method described in "How to enumerate application windows and processes with Win32 APIs." The disadvantage of this approach is that it closes all 16-bit applications that run in that VDM. If this is not the result you want, you need to take another approach.

If you want to close a single 16-bit application in the NTVDM process, you need to follow these steps:

Send a WM_CLOSE message to all top-level windows owned by the process and with the same thread ID as the 16-bit task you want to close. The most effective way to do this is to use EnumWindows (). In the callback function, check that the process ID and thread ID of the window match the 16-bit task to be closed. Keep in mind that the process ID becomes the NTVDM process ID in which the 16-bit application runs.

Although you have a thread ID, you cannot wait for the 16-bit process to terminate. Therefore, you must wait for any length of time (to allow a clean shutdown), and then try to close the application. This operation is not valid if the application is closed. If the application is not closed, it terminates the application.

Terminates the application using a function called Vdmterminatetaskwow (), which can be found in Vdmdbg.dll. It takes the process ID of VDM and the task number of the 16-bit task.

This method allows you to close a single 16-bit application in a VDM under Windows NT. However, 16-bit Windows and wowexec that are running in VDM do not effectively purge resources that have terminated tasks. If you are looking for a way to cleanly terminate a 16-bit application under Windows NT, consider terminating the entire VDM process. Note: If you want to start a 16-bit application that may terminate later, use CREATE_SEPARATE_WOW_VDM with CreateProcess ().

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.