Windows process start Detection

Source: Internet
Author: User


In windows, whether the process is started or not. In a recent project, you need to use the process status detection function. On the internet, there are three methods: 1. is to use GetExitCodeProcess (pi. hProcess, & dwExitCode); check whether the returned value of dwExitCode is STILL_ACTIVE or not. If it is still active, it indicates that it exists; 2. waitforsingleobject, determined by timeout. The specific implementation method can be checked online. 3. openprocess, which is determined by the return value. If the return value is NULL, it indicates that the process has been opened; otherwise, it has not been opened. The above three methods are all said by others, and some people say they have limitations. Let's talk about openprocess. In testing, we found that the thread was closed, but the return value was still NULL. Why ~~, There is no way to solve this problem in the most practical way. 4. check whether the process id matches the input table of the load system. If the process id matches, it indicates that the process is running. Otherwise, the process has crashed. The Code is as follows: # include <Tlhelp32.h> // if the process id is running, returns true of the Process, returns falsebool ProcessExist (DWORD process_id) {www.2cto.com PROCESSENTRY32 pe; DWORD id = 0; HANDLE hSnapshot = createconlhelp32snapshot (TH32CS_SNAPPROCESS, 0); pe. dwSize = sizeof (PROCESSENTRY32); if (! Process32First (hSnapshot, & pe) {CloseHandle (hSnapshot); return false;} while (1) {pe. dwSize = sizeof (PROCESSENTRY32); if (Process32Next (hSnapshot, & pe) = FALSE) {CloseHandle (hSnapshot); return false;} if (process_id = pe. th32ProcessID) {CloseHandle (hSnapshot); return true ;}}}

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.