One-day Windows API training (88) enumprocesses Function
What if an error occurs when the software you developed runs on the user? Of course, you want to generate a report for the running environment information in case of an error, and then email it back to view it. Here we will introduce a function that can locate all the processes in the runtime environment and output the information of each process. Of course, this function can also be used in antivirus software to view suspicious process information.
The enumprocesses function is declared as follows:
Bool
Winapi
Enumprocesses (
DWORD * lpidprocess,
Dword cb,
DWORD * cbneeded
);
Lpidprocess is an array that stores process IDs.
CB is the size of the number of Process Groups.
Cbneeded is the size of the returned process array.
An example of calling a function is as follows:
#001 // obtain all processes of the system.
#002 // Cai junsheng 2007/12/12 QQ: 9073204 Shenzhen
#003 void testenumprocesses (void)
#004 {
#005 //
#006 const int nbufsize = 512;
#007 tchar chbuf [nbufsize];
#008 zeromemory (chbuf, nbufsize );
#009
#010 //
#011 DWORD dwprocs [1, 1024*2];
#012 DWORD dwneeded;
#013
#014 // enumerate all process IDs.
#015 if (! Enumprocesses (dwprocs, sizeof (dwprocs), & dwneeded ))
#016 {
#017 // output error information.
#018 wsprintf (chbuf, _ T ("enumprocesses failed (% d)./N"), getlasterror ());
#019 outputdebugstring (chbuf );
#020
#021 return;
#022}
#023
#024 // calculate the number of process IDs.
#025 DWORD dwproccount = dwneeded/sizeof (DWORD );
#026
#027 wsprintf (chbuf, _ T ("enumprocesses count (% d)./N"), dwproccount );
#028 outputdebugstring (chbuf );
#029
#030}What if an error occurs when the software you developed runs on the user? Of course, you want to generate a report for the running environment information in case of an error, and then email it back to view it. Here we will introduce a function that can locate all the processes in the runtime environment and output the information of each process. Of course, this function can also be used in antivirus software to view suspicious process information.
The enumprocesses function is declared as follows:
Bool
Winapi
Enumprocesses (
DWORD * lpidprocess,
Dword cb,
DWORD * cbneeded
);
Lpidprocess is an array that stores process IDs.
CB is the size of the number of Process Groups.
Cbneeded is the size of the returned process array.
An example of calling a function is as follows:
#001 // obtain all processes of the system.
#002 // Cai junsheng 2007/12/12 QQ: 9073204 Shenzhen
#003 void testenumprocesses (void)
#004 {
#005 //
#006 const int nbufsize = 512;
#007 tchar chbuf [nbufsize];
#008 zeromemory (chbuf, nbufsize );
#009
#010 //
#011 DWORD dwprocs [1, 1024*2];
#012 DWORD dwneeded;
#013
#014 // enumerate all process IDs.
#015 if (! Enumprocesses (dwprocs, sizeof (dwprocs), & dwneeded ))
#016 {
#017 // output error information.
#018 wsprintf (chbuf, _ T ("enumprocesses failed (% d)./N"), getlasterror ());
#019 outputdebugstring (chbuf );
#020
#021 return;
#022}
#023
#024 // calculate the number of process IDs.
#025 DWORD dwproccount = dwneeded/sizeof (DWORD );
#026
#027 wsprintf (chbuf, _ T ("enumprocesses count (% d)./N"), dwproccount );
#028 outputdebugstring (chbuf );
#029
#030}