Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. EnumProcesses() don't return all processses!

EnumProcesses() don't return all processses!

Scheduled Pinned Locked Moved C / C++ / MFC
database
9 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aangerma
    wrote on last edited by
    #1

    Hello, I run EnumProcesses(), to find if the progam "NurOper.exe" is running I debuged it and although "NurOper.exe" is running its don't return it, its return all ather programs include my program- "nurman.exe" what can be the resone for it ,I must know if the progam "NurOper.exe" is running in order not to run it twice, thank for advance . the code is: bool IsRunning=false; int num=0; DWORD aProcesses[1024], cbNeeded, cProcesses; ULONG lCpuusage = 0; UINT index; double percent; if ( !EnumProcesses( aProcesses, sizeof(aProcesses),&cbNeeded ) ) return false; // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); CString str; for (int i = 0; i < cProcesses; i++ ) if( aProcesses[i] != 0 ) { TCHAR szProcessName[MAX_PATH] = TEXT(""); // Get a handle to the process. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,aProcesses[i] ); // Get the process name. if (NULL != hProcess ) { HMODULE hMod; DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod,sizeofhMod),&cbNeeded)) { GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) ); } } str=(CString)szProcessName; // Print the process name and identifier. if(str.Compare(L"nurman.exe")==0 ) if(num==0) num=1; else if(num==1) exit(1); if(str.Compare(L"NurOper.exe")==0 ) IsRunning=true; CloseHandle( hProcess ); } if(!IsRunning) _beginthread(runExe, 0, (void*)12 );

    N E J D 4 Replies Last reply
    0
    • A aangerma

      Hello, I run EnumProcesses(), to find if the progam "NurOper.exe" is running I debuged it and although "NurOper.exe" is running its don't return it, its return all ather programs include my program- "nurman.exe" what can be the resone for it ,I must know if the progam "NurOper.exe" is running in order not to run it twice, thank for advance . the code is: bool IsRunning=false; int num=0; DWORD aProcesses[1024], cbNeeded, cProcesses; ULONG lCpuusage = 0; UINT index; double percent; if ( !EnumProcesses( aProcesses, sizeof(aProcesses),&cbNeeded ) ) return false; // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); CString str; for (int i = 0; i < cProcesses; i++ ) if( aProcesses[i] != 0 ) { TCHAR szProcessName[MAX_PATH] = TEXT(""); // Get a handle to the process. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,aProcesses[i] ); // Get the process name. if (NULL != hProcess ) { HMODULE hMod; DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod,sizeofhMod),&cbNeeded)) { GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) ); } } str=(CString)szProcessName; // Print the process name and identifier. if(str.Compare(L"nurman.exe")==0 ) if(num==0) num=1; else if(num==1) exit(1); if(str.Compare(L"NurOper.exe")==0 ) IsRunning=true; CloseHandle( hProcess ); } if(!IsRunning) _beginthread(runExe, 0, (void*)12 );

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #2

      The Practical Guide to Multithreading - Part 2[^] discusses why this is a bad solution. Read Example 4 if you missed that. The best solution for this problem is to use a mutex. All covered in the article.

      home

      1 Reply Last reply
      0
      • A aangerma

        Hello, I run EnumProcesses(), to find if the progam "NurOper.exe" is running I debuged it and although "NurOper.exe" is running its don't return it, its return all ather programs include my program- "nurman.exe" what can be the resone for it ,I must know if the progam "NurOper.exe" is running in order not to run it twice, thank for advance . the code is: bool IsRunning=false; int num=0; DWORD aProcesses[1024], cbNeeded, cProcesses; ULONG lCpuusage = 0; UINT index; double percent; if ( !EnumProcesses( aProcesses, sizeof(aProcesses),&cbNeeded ) ) return false; // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); CString str; for (int i = 0; i < cProcesses; i++ ) if( aProcesses[i] != 0 ) { TCHAR szProcessName[MAX_PATH] = TEXT(""); // Get a handle to the process. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,aProcesses[i] ); // Get the process name. if (NULL != hProcess ) { HMODULE hMod; DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod,sizeofhMod),&cbNeeded)) { GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) ); } } str=(CString)szProcessName; // Print the process name and identifier. if(str.Compare(L"nurman.exe")==0 ) if(num==0) num=1; else if(num==1) exit(1); if(str.Compare(L"NurOper.exe")==0 ) IsRunning=true; CloseHandle( hProcess ); } if(!IsRunning) _beginthread(runExe, 0, (void*)12 );

        E Offline
        E Offline
        Eugen Podsypalnikov
        wrote on last edited by
        #3

        Why do you think, it was "not returned" ? :)

        virtual void BeHappy() = 0;

        A 1 Reply Last reply
        0
        • A aangerma

          Hello, I run EnumProcesses(), to find if the progam "NurOper.exe" is running I debuged it and although "NurOper.exe" is running its don't return it, its return all ather programs include my program- "nurman.exe" what can be the resone for it ,I must know if the progam "NurOper.exe" is running in order not to run it twice, thank for advance . the code is: bool IsRunning=false; int num=0; DWORD aProcesses[1024], cbNeeded, cProcesses; ULONG lCpuusage = 0; UINT index; double percent; if ( !EnumProcesses( aProcesses, sizeof(aProcesses),&cbNeeded ) ) return false; // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); CString str; for (int i = 0; i < cProcesses; i++ ) if( aProcesses[i] != 0 ) { TCHAR szProcessName[MAX_PATH] = TEXT(""); // Get a handle to the process. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,aProcesses[i] ); // Get the process name. if (NULL != hProcess ) { HMODULE hMod; DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod,sizeofhMod),&cbNeeded)) { GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) ); } } str=(CString)szProcessName; // Print the process name and identifier. if(str.Compare(L"nurman.exe")==0 ) if(num==0) num=1; else if(num==1) exit(1); if(str.Compare(L"NurOper.exe")==0 ) IsRunning=true; CloseHandle( hProcess ); } if(!IsRunning) _beginthread(runExe, 0, (void*)12 );

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          Just a thought, but have you looked at Process32First() and related functions?

          1 Reply Last reply
          0
          • E Eugen Podsypalnikov

            Why do you think, it was "not returned" ? :)

            virtual void BeHappy() = 0;

            A Offline
            A Offline
            aangerma
            wrote on last edited by
            #5

            I saved the names of all processes, I debuged it and the names of all ather processes besides "NurOper.exe" were on the array .

            E 1 Reply Last reply
            0
            • A aangerma

              I saved the names of all processes, I debuged it and the names of all ather processes besides "NurOper.exe" were on the array .

              E Offline
              E Offline
              Eugen Podsypalnikov
              wrote on last edited by
              #6

              ...and "nUrOpEr.exe" - as well ? :) I would perform it by the combination of - Process32First: EXE name stage, and - Module32First: EXE directory stage Please see the following example :) :

              bool GetPathFromPID(DWORD dwPID, CString& cszFullName)
              {
              bool bResult = false;
              cszFullName.Empty();

              HANDLE hToolHelp = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);

              if (INVALID_HANDLE_VALUE != hToolHelp) {
              MODULEENTRY32 ModuleEntry32;
              ModuleEntry32.dwSize = sizeof(MODULEENTRY32);

              if (Module32First(hToolHelp, &ModuleEntry32)) {
                do {
                  CString cszModule(ModuleEntry32.szModule);
                  CString cszExePath(ModuleEntry32.szExePath);
                  if (0 == cszExePath.Right(cszModule.GetLength()).CompareNoCase(cszModule)) {
                    cszFullName = cszExePath;
                    bResult = true;
                    break;
                  }
                } while (Module32Next(hToolHelp, &ModuleEntry32));
              }
              CloseHandle(hToolHelp);
              

              }

              return bResult;
              }

              bool GetRunningProcesses(const CString& csDir,
              CStringArray& arrProcesse,
              CDWordArray& arrProcessID,
              DWORD* pdwFoundCount)
              {
              arrProcesse.RemoveAll();
              arrProcessID.RemoveAll();

              if (pdwFoundCount) {
              *pdwFoundCount = 0;
              }

              HANDLE hToolHelp = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

              if (hToolHelp == INVALID_HANDLE_VALUE) {
              return false;
              }

              PROCESSENTRY32 ProcessEntry32;
              ProcessEntry32.dwSize = sizeof(PROCESSENTRY32);

              CString cszOurDir(csDir);
              cszOurDir.MakeUpper();

              if (Process32First(hToolHelp, &ProcessEntry32)) {
              do {
              if (ProcessEntry32.th32ProcessID) {
              CString cszExePath;
              if (GetPathFromPID(ProcessEntry32.th32ProcessID, cszExePath)) {
              cszExePath.MakeUpper();
              if (-1 != cszExePath.Find(cszOurDir)) {
              arrProcesse.Add(ProcessEntry32.szExeFile);
              arrProcessID.Add(ProcessEntry32.th32ProcessID);
              }
              }
              }
              } while (Process32Next(hToolHelp, &ProcessEntry32));
              }
              CloseHandle(hToolHelp);

              if (pdwFoundCount) {
              *pdwFoundCount = arrProcessID.GetCount();
              }

              return true;
              }

              PS: an administrator only can see the all processes by this code, other users can see the own (their) processes only... :)

              virtual void BeHappy() = 0;

              modified on Monday, September 6, 2010 4:58 AM

              A 1 Reply Last reply
              0
              • A aangerma

                Hello, I run EnumProcesses(), to find if the progam "NurOper.exe" is running I debuged it and although "NurOper.exe" is running its don't return it, its return all ather programs include my program- "nurman.exe" what can be the resone for it ,I must know if the progam "NurOper.exe" is running in order not to run it twice, thank for advance . the code is: bool IsRunning=false; int num=0; DWORD aProcesses[1024], cbNeeded, cProcesses; ULONG lCpuusage = 0; UINT index; double percent; if ( !EnumProcesses( aProcesses, sizeof(aProcesses),&cbNeeded ) ) return false; // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); CString str; for (int i = 0; i < cProcesses; i++ ) if( aProcesses[i] != 0 ) { TCHAR szProcessName[MAX_PATH] = TEXT(""); // Get a handle to the process. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,aProcesses[i] ); // Get the process name. if (NULL != hProcess ) { HMODULE hMod; DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod,sizeofhMod),&cbNeeded)) { GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) ); } } str=(CString)szProcessName; // Print the process name and identifier. if(str.Compare(L"nurman.exe")==0 ) if(num==0) num=1; else if(num==1) exit(1); if(str.Compare(L"NurOper.exe")==0 ) IsRunning=true; CloseHandle( hProcess ); } if(!IsRunning) _beginthread(runExe, 0, (void*)12 );

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                How many processes does Task Manager report? How does that compare to cProcesses?

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Man who follows car will be exhausted." - Confucius

                1 Reply Last reply
                0
                • E Eugen Podsypalnikov

                  ...and "nUrOpEr.exe" - as well ? :) I would perform it by the combination of - Process32First: EXE name stage, and - Module32First: EXE directory stage Please see the following example :) :

                  bool GetPathFromPID(DWORD dwPID, CString& cszFullName)
                  {
                  bool bResult = false;
                  cszFullName.Empty();

                  HANDLE hToolHelp = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);

                  if (INVALID_HANDLE_VALUE != hToolHelp) {
                  MODULEENTRY32 ModuleEntry32;
                  ModuleEntry32.dwSize = sizeof(MODULEENTRY32);

                  if (Module32First(hToolHelp, &ModuleEntry32)) {
                    do {
                      CString cszModule(ModuleEntry32.szModule);
                      CString cszExePath(ModuleEntry32.szExePath);
                      if (0 == cszExePath.Right(cszModule.GetLength()).CompareNoCase(cszModule)) {
                        cszFullName = cszExePath;
                        bResult = true;
                        break;
                      }
                    } while (Module32Next(hToolHelp, &ModuleEntry32));
                  }
                  CloseHandle(hToolHelp);
                  

                  }

                  return bResult;
                  }

                  bool GetRunningProcesses(const CString& csDir,
                  CStringArray& arrProcesse,
                  CDWordArray& arrProcessID,
                  DWORD* pdwFoundCount)
                  {
                  arrProcesse.RemoveAll();
                  arrProcessID.RemoveAll();

                  if (pdwFoundCount) {
                  *pdwFoundCount = 0;
                  }

                  HANDLE hToolHelp = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

                  if (hToolHelp == INVALID_HANDLE_VALUE) {
                  return false;
                  }

                  PROCESSENTRY32 ProcessEntry32;
                  ProcessEntry32.dwSize = sizeof(PROCESSENTRY32);

                  CString cszOurDir(csDir);
                  cszOurDir.MakeUpper();

                  if (Process32First(hToolHelp, &ProcessEntry32)) {
                  do {
                  if (ProcessEntry32.th32ProcessID) {
                  CString cszExePath;
                  if (GetPathFromPID(ProcessEntry32.th32ProcessID, cszExePath)) {
                  cszExePath.MakeUpper();
                  if (-1 != cszExePath.Find(cszOurDir)) {
                  arrProcesse.Add(ProcessEntry32.szExeFile);
                  arrProcessID.Add(ProcessEntry32.th32ProcessID);
                  }
                  }
                  }
                  } while (Process32Next(hToolHelp, &ProcessEntry32));
                  }
                  CloseHandle(hToolHelp);

                  if (pdwFoundCount) {
                  *pdwFoundCount = arrProcessID.GetCount();
                  }

                  return true;
                  }

                  PS: an administrator only can see the all processes by this code, other users can see the own (their) processes only... :)

                  virtual void BeHappy() = 0;

                  modified on Monday, September 6, 2010 4:58 AM

                  A Offline
                  A Offline
                  aangerma
                  wrote on last edited by
                  #8

                  First,thank you for yours helpfull answer, I would like to ask you how can I have an access to the name of each procces, i.e I need to chack whether certain program is active. thanks again

                  E 1 Reply Last reply
                  0
                  • A aangerma

                    First,thank you for yours helpfull answer, I would like to ask you how can I have an access to the name of each procces, i.e I need to chack whether certain program is active. thanks again

                    E Offline
                    E Offline
                    Eugen Podsypalnikov
                    wrote on last edited by
                    #9

                    Please see the usage of the field ProcessEntry32.szExeFile in the second function above :)

                    virtual void BeHappy() = 0;

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups