EnumProcesses() don't return all processses!
-
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 );
-
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 );
-
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 );
Why do you think, it was "not returned" ? :)
virtual void BeHappy() = 0;
-
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 );
Just a thought, but have you looked at
Process32First()
and related functions? -
Why do you think, it was "not returned" ? :)
virtual void BeHappy() = 0;
-
I saved the names of all processes, I debuged it and the names of all ather processes besides "NurOper.exe" were on the array .
...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
-
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 );
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
-
...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
-
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
Please see the usage of the field
ProcessEntry32.szExeFile
in the second function above :)virtual void BeHappy() = 0;