enumerating process

This is a discussion on enumerating process within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I have this function that iterates through all the process and prints them: Code: int ProcessJudger (void){ // enumerazione ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    enumerating process

    Hi, I have this function that iterates through all the process and prints them:

    Code:
    int ProcessJudger (void){
      
      // enumerazione processi
      char szProcessName[MAX_PATH];
      DWORD aProcesses[1024], cbNeeded, cProcesses;
      unsigned int i;
      double tmp;
      
      // process identification number
      char tempid[MAX_PATH];
      char pid[MAX_PATH];
      int  PidNumber = 0;
      
      if ( !EnumProcesses( aProcesses,
                           sizeof(aProcesses),
                           &cbNeeded ) )
            return -1;
    
        cProcesses = cbNeeded / sizeof(DWORD);
        
        for ( i = 0; i < cProcesses; i++ ) {
          HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                         PROCESS_VM_READ,
                                         FALSE,
                                         aProcesses[i] );
          if ( NULL != hProcess ) {
              HMODULE hMod;
              DWORD cbNeeded;
    
              if ( EnumProcessModules( hProcess,
                                       &hMod,
                                       sizeof(hMod),
                                       &cbNeeded 
                                     ) )
              {
                  GetModuleBaseName( hProcess,
                                     hMod,
                                     szProcessName,
                                     sizeof(szProcessName));
              }// end nested IF
           }// end IF
           
           printf ("Processo: %s\n", szProcessName);
           
    
          CloseHandle( hProcess );
        }// end FOR
        
        printf("\n");
        
        return 0;
    }
    It seems to be working but it lists 3 times the same process, for example:

    Processo: calc.exe
    Processo: calc.exe
    Processo: smss.exe
    Processo: smss.exe
    Processo: winlogon.exe
    Processo: services.exe
    Processo: lsass.exe
    Processo: svchost.exe
    Processo: svchost.exe
    Processo: svchost.exe
    Processo: svchost.exe
    Processo: spoolsv.exe
    Processo: Explorer.EXE
    Processo: Explorer.EXE
    Processo: AVGUARD.EXE
    Processo: AVWUPSRV.EXE
    Processo: nvsvc32.exe
    Processo: SMAgent.exe
    Processo: svchost.exe
    Processo: svchost.exe
    Processo: vsmon.exe
    Processo: StartupMonitor.exe
    Processo: zlclient.exe
    Processo: AVGNT.EXE
    Processo: boincmgr.exe
    Processo: boinc.exe
    Processo: setiathome_4.18_windows_intelx86.exe
    Processo: msnmsgr.exe
    Processo: WISPTIS.EXE
    Processo: DevCpp.exe
    Processo: msimn.exe
    Processo: iexplore.exe
    Processo: cmd.exe
    Processo: Limiter.exe
    Processo: calc.exe
    any idea WHY it sees "calc.exe" (that is the last opened process) 3 times?
    Last edited by BianConiglio; 07-07-2005 at 05:23 PM.
    This forum is the best one I've ever seen. Great ppl, great coders

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Probably because there are 3 instances

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    he no! because with the task manager and other tools I see just ONE instance.. and the also the first 2 "calc.exe" have 0 as pid, it is an error, but where?
    This forum is the best one I've ever seen. Great ppl, great coders

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    and if I initialize szProcessName to "test", the first 2 processes will be "test"...strange!!
    This forum is the best one I've ever seen. Great ppl, great coders

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > printf ("Processo: %s\n", szProcessName);
    Well you do print whatever happened to be in the buffer last, whether or not some previous functions returned 'success' or 'failure'.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    -AppearingOnThis..........
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    44
    You need to have your printf() after all the functions, as some processes you aren't allowed to open without debugging privileges. At the moment you're just printing something without knowing if those functions succeeded or not.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thanx everyone I've put the printf into the :
    Code:
    if ( NULL != hProcess )
    the strange thing is that I have to filter the first result, infact if I don't initialize

    Code:
    char szProcessName[MAX_PATH]
    the first (bugged!) result is the last executed program.. if I initialize it to something, this something will be the first process found.. anyway I've filtered it out and it's working well, anyway I will debug better the code to see what it's really happening, because I'm afraid this will lead to some other nested bugs

    thanks to everyone
    This forum is the best one I've ever seen. Great ppl, great coders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 01:14 PM
  3. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  4. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  5. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21