CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //Create a snapshot of the system process list and return a handle to the snapshot. If you don’t understand what snapshots and handles mean, go to Baidu. To simply understand, a snapshot is similar to a view in a database, and a handle is an unsigned integer, equivalent to an ID.
pe.dwSize=sizeof(PROCESSENTRY32); //This sentence explicitly indicates the size of the PROCESSENTRY32 structure. It seems a bit redundant, but MSDN says it cannot be omitted. I omitted it when I tested it myself. Something will go wrong.
if(!Process32First(hSnapshot,&pe)) return 0; //This sentence calls Process32First to start traversing the process list in the snapshot and copies the process information to the pe data structure. If it fails and returns FALSE, there is no need to execute subsequent Process32Next to traverse the entire list. This function can be used in conjunction with Process32Next to traverse the process list one by one. The Process32Next parameter is the same as Process32First. It also returns FALSE on failure. If successful, the information of the processes in the snapshot in the process list will be copied to the pe data structure one by one. It is worth mentioning that because process snapshots are used to obtain process information, if during this process one or some processes are closed by other programs or end by themselves, or a new process is added to the process queue , this change will not be reflected in the snapshot pointed to by hSnapShot. SnapShot is like taking a picture of someone. It only records a person's appearance at that moment. However, philosophically speaking, things are in motion and cannot represent the person's current situation. Similarly, the process snapshot here can only reflect the time when the snapshot was created. The status of all processes in the system's process queue. What are the benefits of using snapshots? That is, there is no need to repeatedly obtain the list of processes in the system every time when obtaining process information later. Instead, it is stored in the form of snapshots, so it is faster and helps protect data. The disadvantage is that it is not real-time. But usually a snapshot is enough.
if(strcmp(pe.szExeFile,name)==0) //This is easy to understand. It is to compare whether pe.szExeFile and name are the same. The specific comparison method is to compare the ASCII codes of the two. Difference, if you don’t understand ASCII code and strcmp, you can use Baidu to explain it.
id=pe.th32ProcessID;
return id;
The function of these two sentences together is to return the PID of the process, which is what you see in CMD TASKLIST The PID is the ID used to uniquely identify the process. Subsequent OpenProcess access process operations require this ID. You can use OpenProcess to get the process handle hProcess, and then call TerminateProcess hProcess,0 to force the end of a general process.
The middle CloseHandle(hSnapshot); //This sentence can be understood as garbage collection, and its function is to release the memory space identified by the snapshot handle created with CreateToolhelp32Snapshot.
In addition, the friend downstairs is right. Your code cannot compare process names in a case-insensitive manner. So it is best to add a processing process to unify the upper and lower case. I will give you an implementation method without using CString, for reference only:
In fact, you just need to write a function to convert all the characters to uppercase and then compare them.
//Inline function, used to return an empty string.
inline char* DefReturn()
{
char *ret = new char[1];
ret[0] = '\0';
return ret;
}
char* UCase(char *chOri)
{
if(chOri == NULL) return DefReturn();
size_t nLen = strlen(chOri);
if(nLen)
{< /p>
char *chRet = new char[nLen+1];
for(size_t index = 0; index { chRet[index] = toupper(chOri[index]); } chRet[index] = '\0'; return chRet ; } else { return chOri; } } //Convert all to uppercase and start comparing if(strcmp(UCase(pe.szExeFile),UCase(name))==0) { id=pe.th32ProcessID; break; }