In C++, you can easy to get all ProcessID on Operation System.By using PROCESSENTRY32, CreateToolhelp32Snapshot, Process32First API.
Now let's take a look
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:CreateToolhelp32Snapshot
HANDLE WINAPI CreateToolhelp32Snapshot(
_In_ DWORD dwFlags,
_In_ DWORD th32ProcessID
);
Parameters
dwFlags [in]The portions of the system to be included in the snapshot. This parameter can be one or more of the following values.
+ TH32CS_INHERIT
+ TH32CS_SNAPALL
+ TH32CS_SNAPHEAPLIST
+ TH32CS_SNAPMODULE
+ TH32CS_SNAPMODULE32
+ TH32CS_SNAPPROCESS
+ TH32CS_SNAPTHREAD
- th32ProcessID [in]
- The process identifier of the process to be included in the snapshot.
This parameter can be zero to indicate the current process. This
parameter is used when the TH32CS_SNAPHEAPLIST, TH32CS_SNAPMODULE, TH32CS_SNAPMODULE32, or TH32CS_SNAPALL value is specified. Otherwise, it is ignored and all processes are included in the snapshot.
If the specified process is the Idle process or one of the CSRSS processes, this function fails and the last error code is ERROR_ACCESS_DENIED because their access restrictions prevent user-level code from opening them.
If the specified process is a 64-bit process and the caller is a 32-bit process, this function fails and the last error code is ERROR_PARTIAL_COPY (299).
Return Value
If the function succeeds, it returns an open handle to the specified snapshot.If the function fails, it returns INVALID_HANDLE_VALUE. To get extended error information, call GetLastError. Possible error codes include ERROR_BAD_LENGTH.
Process32First
BOOL WINAPI Process32First(
_In_ HANDLE hSnapshot,
_Inout_ LPPROCESSENTRY32 lppe
);
Parameters
- hSnapshot [in]
- A handle to the snapshot returned from a previous call to the CreateToolhelp32Snapshot function.
- lppe [in, out]
- A pointer to a PROCESSENTRY32 structure. It contains process information such as the name of the executable file, the process identifier, and the process identifier of the parent process.
Return value
Returns TRUE if the first entry of the process list has been copied to the buffer or FALSE otherwise. The ERROR_NO_MORE_FILES error value is returned by the
GetLastError function if no processes exist or the snapshot does not contain process information.
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:Remarks
The calling application must set the dwSize member of
PROCESSENTRY32 to the size, in bytes, of the structure.
To retrieve information about other processes recorded in the same snapshot, use the
Process32Next function.
Source code Example:
This program will be Read All Process and get Skype process name/******************************************************************************** Developer: zidane (huuvi168@gmail.com) Last Modified: 2013-09-01 ********************************************************************************/ #include "stdafx.h" #include <tlhelp32.h> /* ----------------------------------------------------------------------- compare path with skype.exe, if equal, return true, otherwise return false --------------------------------------------------------------------- */ bool CTestDlg::CheckProcess(const wchar_t* path) { if ( wcscmp(path, L"skype.exe")==0 || wcscmp(path, L"skype.exe *32")==0 || wcscmp(path, L"Skype.exe")==0 ||wcscmp(path, L"Skype.exe *32")==0 ) return true; return false; } /* ----------------------------------------------------------------------- Browser All Process On OS, get process Id with Skype.exe file name and get Handle of Skype ProcessID --------------------------------------------------------------------- */ void
CTestDlg
::GetAnyProcess() { PROCESSENTRY32 entry; entry.dwSize = sizeof(PROCESSENTRY32); HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); if (Process32First(snapshot, &entry) == TRUE) { do { if (CheckProcess(entry.szExeFile) == 1) { // do something here... HANDLE hHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); CString szMsg = L""; szMsg.Format(L"entry.th32ProcessID = 0x%08X(%d) - hHandle = 0x%08X", entry.th32ProcessID, entry.th32ProcessID, hHandle); MessageBox(szMsg); } } while (Process32Next(snapshot, &entry) == TRUE); } CloseHandle(snapshot); }
Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about
Get all ProcessID on Operation System By CreateToolhelp32Snapshot API ,
please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋
Webzone Tech Tips, all things Tech Tips for web development
- I am Zidane, See you next time soon ✋✋✋✋