Analysis of Visual C ++ application implementation methods

Source: Internet
Author: User

Visual C ++ is a powerful Visual software development tool. With the development of science and technology in the Times, Visual C ++ applications have become the preferred tool for programmers to develop software, it is not only a C ++ compiler, but also a visual integrated development environment based on the Windows operating system.

So how to clean the process? In Windows, a tool named tskill.exe can be used to clear the processes of a program. After Entering "tskill program name", the running instances can be cleared. But how do I implement the tskill function in the code? To solve this problem, this example describes how to implement it in Windows2000.

I. Implementation Method

In Visual C ++ applications, the safest way to kill a process is to send the WM_CLOSE message to the main window of the running program. The implementation code is as follows:

 
 
  1. HWNDHwnd=This. M_hWnd; // obtain the Main Window
  2. PostMessage (hwnd, WM_CLOSE, 0, 0 );

After the message is sent, wait until the process is terminated. when the process is terminated, it sends a status signal and WaitForSingleObject returns WAIT_OBJECT_0. If another value is returned, the process is suspended or still being processed.

In this case, the only way to kill this process is to use the more powerful API function: TerminateProcess (). If you want to be nice, you can send a WM_QUERYENDSESSION message to the main window before closing. When the user ends the session (log out) or calls the ExitWindows () function, the application will receive the message.

Then, prepare to exit the process. A confirmation dialog box will pop up to tell you: "The program is coming soon. If you want to save the modification, it is the best time. Do you want to save it? "Three options are available (Yes/No/Cancel ). In addition, if you send a WM_QUERYENDSESSION message, you can reject the launch process (press the "Cancel key"). If so, the process will continue.

If the process to be closed is suspended, it is very important to use the SendMessageTimeout () function instead of the SendMessage () function. Its Parameter SMTO_NOTIMEOUTIFNOTHUNG is a flag only available for Windows 2000 and Windows XP.

  • A summary of C ++ Language Learning
  • Excellent object-oriented design language-C ++
  • What is the design principle of C ++?
  • Introduction to VC ++ applications and description of Modes
  • How to insert new elements for the C ++ Stack

The meaning is "If the thread is not suspended, do not Time Out". In other words, if the thread is working properly, wait forever so that the user can see the dialog box and decide what to do, after the user makes the final decision, SendMessageTimeout () will return with the corresponding bOKToKill value.

To enhance code reusability, this example encapsulates implementation details in a class named CFindKillProcess, including searching and killing processes. For details, see EnumProc. h and EnumProc. cpp. There are two other reusable classes in the file: CProcessIterator and cproceswiterator.

FindProcess (), a member function of the Visual C ++ application, looks for an input program. If this process is found, it returns the ID of the process and sends the ID to CFindKillProcess: KillProcess () function. The KillProcess () function encapsulates the closing window and termination logic.

It uses the cmaindomainwiterator class object to enumerate the process's main window (more than one, see "how to get the main window of a process and the name of the program that created the process? "), And send WM_CLOSE to every window, and then wait for the process to die. It has a Boolean parameter used to indicate whether to execute the TerminateProcess () function when the application process is unwilling to exit. For details, see the downloaded code.

Ii. programming steps

1. Start the Visual C ++ application, generate a console application, and name it "kp ";

2. Add the definition of CfindKillProcess and CProcessIterator classes to the program code;

3. Add code and 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.