-
Invalid Handle Error
Hello,
I'm using the following function to get the information about a process and pass the process handle to a function that gets CPU usage. When I run this code on my Windows XP system, it works fine. However, when I run it on Windows Server 2003 system, I get an invalid handle error when I pass the handle to the other function. I think that the problem is in the privileges when I set the value of the handle.
Code:
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, pe32.th32ProcessID );
However, I'm not sure what to change it to. I would love some suggestions.
Here's the full code for getting the process id and calculating CPU usage
Code:
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,"WinLogonServer.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 );
}
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;
}
-
Perhaps you're running as "admin" locally, and as a "mortal" on the server.
Also, add actual error codes to the error reports so you can look up the reason for the failure rather than just having to deal with a useless "it didn't work" message.
Post moved to windows forum
-
Thanks for the advice. I added my account to the admin group of the server. I run it as an .exe under scheduled tasks, but now the code runs and never stops. It doesnt use up any CPU in task manager. It justs shows "running" on the scheduled tasks console. On my computer, it only takes a matter of seconds.