How to enumerate all device drivers in the system

24 02 2009

description5

 

 

Does anybody know how to enumerate all device drivers in our system.

howcanidoit6

 

 

PSAPI itself giving differnt APIs to do this. EnumDeviceDrivers and GetDeviceDriverBaseName are the APIs used for this.

mycodesnippet5

 

 

void DisplayDeviceDrivers()
{
   LPVOID   drivers[ARRAY_SIZE];
   DWORD    cbNeeded;
   int      cDrivers, i;
  
   if( EnumDeviceDrivers(drivers, sizeof(drivers), &cbNeeded) &&
      cbNeeded < sizeof(drivers))
   {
      TCHAR szDriver[ARRAY_SIZE];
     
      cDrivers = cbNeeded/sizeof(drivers[0]);
     
      AfxMessageBox("Total Device Drivers count : %ul", cDrivers);
     
      for (i=0; i < cDrivers; i++ )
      {
         GetDeviceDriverBaseName( drivers[i], szDriver, sizeof(szDriver)/sizeof(szDriver[0]) );
         CString str;
         str.Format("%d: %s", i+1, szDriver);
         AfxMessageBox(str);
        
      }
   }
   else
      AfxMessageBox("EnumDeviceDrivers failed.");
}

mynote5

 

 

Don’t forget to include Psapi.lib to library modules.





RAM Booster

11 02 2009

description4

 

 

Computer RAM memory is very important because it allows the application running quickly. When we run large number of programs it may consumes all RAM memory, then our computer has to use hard disk as the memory. But the hard disk is very slower than the RAM memory, so the computer becomes too slow.

howcanidoit5

 

 

So we need to free up or releasing some memory safelyPSAPI having different APIs to implement this. The EmptyWorkingSet API function removes as many pages as possible from the working set of the specified process. So first of all we need to get the process handles by using the API EnumProcesses. Please look on my previous post to see how to get the handles of all the process running in your machine.

mycodesnippet4

 

 

void BoostMyRAM()
{ 
   // Get the list of process identifiers.  
   DWORD aProcesses[1024], cbNeeded, cProcesses;
   unsigned int i;
  
   //This returns a list of handles to processes running on
   //the system as an array.
   if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
      return;
  
   // Calculate how many process identifiers were returned.  
   cProcesses = cbNeeded / sizeof(DWORD);
  
   for ( i = 0; i < cProcesses; i++ )
   {
      if( aProcesses[i] != 0 )
      {
         // Get a handle to the process.  
         HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
            PROCESS_VM_READ | PROCESS_ALL_ACCESS,
            FALSE, aProcesses[i] );
        
         // Get the process name.  
         if (NULL != hProcess )
         {
            TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
            HMODULE hMod;
            DWORD cbNeeded;
           
            if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
            {
               //This function returns the short name for a module,
               //typically the file name portion of the EXE or DLL
               GetModuleBaseName( hProcess, hMod, szProcessName,
                  sizeof(szProcessName)/sizeof(TCHAR) );
            }
           
            CString str = szProcessName;
           
            //specify the process names
            //or choose whole processes
            if(0 == str.CompareNoCase(_T("devenv.exe")))
            {
               //Removes as many pages as possible from the
               //working set of the specified process.
               if( !EmptyWorkingSet(hProcess) )
               {
                  // AfxMessageBox(_T("Failed") ); //error
               }
            }
         }        
      }
   }    
}

mynote4

 

 

After executing the sample code please look on the Task Manager and check any variation in the memory usage of the given application.

Don’t forget to include Psapi.lib to library modules.





How to list all running process in the system

10 02 2009

description3

 

 

We are familiar with Task Manager, which is showing all the running process in our system. Does anybody know how to list these in our C++ application.

howcanidoit4

 

 

There is a wide area in WIndows APIs for that, known as Process Status Application Programming Interface (PSAPI). This library helps us to get the information about processes and device drivers.

Our particular task could be achieved by using the APIs EnumProcesses, OpenProcess, EnumProcessModules and GetModuleBaseName.

mycodesnippet3

 

 

#include "psapi.h"
void main()
{
   // Get the list of process identifiers.  
   DWORD aProcesses[1024], cbNeeded, cProcesses;
   unsigned int i;
  
   //This returns a list of handles to processes running on the system as an array.
   if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
      return;
  
   // Calculate how many process identifiers were returned.  
   cProcesses = cbNeeded / sizeof(DWORD);
  
   // Display the name and process identifier for each process.  
   for ( i = 0; i < cProcesses; i++ )
      if( aProcesses[i] != 0 )
         DisplayProcessNameAndID( aProcesses[i] );  
}
void DisplayProcessNameAndID( DWORD processID )
{
   TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
  
   // Get a handle to the process.  
   HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
      PROCESS_VM_READ,
      FALSE, processID );
  
   // Get the process name.  
   if (NULL != hProcess )
   {
      HMODULE hMod;
      DWORD cbNeeded;
     
      //Given a handle to a process, this returns all the modules running within the process.
      //The first module is the executable running the process,
      //and subsequent handles describe DLLs loaded into the process.
      if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
      {
         //This function returns the short name for a module,
         //typically the file name portion of the EXE or DLL
         GetModuleBaseName( hProcess, hMod, szProcessName,
            sizeof(szProcessName)/sizeof(TCHAR) );
      }
   }
  
   // Display the process name and identifier.
   CString str;
   str.Format("Text:%s, PID : %u", szProcessName, processID );
   AfxMessageBox(str);
  
   //close the process handle
   CloseHandle( hProcess );
}

Code Snippet Courtesy : MSDN 2008

mynote3

 

 

Please include Psapi.lib to library modules.