Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tips] Get all ProcessID on Operation System By CreateToolhelp32Snapshot API

Wednesday 2 September 2015
|
Read: Completed in minutes

[Tips] Get all ProcessID on Operation System By CreateToolhelp32Snapshot API


In C++, you can easy to get all ProcessID on Operation System.By using PROCESSENTRY32, CreateToolhelp32Snapshot, Process32First API.

Now let's take a look




Get all ProcessID on Operation System By CreateToolhelp32Snapshot API









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.

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 ✋✋✋✋

🙇🏼 We Appreciate Your Comments and Suggestions - Webzone - all things Tech Tips web development 🙇🏼
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com