Thread: Invalid Handle Error

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    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; 
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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
    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.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM