Clean up a process with Visual C + +

Source: Internet
Author: User
Tags class definition thread

Reader friends may often run into a problem where they are not able to manipulate files when they find that they are being used by other programs, are open, and are open exclusively. Therefore, to manipulate these files, you must clear the process that opened the files. So how do you clean the process cleanly? In fact, in the Windows2000 operating system version of a tool called Tskill.exe, you can use it to clear off the process of a program, after entering the "Tskill program name" can clear its running instance. But how do you implement the Tskill function in your code? To solve this problem, this example introduces the method implemented under Windows2000.

One, the realization method

In Visual C + + programming, the safest way to kill a process is to send a WM_CLOSE message to the main window that runs the program, which implements the following code:

HWND hwnd =this.m_hWnd; // 获得主窗口
PostMessage(hwnd, WM_CLOSE, 0, 0);

When this message is sent, it should normally wait until the process does terminate, it emits a status signal when the process terminates, and WaitForSingleObject returns WAIT_OBJECT_0. If a different value is returned, the process is either suspended or still being processed. In this case, the only way to kill the process is to use a more powerful API function: TerminateProcess (). If you want to do it nicely, you can send a WM_QUERYENDSESSION message to the main window before you close it, and when the user ends the session (log out) or calls the ExitWindows () function, the application receives the message and prepares to exit the process. At this time generally will pop up a confirmation dialog box, told the user: "The program to launch, if you want to save the modified things, now is the best time, want to save?" "There are three kinds of options (yes/no/cancel). In addition, sending a WM_QUERYENDSESSION message can deny the eject process (press "Cancel Key"), and if so, the process will continue.

If the process you want to close is suspended, it is important to use the SendMessageTimeout () function, rather than using the SendMessage () function, whose argument Smto_notimeoutifnothung is a Windows 2000 and Windows XP are the only signs. The meaning is "if the thread does not hang, do not timeout", in other words, if the thread is doing normal processing, then wait forever so that the user can see the dialog box and decide what to do, when the user finally makes a decision, SendMessageTimeout () will return with the corresponding Boktokill value.

In order to enhance the reusability of the Code, the implementation details are encapsulated in a class called Cfindkillprocess, which includes finding and killing processes, see ENUMPROC.H and EnumProc.cpp files for details. There are two other reusable classes in the file, one is Cprocessiterator and the other is cwindowiterator. This is described in detail in the example "getting the main window of a process" and the name of the program that created the process.

The member function of the Cfindkillprocess class Findprocess () finds a program that, if found, returns the ID of the process, and then passes this ID to the cfindkillprocess::killprocess () function. The KillProcess () function encapsulates the closing window and the terminating logic, which uses the Cmainwindowiterator class object to enumerate the main windows of the process (perhaps more than one, see "How do I get the main window of a process and the program name that created the process?"). "), and sends wm_close to every window, and then waits for the process to die. It has a Boolean parameter that indicates whether to execute the terminateprocess () function when the application process is unwilling to exit. For more details, see the downloaded code.

Second, programming steps

1, start visual c++6.0, generate a console application, the program named "KP";

2, in the program code to add cfindkillprocess, Cprocessiterator class definition;

3, add code, compile and run the program.

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.