Program running long

This is a discussion on Program running long within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I have a program that is getting a process id and calculating its cpu usage. When I run it ...

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

    Program running long

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

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    or is it a permissions issue
    I'll take a wild guess since I have absolutely no idea how your server is configured. But the default DACL does allow intrusive access for the creator of the process and LOCAL_SYSTEM. If a process is created by the admin account, another account cannot open it. And vice versa unless the Admin account enables the debug privilege. Thus, try enabling the debug privilege prior to opening the process.

    Also, keep in mind that your network administrator may have disabled SeDebugPrivilege on the server.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    317
    try enabling the debug privilege prior to opening the process.
    just wondering how do you do that?

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Quote Originally Posted by Anddos View Post
    try enabling the debug privilege prior to opening the process.
    just wondering how do you do that?
    Check out the sample code at this link. Especially, the EnableTokenPrivilege function. You'll have to set the privilege level prior to entering your loop that opens the processes etc.

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, 02:07 AM
  2. Stop program from running twice?
    By Abda92 in forum C Programming
    Replies: 19
    Last Post: 03-17-2008, 01:35 PM
  3. Quick, Partiotion, and Insertion Sort
    By silicon in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2005, 08:47 PM
  4. Knight's Tour Recursion Problem
    By thephreak6 in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 09:18 AM
  5. Why is my program running away?
    By badkitty in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2001, 04:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21