Thread: New to C++ and I need to enumerate through processes

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    New to C++ and I need to enumerate through processes

    I'm having a hard time. I have been a professional VMS developer at DEC but not a C++ developer. I've ordered a book but it hasn't gotten here yet.

    I need help on where to start.
    Code:
    #include <tlhelp32.h>
    
    typedef BOOL (__stdcall *PGetProcessMemoryInfo)(
    HANDLE Process, // handle to process
    PPROCESS_MEMORY_COUNTERS ppsmemCounters, // buffer
    DWORD cb // size of buffer
    );
    
    PGetProcessMemoryInfo pfGetProcessMemoryInfo;
    
    BOOL GetProcessList()
    {
    HANDLE hProcessSnap = NULL; 
    BOOL bRet = FALSE; 
    PROCESSENTRY32 pe32 = {0}; 
    int iCount = 0;
    // Take a snapshot of all processes in the system. 
    pfGetProcessMemoryInfo
    = (PGetProcessMemoryInfo)GetProcAddress(LoadLibrary("psapi"),"GetProcessMemoryInfo");
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
    if (hProcessSnap == INVALID_HANDLE_VALUE) 
    return (FALSE); 
    // Fill in the size of the structure before using it. 
    pe32.dwSize = sizeof(PROCESSENTRY32); 
    PPROCESS_MEMORY_COUNTERS pmc;
    ZeroMemory(&pmc,sizeof(pmc));
    // Walk the snapshot of the processes, and for each process, 
    // display information. 
    if (Process32First(hProcessSnap, &pe32)) 
    { 
    DWORD dwPriorityClass; 
    BOOL bGotModule = FALSE; 
    MODULEENTRY32 me32 = {0}; 
    do 
    { 
    bGotModule = GetProcessModule(pe32.th32ProcessID, 
    pe32.th32ModuleID+1, &me32, sizeof(MODULEENTRY32)); 
    sprintf(szTemp,"Module%d",count++);
    if (bGotModule) 
    { 
    HANDLE hProcess; 
    // Get the actual priority class. 
    hProcess = OpenProcess (PROCESS_ALL_ACCESS, 
    FALSE, pe32.th32ProcessID); 
    pfGetProcessMemoryInfo(hProcess,pmc,sizeof(pmc));
    dwPriorityClass = GetPriorityClass (hProcess); 
    CloseHandle (hProcess); 
    //your code .............
    //..........
    RefreshThreadList(pe32.th32ProcessID,iCount);
    iCount ++;
    } 
    } while (Process32Next(hProcessSnap, &pe32)); 
    bRet = TRUE; 
    } 
    else 
    bRet = FALSE; // could not walk the list of processes 
    // Do not forget to clean up the snapshot object. 
    CloseHandle (hProcessSnap); 
    return (bRet); 
    }
    
    BOOL GetProcessModule(DWORD dwPID, DWORD dwModuleID, LPMODULEENTRY32 lpMe32, DWORD cbMe32)
    {
    BOOL bRet = FALSE; 
    BOOL bFound = FALSE; 
    HANDLE hModuleSnap = NULL; 
    MODULEENTRY32 me32 = {0}; 
    // Take a snapshot of all modules in the specified process. 
    hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID); 
    //hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, dwPID); 
    if (hModuleSnap == INVALID_HANDLE_VALUE) 
    return (FALSE); 
    // Fill the size of the structure before using it. 
    me32.dwSize = sizeof(MODULEENTRY32); 
    
    // Walk the module list of the process, and find the module of 
    // interest. Then copy the information to the buffer pointed 
    // to by lpMe32 so that it can be returned to the caller. 
    if (Module32First(hModuleSnap, &me32)) 
    { 
    do 
    { 
    if (me32.th32ModuleID == dwModuleID) 
    { 
    CopyMemory (lpMe32, &me32, cbMe32); 
    bFound = TRUE; 
    } 
    } 
    while (!bFound && Module32Next(hModuleSnap, &me32)); 
    
    bRet = bFound; // if this sets bRet to FALSE, dwModuleID 
    // no longer exists in specified process 
    } 
    else 
    bRet = FALSE; // could not walk module list 
    // Do not forget to clean up the snapshot object. 
    CloseHandle (hModuleSnap); 
    return (bRet);
    }
    
    DWORD CEmuteFileDlg::RefreshThreadList(DWORD dwOwnerPID,int iCount)
    {
    HANDLE hThreadSnap = NULL; 
    BOOL bRet = FALSE; 
    THREADENTRY32 te32 = {0};
    int Step = 1;
    // Take a snapshot of all threads currently in the system. 
    hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); 
    if (hThreadSnap == INVALID_HANDLE_VALUE) 
    return (FALSE); 
    // Fill in the size of the structure before using it. 
    te32.dwSize = sizeof(THREADENTRY32); 
    // Walk the thread snapshot to find all threads of the process. 
    // If the thread belongs to the process, add its information 
    // to the display list.
    if (Thread32First(hThreadSnap, &te32)) 
    { 
    do 
    { 
    CString str,strList;
    if (te32.th32OwnerProcessID == dwOwnerPID) 
    { 
    //your Code.........
    //........
    Step ++;
    } 
    } 
    while (Thread32Next(hThreadSnap, &te32)); 
    bRet = TRUE; 
    } 
    else 
    bRet = FALSE; // could not walk the list of threads 
    // Do not forget to clean up the snapshot object. 
    CloseHandle (hThreadSnap); 
    return (bRet); 
    }
    I got this off the net. What kind of project do I put it in and where do I put it? I have no doubt that I'll have more questions.

    I have Windows 7 and VS2010. Oh yes, I'd prefer 64 bit to 32 bit code.
    Renee
    Last edited by ReneeCulver; 05-01-2011 at 06:34 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I can't read that big chunk of code you have there. What's your problem specifically?
    As far as i know, WinAPI programming is almost identical in all languages...
    Devoted my life to programming...

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indentation! We cannot read that big chunk of badly formatted mess!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Can we get this moved to the Windows board?

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is there a way to enumerate handles?
    By abachler in forum Windows Programming
    Replies: 6
    Last Post: 11-01-2007, 09:03 AM
  2. How to enumerate users...
    By dydoko in forum Windows Programming
    Replies: 1
    Last Post: 07-25-2004, 09:57 PM
  3. Enumerate all
    By geek@02 in forum Windows Programming
    Replies: 7
    Last Post: 06-14-2004, 05:57 PM
  4. Enumerate ALL CD-drives
    By Steph in forum Windows Programming
    Replies: 3
    Last Post: 06-04-2003, 12:24 AM
  5. Can't enumerate 'FAR' ??
    By Perspective in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2003, 05:10 PM