VisualCpp.org Collection of Visual C++ Stuffs

26Jul/07Off

Getting Handle, GUI Object and Memory usage count of Process?

 Sometime back I am load testing the application and I have to note down the memory usage, handle count and GUI Object count of the process at the regular interval. Which itself is very cumbersome task. Continue seeing the taskmgr.exe for this thing and then note it down it on plain paper or excel.

 So I think of writing a small application will automate my task! I had searched MSDN, Many programming forums to find this stuff. Here is small code which retrieves the above from specified process. Please read my this tip to retrieve ProcessID of particular process. Here I am retrieving the Process handle using OpenProcess API, then Using GetProcessHandleCount, GetGuiResource and GetProcessMemoryInfo.

HANDLE hProcess =
  OpenProcess(PROCESS_QUERY_INFORMATION| PROCESS_VM_READ,FALSE,lppe.th32ProcessID);

unsigned long lThreadCount=0,lGdiCount=0,lHandleCount =0 ;

GetProcessHandleCount(hProcess,&lHandleCount);
lGdiCount = GetGuiResources(hProcess,GR_GDIOBJECTS);

PROCESS_MEMORY_COUNTERS pmcProcMemCounter = { 0 };

GetProcessMemoryInfo( hProcess,
  &pmcProcMemCounter,
  sizeof( pmcProcMemCounter ));

Filed under: General Comments Off
26Jul/07Off

Reteriving Process information By Executable Name?

Many times programmer needed the Process Information like Process Handle, ID, Executable Path for running process, I had written small code which will give you all this information mentioned above. here i used ToolHelp api to retrieve all running process and compared each executable name with required Executable

  CString csName,strprocessName; //Here strprocessName contain name of process to
  // be searched
 // Get Global Snapshot of Process
  HANDLE hGblHandle=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
  PROCESSENTRY32  lppe;

bool bFound = false;
  if(hGblHandle)
 {
     lppe.dwSize = sizeof(PROCESSENTRY32);
     if(Process32First(hGblHandle,&lppe))
     {
           csName = lppe.szExeFile;
           csName.MakeLower();
          if(csName.Find(strprocessName) == -1)
         {
               while(Process32Next(hGblHandle,&lppe))
               {
                   csName = lppe.szExeFile;
                   csName.MakeLower();
                   if(csName.Find(strprocessName) != -1)
                  {
                     bFound = true;
                     break;
                  }
         }
    }
  else
        bFound = true;
   }

}

if bFound comes to be true, you got lppe variable filled with required information.

Filed under: General, Win32 Comments Off
23Jul/07Off

How to see all Function(s) exported by DLL?

MS provides you a Classic tool namely Dependency Walker (Depends.exe) which show you all the function exported by the particular DLL. Now what if your DLL shows only four functions like DllCanUnloadNow, DllGetClassObject, DllRegisterServer and DllUnregisterServer which signifies that it is COM DLL. You can use another tool provided by MS namely OleView which will show you the all interface associated with this COM DLL.

On other note, using Dependency Walker gives you Function Arguments in mangled form if exported by using _declspec, you can see original parameter list using UndName.exe

Filed under: DLL, General, Win32 Comments Off