Ali Keshavarz's Website
RSS icon Home icon
  • Update 1 for RAD Studio XE2 is available

    Posted on September 28th, 2011 Ali Keshavarz 1 comment

    Today,  just less than a month of releasing the original XE2 product, Embarcadero published the first update of a series of frequent updates for RAD Studio XE2 (including Delphi XE2 and C++ Builder XE2). Update 1 contains 120 bug fixes in FireMonkey, VCL Styles, compiler, and IDE. This update is mandatory for installing the future updates. Here is the official announcement: Update 1 for Delphi XE2, C++Builder XE2 and RAD Studio XE2 . Make sure to read Release Notes for Update 1 before installing it, because Update 1 requires a full uninstall of original XE2 installation and is not a simple upgrade. You can also see a list of all bug fixes in this update here: Delphi XE2 and C++Builder XE2 Update 1 Bug Fix List .

    As I checked the bugs list, at least one bug regarding BiDi support (QC # 97965) is fixed in VCL Styles. No other BiDi related bugs are fixed in this update. Embarcadero staff mentioned in Embarcadero newsgroups that the company is planning to release monthly updates for XE2 product, so we should wait to see if any of the BiDi related bugs will be fixed in the coming updates or not.

  • Enumerating Windows services

    Posted on September 13th, 2011 Ali Keshavarz 3 comments

    A while ago, I had to enumerate all Windows services for a project. I was browsing the source code today, and thought maybe it would be a good idea to talk about this matter in a blog post, and publish a sample source code to achieve this.

    The easiest way to enumerate Windows services is using WMI. Of course using WMI requires some basic knowledge of MS COM technology which is tricky, but thanks to the great tool. Delphi WMI Class Generator by Rodrigo Ruz, you can have a ready Delphi class for obtaining information about installed services on a Windows machine without even writing a single line of code. All you have to know is that WMI class for Windows services is Win32_Service under root\CIMV2 namespace. Delphi WMI Class Generator will generate a unit named uWin32_Service.pas which contains a class named TWin32_Service. This class returns info about a single service for you. If you need to enumerate all installed services, you need to write a couple of codes to call its GetCollectionCount to retrieve number of available services, and SetCollectionIndex to set current item index for the class. Here is a sample code to retrieve names of all installed services on the local machine using TWin32_Service class:

    var
      i: Integer;
    begin
      with TWin32_Service.Create do
      try
        for i := 0 to GetCollectionCount - 1 do
        begin
          Writeln(DisplayName);
          SetCollectionIndex(i);
        end;
      finally
        Free;
      end;
    end;
    

    However, WMI is slow, and it relays on WMI service. If you need a faster way to enumerate Windows services, or if for whatever reason WMI service is disabled or not working on your target machine; then you have to relay on Windows API. Using Windows API for this purpose is not as easy as using the automatically generated TWin32_Service class. I am not aware of a single Windows API function that can enumerate Windows services for you and give you all the detailed information which WMI returns for each service. You have to call roughly a dozen of API functions to enumerate Windows services and retrieve some nice information for each service.

    First you need to open a handle to service manager on the target machine by calling OpenSCManager function, then you should call EnumServicesStatusEx function to enumerate available services, and retrieve their names and status. If you just need service names and status, you are done, but if you need to retrieve description and configuration for each service too, then you should open a handle for each service using OpenService function by giving it service name which you obtain through EnumServicesStatusEx call , and then call QueryServiceConfig and QueryServiceConfig2 functions to get the required configurations. At the end you should make sure you free all the allocated buffers, and close all handles. Delphi installation does not provide header translation for some of these API functions (particularly EnumServicesStatusEx and QueryServiceConfig2), but they are translated and available for Delphi by well-known Project JEDI, and their JEDI Windows API Headers which contain the majority of Windows headers. If you are a Delphi developer and have not heard about them yet (That would be weird!!), you can download the package from this link.

    Anyways, in the source code below, I wrapped all of those API calls into a class named TAkServices. This class holds an internal list containing information for each Windows service installed on a given machine. I tested it on both Delphi 2010 (Win32) and Delphi XE2 (Win64):

    Read the rest of this entry »

  • ProcessInfo 1.5 is released with Win64 support

    Posted on September 12th, 2011 Ali Keshavarz 5 comments

    ProcessInfo 1.5 is released. The changes in this release are:

    • Added support for Win64 and Delphi XE2.
    • Added TProcessItem.CloseProcess method to close a process normally (not forcefully).
    • Added TProcessItem.CurrentProcess property. This property always refers to the current process in the list of running processes.
    • Added PrivateWorkingSetSize property to TProcessItem.MemoryInfo.
    • Added global  ProcessInformation function. This function returns a global instance of TProcessInfo. The instance will be freed when the application is terminating.
    • Fixed a few minor bugs.

    To download ProcessInfo 1.5, please go to ProcessInfo page.

  • Different function parameter modifiers in Delphi

    Posted on August 2nd, 2010 Ali Keshavarz 6 comments

    Recently a friend asked me about constant parameters in Delphi functions, that brought the idea of writing a new post about passing parameters to functions/procedures in Delphi.

    Parameter modifiers

    When you call a function or procedure (I am going to refer to both of them as Functions here), you have to somehow pass the required input or output parameters to it. Parameters are passed to functions either by value, or by reference.

    Passing by value means, compiler makes a new copy of the original data, and sends it to the function. This way, the function has its own copy of data, and changing value of the parameter inside the function does not affect the original data. This is the default mode when you pass a parameter to a function in Delphi: Read the rest of this entry »

  • ProcessInfo 1.3 is released

    Posted on July 26th, 2010 Ali Keshavarz 2 comments

    Hi,

    ProcessInfo 1.3 is released. The changes in this release are:

    • CPU usage is added to TProcessItem.
    • Is64Bit is added to TProcessItem.
    • IsAccessible is added to TProcessItem.
    • Setting thread priority is added to TThreadItem.
    • Setting process base priority class is added to TProcessItem.

    To download ProcessInfo 1.3, please go to ProcessInfo page.

  • How to use TProcessInfo #2

    Posted on July 26th, 2010 Ali Keshavarz No comments

    I had a few e-mails and comments about using TProcessInfo, so I decided to answer those questions in new blog posts.

    Q1: How can we find a module which is running a specific thread, giving a thread ID:

    A: First of all, Threads are not assigned to modules, they are assigned to processes. You can find a process which is running the thread, by searching in the list of threads owned by each running process. Here is a sample code:

    function FindProcessByThreadID(ID: Cardinal): TProcessItem;
    var
      ProcessInfo : TProcessInfo;
      AProcess : TProcessItem;
    begin
      Result := nil;
      ProcessInfo := TProcessInfo.Create(nil);
      try
        for AProcess in ProcessInfo.RunningProcesses do
        begin
          if Assigned(AProcess.Threads.FindByID(ID)) then
          begin
            Result := AProcess;
            Exit;
          end;
        end;
      finally
        ProcessInfo.Free;
      end;
    end;
    

    FindProcessByThreadID iterates over all running processes, and checks which of them has a thread with the given ID. If a process owns a thread with that ID, it will return that process as a TProcessItem object.

    Once you have the process object, you can get more information about the process or the specific thread, or you can kill the process, or suspend the thread. For example:

    var
      Process : TProcessItem;
    begin
      Process := FindProcessByThreadID(StrToInt(3444));
      if Assigned(Process) then
        ShowMessage(Process.FullPath);
    end;
    

    The above code looks for a thread with its ID = 3444, and if it is found, it provides full path of the EXE file for that process.

    Q2: How can we get memory consumption for a given process?

    A: You can read memory info for a given process using TProcessItem.MemoryInfo. It provides different info about the memory the process is consuming. Here is an example for reading memory info of Google Chrome process while it is running :

    var
      ProcessInfo : TProcessInfo;
      AProcess : TProcessItem;
    begin
      ProcessInfo := TProcessInfo.Create(nil);
      try
        AProcess := ProcessInfo.RunningProcesses.FindByName('Chrome.exe');
        if Assigned(AProcess) then
          ShowMessage(Format('WorkingSet Size = %d, Peak WorkingSet Size = %d',
                             [AProcess.MemoryInfo.WorkingSetSize,
                              AProcess.MemoryInfo.PeakWorkingSetSize]));
      finally
        ProcessInfo.Free;
      end;
    end;
    

    First we search for Chrome.exe process among running processes. Once the process is found, we read its current working set size, and pick working set size through MemoryInfo property, and show them in a message box. The values returned are in Bytes.

    Q3: How can we get process base priority class or change it?

    A: You can access base priority class for a given process or modify it (added in version 1.3), using TProcessItem.

    PriorityClassBase. Be careful, adjusting a base priority class of a given process inappropriately might make other processes unresponsive! Here is a example code:

    var
      ProcessInfo : TProcessInfo;
      AProcess : TProcessItem;
    begin
      ProcessInfo := TProcessInfo.Create(nil);
      try
        AProcess := ProcessInfo.RunningProcesses.FindByName('firefox.exe');
        if Assigned(AProcess) then
          AProcess.PriorityClassBase := BELOW_NORMAL_PRIORITY_CLASS;
      finally
        ProcessInfo.Free;
      end;
    end;
    

    You can also change each priority of  each thread within a process by setting BasePriority property of that TThreadItem instace.

    Q4: How can I check if a process is 64-bit?

    A: You can check this by reading TProcessItem.Is64Bits property.

    Q5: How can I get CPU usage for a given process?

    A: You can get CPU usage by reading TProcessItem.CPUUsage property.

    I hope these few answers and sample source codes can help you use Process Info. For other examples of using Process Info, please refer to this post: How to use TProcessInfo

    If there is any question regarding using Process Info, or anything about Delphi that you think worth being mentioned in Tips & Tricks section of this website, please contact me, and let me know about it.

    Have Fun!

  • ProcessInfo 1.2 is released

    Posted on January 13th, 2010 Ali Keshavarz 1 comment

    Hi,

    ProcessInfo 1.2 is released. The changes in this release are:

    • SuspendThread, ResumeThread, TerminateThread methods are added to TThreadItem class. Now you can pause/resume/terminate any running thread in a given process.
    • TProcessInfo.Active and TAppInfo.Active are published properties, and can be set in design mode.
    • TProcessInfo.RunningProcesses and TAppInfo.RunningApplications automatically populate the corresponding list if UpdateList method is not called yet. This means even if you don’t activate any of these two components, or call their UpdateList method, accessing RunningProcesses or RunningApplications does not cause Access Violation.

    To download ProcessInfo 1.2, please go to ProcessInfo page.

    Regards.

  • Component Toolbar slows down RAD Studio 2010

    Posted on January 2nd, 2010 Ali Keshavarz 4 comments

    In RAD Studio 2010 there is a new IDE feature called Component Toolbar. This feature provides a component palette similar to the old Delphi component palette.

    Component Toolbar
    Component Toolbar in RAD Studio 2010

    This toolbar provides a nice search box which is able to filter components in the palette based on the search phrase. This toolbar is disabled by default, and is shown when Classic Layout is selected. Here you can see a snapshot of Classic Layout in RAD Studio 2010:

    Classic Layout in Delphi 2010

    A few weeks ago while I was working with Delphi 2010, I noticed a delay when switching from code view to form designer. The delay was there not only in the complex forms, but also in simple empty forms! It was not a big delay (about 1 second), but since I had to switch between code and form designer view many times, it was really annoying, so I decided to investigate it and find out what is causing this delay.

    Read the rest of this entry »

  • ProcessInfo 1.1 is released

    Posted on October 22nd, 2009 Ali Keshavarz 1 comment

    Hi,

    I released a new version of ProcessInfo. In this release I added these features:

    • Enumerators are added for Windows, Threads, Modules, and Processes; Now you can use for-in statements in D2007 and above for iterating on running processes list, or modules\threads\windows of a given process.
    • TProcessItem.UserName is added; This property returns domain name\user name which is running the process.
    • TProocessInfo.AdjustDebugPrivilage is added; This method is called automatically.
      TThreadItem.ToString & TProcessItem.ToString are added; TThreadItem.ToString returns ThreadID. TProcess.ToString returns process EXE name.
    • Now supports Delphi 7,2007,2009, 2010; Some conditional compiler directives are added so that it can be used in D7, 2007, 2009, and 2010. I tested it in D7, 2009, and 2010. It should work in D2007 too.

    To download ProcessInfo 1.1, please go to ProcessInfo page.

    Regards.

  • How to use TProcessInfo

    Posted on September 11th, 2009 Ali Keshavarz 5 comments

    The other day I published Process Info component pack which contains TProcessInfo and TAppInfo. A sample task manager was also published as demo.

    I thought maybe it is a good idea to talk about these components and some of their usages by providing some sample source codes. So I will start with TProcessInfo.

    Read the rest of this entry »