Ali Keshavarz's Website
RSS icon Home icon
  • Boolean and BOOL

    Posted on November 5th, 2011 Ali Keshavarz 2 comments

    It took me an hour to fix an Access Violation in one of my source codes tonight. The AV caught my eyes when I thought everything was done correctly, and it is ready for committing changes and hit the bed!

    It was a vague AV, giving me just a few NT OS functions in call stack trace, and stopping on any line containing Result (as the return value for a property getter). I reviewed the code several times, everything seemed OK, and I had only changed tens of lines of code, so I was really confused where the hell this AV came from; I mean, I knew which property getter method was causing the error, and the method was not lengthy or complicated, but I had no clue how come it was causing such an AV >-(

    Eventually while I was reviewing the changed codes again and again, I noticed something; there was a Windows API call which required a PBOOL parameter, and I had called it twice in the function code. One call was written a while ago, and the other was written tonight. The problem was, for the second call I had defined the parameter as Boolean rather than defining it as BOOL (passing its address using @ operator). That’s a shame!

    So here’s a personal note for myself: Do not code at 2 AM, specially when you are kinda sleepy. If you did, make sure your eyes can distinguish between BOOL, and Boolean :)

  • 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.

  • FireMonkey and support for bi-directional text – Part 2

    Posted on September 11th, 2011 Ali Keshavarz 4 comments

    Last month when RAD Studio XE2 was still in Beta phase, I wrote a weblog post about bi-directional (BiDi) text support in KSDev VGScene (ancestor of FireMonkey), questioning FireMonkey support for BiDi text, and why it is important to support it. As I mentioned in a post-script update, Andreano Lanusse commented on my post, and told us BiDi text would not be supported in the initial version of FireMonkey. Now it is been around a couple of weeks that the official RAD Studio XE2 is released, and its trial version is available for download. I decided to test BiDi support in the official FireMonkey using the trial version, and write the second part of my  investigation on BiDi text support in FireMonkey.

    First of all I should mention that I didn’t expect any BiDi support in this version, so my initial intention was to highlight the bugs for BiDi support in FireMonkey and report them to be hopefully fixed in the future releases. The first impression was that FireMonkey is not as bad as VGScene in supporting BiDi text. It was a surprise because I didn’t really expect any BiDi support after reading Andreano’s comment. There is very limited and half baked support for BiDi text in the initial release and it still has a long way to go for fully supporting BiDi text. Here I am going to discuss support for BiDi text in FireMonkey version 1.0, highlighting the bugs which should be fixed regarding this support, and the QC report regarding each bug.

    Before I discuss BiDi support, I should mention that according to Michael Swindell, Senior vice president of marketing and product management at Embarcadero, FireMonkey will be updated frequently. The first update is expected before the end of September, and the other updates will be published more often than quarterly [Source]. That means, we will see several updates for FireMonkey before the new version of RAD Studio is published next year. That is really exciting and makes me very hopeful that all or at least many of the bugs mentioned here might be fixed before the next major release of RAD Studio in 2012. It is important to be hopeful about evolution of a product, and feel that your feedbacks are really considered by its developers. Years ago many of my friends tried to notify Borland about some of the existing bugs in VCL regarding BiDi and right-to-left languages support, but they never got fixed, and many of those guys migrated to .NET, abandoning Delphi. I myself stopped reporting any new bug to QC when Embarcadero deleted my EDN account just because my country in my profile was mentioned as Iran. Now I feel the team listens more to user feedbacks. It doesn’t really matter if I am Iranian or American or whatever when I am reporting a bug or when I am discussing some technical stuff. Anyways, that is another story and I discussed that in an older blog post. Let’s go back to FireMonkey and its support for BiDi text:

    Read the rest of this entry »

  • RAD Studio XE2 is released!

    Posted on September 2nd, 2011 Ali Keshavarz 4 comments

    Eventually RAD Studio XE2 is RTMed and ready for download. There is a good article in the online documentation wiki about what’s new in Delphi and C++ Builder XE2. You can also view the features matrix for different editions here.

    And here  is download link for the 30-days trial version from Embarcadero. Before installing the final version, if you already have XE2 beta installed, make sure you uninstall it completely and remove all of its folders which might remain undeleted; otherwise, you might have to deal with weird installation issues.

    I hope this will be a new start for Delphi :-)

  • FireMonkey and support for bi-directional text

    Posted on August 16th, 2011 Ali Keshavarz 12 comments

    If you are following Delphi news, you probably know that Delphi XE2 is supposed to be released very soon, and it has many exciting features. One of the important new features introduced in Delphi XE2 is its GPU-accelerated, vector-based GUI framework; FireMonkey. I talked about XE2 and FireMonkey in the previous post, but since it is not officially released yet, I am not sure if FireMonkey will have any support for bi-directional (BiDi) text and Right-to-Left languages. A few days ago, I asked Andreano Lanusse in his blog post about this, but I received no reply. Well, I knew FireMonkey is based on existing KSDev VGScene component which was purchased by Embarcadero earlier this year. I had played with VGScene a while ago; however, I couldn’t remember if it had any support for bi-directional text. Anyhow, I decided to install a copy of the last published version of VGScene (4.2) on Delphi XE to see what kind of support for BiDi text languages it provides.

    Before I describe my experience with VGScene, let me explain what supporting bi-directional text really means, why is it important, and what kind of support for it exists in MS Windows…

    Read the rest of this entry »

  • Waiting for Delphi XE2 with excitement

    Posted on August 7th, 2011 Ali Keshavarz 17 comments

    Delphi world is full of good news these days. Delphi XE2 is announced officially a couple of days ago, and it is supposed to bring some lovely features Smile

    It was obvious that XE2 will bring 64-bits support to Delphi, along with supporting Mac OS as a new target platform. Mac OS support was supposed to be added to Delphi XE, but it was postponed to this release. Although 64-bits and Mac OS support are cool, they were both expected. The big news was FireMonkey!

    When Embarcadero announced their plan for adding Mac OS support, the original plan was a continuation of the old CLX framework which was based on Qt. The difference was, the new CLX was supposed to bring just cross-compiling for Mac OS (and later on Linux), not like original CLX which had a Linux-based IDE (Kylix) for compiling the same source code for Linux. However the plan changed, Embarcadero bought KSDev company, creator of VGScene and DXScene components, and hired its programmer. They decided to provide a vector-based, GPU accelerated, cross-platform GUI framework based on the recently bought KSDev products; and that is FireMonkey.

  • Implementing Singleton pattern in Delphi

    Posted on June 4th, 2011 Ali Keshavarz 11 comments

    There are several ways to implement Singleton pattern in Delphi. Most of the publicly available source codes that I saw use old techniques to keep backward compatibility with older versions of Delphi (e.g. Delphi 7). In such techniques usually a global variable or a writable const is used to keep a reference to the singleton instance; because in those old versions of Delphi, there were no way to define a class variable.

    Also many of them try to hide the constructor under protected visibility, assuming it would prohibit users from instantiating the class, and forcing them to use the defined method for accessing the singleton instance. I can’t realize what was the rationale behind hiding the constructor, because all Delphi classes are inherited from TObject, and it already has a default Create constructor defined as public, so a user can always call that Create method to instantiate a new object and getting around your singleton implementation.

    A few months ago when I was looking for a singleton implementation in Delphi which does not use old writable consts or global variables; I found a blog post by Yanniel Alvarez Alfonso, implementing Singleton pattern using class variables, but there was still the problem of hiding constructors, so I wrote a comment there, telling him that hiding the constructor is not a good idea; and instead of that, he should override TObject.NewInstance method. He appreciated the note, and changed his code to reflect that.

    Today I needed to implement Singleton again, but I was too lazy to write it, so I searched for it in my source codes, but didn’t find my own code! I tried my favorite modeling tool; ModelMaker, but their Singleton pattern implementation was the same old-style writable const technique (even in the new ModelMaker 11). Eventually I decided to write it again, and save it as a code template in ModelMaker to use it later when I need it and I am not in the mood of writing it again and again Smile

    Read the rest of this entry »

  • Delphi 64 is coming

    Posted on April 1st, 2011 Ali Keshavarz 1 comment

    Eventually Embarcadero released a sneak preview of Delphi 64-bits compiler. Four days ago, there was an April 1 fake story about this, but now it is all real:

    http://www.embarcadero.com/products/delphi/64-bit

    According to their latest published Delphi roadmap by Embarcadero, they had planned a beta version of the 64bits compiler to be released in the first half of 2011. Currently there is only a sneak preview video available, but you can fill a form to receive more information, or participate in its beta testing program. Seems this time Embarcadero are stick to their published roadmap, unlike what we saw for Delphi XE release.