Hello,
I have a program that is getting a process id and calculating its cpu usage. When I run it on my computer, it runs for a second or two and completes successfully. I ran it on a server using an interactive user account. When I did this, it would error saying that it could not create a handle. When I ran it as an administrator, the program would run. However, the program never stops and it doesnt write to a log file I created successfully. I am wondering if there is something that is wrong with my code that is causing this, or is it a permissions issue. I appreciate any help.
Code:int iFunGetCpuProcUsage( HANDLE hHandle )
{
LONG lOldIdle, lOldUser, lNewUser, lOldKernel, lNewKernel, lNewIdle, lProcUsage, lUser, lKernel, lIdle, lSys;
DWORD dwOldTime, dwNewTime, dwTime;
FILETIME idleTime, kernelTime, userTime, ftCreate, ftExit, ftUser, ftKernel;
int iProcUsage;
dwOldTime = timeGetTime();
if( ! GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftUser, &ftKernel ) )
{
printf("error old getprocesstime %d", GetLastError() );
getch();
}
lOldUser = iFunGetTime( ftUser );
lOldKernel = iFunGetTime( ftKernel );
Sleep( 1000 );
dwNewTime = timeGetTime();
if( ! GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftUser, &ftKernel ) )
{
printf("error new getprocesstime %d", GetLastError() );
getch();
}
lNewUser = iFunGetTime( ftUser );
lNewKernel = iFunGetTime( ftKernel );
lKernel = lNewKernel - lOldKernel;
lUser = lNewUser - lOldUser;
dwTime = dwNewTime-dwOldTime;
if( dwTime == 0 )
{
Sleep( 100 );
dwNewTime = timeGetTime();
dwTime = dwNewTime-dwOldTime;
}
iProcUsage = (((lKernel+lUser)*100 ) / (Number_of_Processes * dwTime) );
return iProcUsage;
}
BOOL GetProcessList(void)
{
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass;
char ProcessID[10];
char CPUUsage[10];
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE )
{
printError( TEXT("CreateToolhelp32Snapshot (of processes)") );
return( FALSE );
}
// Set the size of the structure before using it.
pe32.dwSize = sizeof( PROCESSENTRY32 );
// Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32First( hProcessSnap, &pe32 ) )
{
printError( TEXT("Process32First") ); // show cause of failure
CloseHandle( hProcessSnap ); // clean the snapshot object
return( FALSE );
}
// Now walk the snapshot of processes, and
// display information about each process in turn
do
{
// Retrieve the priority class.
dwPriorityClass = 0;
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
if( hProcess == NULL )
printError( TEXT("OpenProcess") );
else
{
dwPriorityClass = GetPriorityClass( hProcess );
if( !dwPriorityClass )
printError( TEXT("GetPriorityClass") );
CloseHandle( hProcess );
}
if( strstr(pe32.szExeFile,"ppserver.exe"))
{
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, pe32.th32ProcessID );
itoa(pe32.th32ProcessID, ProcessID, 10);
itoa(iFunGetCpuProcUsage( hProcess ), CPUUsage, 10);
LogMessage("The Process ID for", pe32.szExeFile, "is", "...." , ProcessID);
LogMessage("The CPU Usage for", pe32.szExeFile, "is", CPUUsage ,"%");
}
}
while( Process32Next( hProcessSnap, &pe32 ) );
CloseHandle( hProcessSnap );
return( TRUE );
}