Marco Cantu

Syndicate content
Techie Italian Blogging on Delphi and More
Updated: 3 min 23 sec ago

Faster Compilation for Delphi Win32 Generics in 10.1 Berlin

Wed, 05/25/2016 - 02:57

While the Delphi compiler is recognized as an extremely fast compiler, when it comes to generics it can significantly slow down, along with the linker. The latest version of Delphi offers a significant improvement.

During the development of 10.1 Berlin Delphi’s R&D team, and in particular the compiler team, looked into areas in which we could improve the compiler quality, both in terms of code generation and in terms of compiler speed. Out of many improvements, one worth calling out it the significantly faster compilation for code involving a large number of generic data types.

This wasn’t actually a single fix, but a collection of 6 different compiler optimizations, each of which contributes to the overall improvement. Ranging from small to very large projects, we have internal test cases that show different gains for each of these individual optimizations – well, in general, as there are individual optimizations that make compilation slower in given scenarios.

As an example, one of our customers (thanks, Guenther) had submitted a demo (and actually even a demo generator) as part of That demo code on Win32 in 10 Seattle takes on average 25 seconds for a full build. The same exact code compiles in 10.1 Berlin in less than 10 seconds. That is, it is taking only 40% of the original time – a 150% improvement.

Other code scenarios, specifically those not so extreme, will see a more moderate advantage, but the changes are applicable to almost all code and both the compiler and the linker. As a more real-world scenario, I downloaded the latest Spring4D library, and compiled the Win32 version of the Spring.Tests projects of 10 Seattle and 10.1 Berlin, and got the following numbers:

  • 10 Seattle: 6.01 seconds
  • 10.1 Berlin: 4.50 seconds

As a further test (although less significant because it is very fast) I also compiled the Demo.Spring.Collections.Stack demo and saw the following timing:

  • 10 Seattle: 1.02 seconds
  • 10.1 Berlin: 0.70 seconds

The summary is there is a consistent improvement in compiling code with generics, in this real-world cases the compilation is about 30% faster. I had a chat with Stefan Glienke, and he confirmed seeing similar numbers. 

There is a caveat, though. The original request was to speed up the Win64 compiler, and that hasn’t actually seen a significant gain in this cycle, I only see a 10% improvement in that case. That clearly means we still have a lot of room for improvement, and need to keep focusing on the compiler performance (but without forgetting code generation quality and performance of the compiled code, of course).

In any case, 10.1 Berlin represents a good step in the right direction in terms of compilation of Delphi code with generics and I hope this is appreciated.

Categories: News, Blogs, and Tips

Delphi Blogs of the Week/Month #42

Mon, 05/23/2016 - 03:29

This is a new installment of my occasional (mostly monthly) collection of relevant Delphi links.

Embarcadero News

Company focus is summarized in this video, "Inspire. Develop. Excel.":

As you have seen also in this blog, we have officially lunched a new product, RAD Server, a new offer of existing enterprise level technologies (EMS, BeadonFencing, IotT remoting, etc). Read more at Notice that the complete evaluation software is already included in RAD Studio 10.1 Berlin at the Enterprise level, along with the all of the specific development tools. You can also read

If you missed RAD Studio 10.1 Berlin launch webinar, the replay in on YouTube at

​Technical Blog Posts

A summary of RAD Studio 10.1 Berlin FireUI Live Preview articles, videos, apps and docs by David I at

InterBase Array Fields and FireDAC, by Stephen at

Comparison of Database access frameworks in Delphi (in which FireDAC has a big role) at

A Delphi Guy Walks Into Google, by Allen Bauer at

A Delphi mobile app used to track Bolzano city elections, at

The new TBufferedFileStream class:

Third-Party Related News

Using Advantage Database Server with 10.1 Berlin by Joachim at

Interesting project by Daniele to support Google's FireBase in Delphi at 

And Finally...

Verity Stob has a new column about the Sons of Kahn, covering last 6 months of news around RAD Studio. And punctuation. And misspelled Wookeys. See

And, finally, don't miss this months offers: Upgrade from very old versions of RAD Studio and free mobile pack if you buy Delphi or C++Builder Professional! See

Categories: News, Blogs, and Tips

RAD Server Launched, Webinar on Thursday

Wed, 05/18/2016 - 00:52

Last week my team at Embarcadero has launched a new product, RAD Server, which combines several Enterprise-grade technologies in a single, much cheaper offering. RAD Server, in fact, combines the REST API hosting offered by EMS, with IoT integration (ThingPoint) and the proximity awareness offered by BeaconFence.

The idea behind RAD Server is to make the development of REST APIs and other related services extremely simple, much like Delphi and RAD Studio do for the client side. Having a ready-to-use, turn-key solution which can scale at a very low price, can be a nice and effective way for developers to implement a modern, REST-based, multi-tier solution for both mobile and desktop applications.

Here are some relevant links about the launch and some further reading about the technologies included in RAD Server:

- The official press release can be found on the community site:

- The new product page is at

- The launch webinar, next Thursday (May 19th), "Introducing RAD Server - Walk Through an example Real World Scenario in Creating a Modern Retail Customer Experience Solution" (where I'll have a small part) -- register at 

The technology behind RAD Server is very powerful, and we'll have more information about it soon, in terms of blog posts and webinars, but you can already read the core features of the REST API hosting solution in this EMS white paper by Cary Jensen: While the current version has several extensions, the core concepts apply.




Categories: News, Blogs, and Tips

Ribbon Controls in RAD Studio 10.1 Berlin

Mon, 05/09/2016 - 04:28

A few Delphi and C++Builder developers who moved to 10.1 Berlin are wondering what happened to the VCL Ribbon controls and how to get them. Here is the full story.

A Tale of the Fluent UI (or Office UI)

Let me start from the beginning, while trying to keep the story short. The Ribbon VCL controls offer an implementation, natively done in the VCL with no external dependencies, of Microsoft Fluent UI (also know as Office UI). Given Microsoft has a patent on this UI (debatable, I know) using the controls required to accept a license from Microsoft, originally available at In the past RAD Studio installation alerted that accepting that license was required for legally using the controls.

Later on, Microsoft built an actual SDK (in Windows 7 and above, also back ported to Vista) to support building applications with the Fluent UI. So far so good. Afterwards, they retired the original license. Any developer who had already accepted Microsoft Fluent UI license can still build a clone UI, but developers cannot obtain that license any more. You can see this reported, among others, in Wikipedia at

The Microsoft Ribbon Licensing Page has been retired, therefore it is no longer possible to license the ribbon control from Microsoft.

This is why we had to pull the Ribbon VCL controls from the core RAD Studio product, as we have no way to grant all customers a license or indicate how to obtain one. This doesn't mean we are removing the controls completely. While we don't expect to have the Ribbon VCL controls as part of the product and covered by support, we are in the process of making the Ribbon controls available in the GetIt Package Manager, for any developer who has a valid license with Microsoft (something you have to determine yourself, of course). This was supposed soon after the 10.1 Berlin release, should be coming shortly.

Ribbon Future Options for RAD Studio Developers

Overall, though, this change of direction from Microsoft (which actually happens a few years back), requires rethinking the use of these controls. I'd offer 3 alternatives:

  • If you have a license and want maximum compatibility with existing code, you can keep using the existing Ribbon controls. Shortly we'll make the official package available on GetIt. We are also assessing how to keep fixing bugs and updating this library over time. This might involve the community.
  • You can switch to using an implementation of the Ribbon SDK from Microsoft. One set of VCL controls wrapping that implementation and called Ribbon Framework is available in the library and it is already hosted in GetIt (and was hosted also for 10 Seattle). This has no legal limitations, and the team might also need help to keep this project moving. And thanks to Joachim Marder and the team behind these controls
  • You can consider moving away from the Ribbon UI. But this is more of a UI discussion I don't really want to focus on in this blog post.

As it should be clear from reading this blog post, it was never our intention to stop supporting the VCL Ribbon controls, but we don't have a way to do this legally for all of our customers. I know other component vendors have different interpretations of the legal status, but we did check with several sources (including directly with Microsoft) and we are convinced that there isn't another option. Once the VCL Ribbon controls will be made available in GetIt, consult with your legal counsel about using them.

Categories: News, Blogs, and Tips

RAD Studio Quality Update After 10.1 Berlin

Mon, 05/02/2016 - 06:38

The release of RAD Studio 10.1 Berlin marks another step towards improving the overall product quality and the company process of managing customer reported issues.

Issues Fixed in Berlin

Let me focus on 10.1 Berlin first. As we stated publicly (see slide below), in this release the team fixed 744 customer reported issues. The total number of internal issues fixed is more than 2,500 (which includes those 744), as most issues are reported internally.

This is 30% to 50% higher than recent releases, as Malcolm pointed out in his blog. Notice that these 744 are either bugs fixed or that don't reproduce, likely due to some other fix. The count doesn't include duplicates (issues reported by more than one person) or reports we decided not to fix because they are as expected, considered documentation issues, or other cases. With those, the overall number would be over a thousand.

April Hot-fixes to the Rescue

Despite the effort for fixing bugs, some new issues slipped in the release. In particular there were a couple of rather significant regressions for which we have already released two hot fixes:

Bugs Status and Quality Portal

Having said this, it is worth trying to make some assessment about the status of bugs, and particularly publicly reported bugs and feature requests. While not everything has been synchronized between the internal and the public system, most of the information should be updated now. Looking to Quality Portal issues reported as bugs, there are 4,595 entries of which 67% are either closed or resolved. This is good progress. Feature requests are mostly open, as they started coming in a few months back, when plans for 10.1 Berlin had already been done.

Given the project page for RAD Studio on the Quality Portal hasn't got a real picture of the quality status, I've been creating an alternative page, currently at (viewing the page requires you to login to the system). A snapshot of the current status is below. The plan is to add similar widgets to the main project page.

Categories: News, Blogs, and Tips

Weak and Unsafe Interface References in Delphi 10.1 Berlin

Thu, 04/28/2016 - 07:45

Delphi 10.1 Berlin adds a couple of features to the Delphi language, bridging gaps between platforms and extending the Win32/Win64 interface reference model. This is not the only change to the language, but clearly the most relevant one. Another, focused on mobile, is the introduction of the native UTF8String type on all platforms, mobile compilers included.

But let me get back to the way references to interfaces work on the desktop compilers. By default, all references to interfaces use reference counting. As you assign a variable to the interface the reference count is increased, as the variable is set to nil or goes out of scope, the reference count is decreased. When the reference count gets to zero the object is deleted from memory. That is not actually always true, as the actual behavior is implemented by each class, so you can write a class that implements an interface and ignores the reference counting mechanism.

Anyway, getting back to the common scenarios in which reference count is active, you can have code like the following, which relies on reference counting to dispose the temporary object:

procedure TForm3.Button2Click(Sender: TObject); var one: ISimpleInterface; begin one := TObjectOne.Create; one.DoSomething; end;

What if the object has a standard reference count implementation and you want to create an interface reference that is kept out of the total count of references? You can now achieve this by adding the [unsafe] attribute to the interface variable declaration, changing the code above to:

procedure TForm3.Button2Click(Sender: TObject); var [unsafe] one: ISimpleInterface; begin one := TObjectOne.Create; one.DoSomething; end;

Not that this is a good idea, as the code above would cause a memory leak. By disabling the reference counting, when the variable goes out of scope nothing happens. There are some scenarios in which this is beneficial, as you can still use interfaces and not trigger the extra reference. In other words, an unsafe reference is treated just like... a pointer, with no extra compiler support.

Now before you consider using the unsafe attribute for having a reference without increasing the count, consider that in most cases there is another better option, that is the use of weak references. Weak references also avoid increasing the reference count, but they are managed. This means that the system keeps track of weak references, and in case the actual object gets deleted, it will set the weak reference to nil. With an unsafe reference, instead, you have no way to know the status of the target object (a scenario called dangling reference).

In which scenarios are weak reference useful? A classic case is that of two object with cross-references. In such a case, in fact, the object would artificially inflate the reference count of the other objects, and they'll basically remain in memory forever (with reference count set to 1), even when they become unreachable.

As an example consider the following interface, accepting a reference to another interface of the same time, and a class implementing it with an internal reference:

type ISimpleInterface = interface procedure DoSomething; procedure AddObjectRef (simple: ISimpleInterface); end; TObjectOne = class (TInterfacedObject, ISimpleInterface) private anotherObj: ISimpleInterface; public procedure DoSomething; procedure AddObjectRef (simple: ISimpleInterface); end;

If you create two objects and cross-reference them, you end up with a memory leak:

var one, two: ISimpleInterface; begin one := TObjectOne.Create; two := TObjectOne.Create; one.AddObjectRef (two); two.AddObjectRef (one);

Now the solution available in Delphi 10.1 Berlin is to mark the private field anotherObj as weak:

private [weak] anotherObj: ISimpleInterface;

Now the reference count is not modified when you pass the object as parameter to the AddObjectRef call, it stays at 1, and it goes back to zero when the variables go out of scope, freeing the objects from memory.

Now there are many other cases in which this feature becomes handy, and there is some real complexity in the underlying implementation. It is great feature, but one that takes some effort to fully master. Also, it does have some runtime cost, as weak references are managed (while unsafe ones are not).

Categories: News, Blogs, and Tips

RAD Studio 10.1 Berlin Launch Webinar Replay

Wed, 04/27/2016 - 09:45

The replay of last week launch webinar of RAD Studio, Delphi and C++Builder 10.1 Berlin by RAD Product Managers (myself included) is available on YouTube.

Some information about the webinar replay is also at

The YouTube video is available at 

Categories: News, Blogs, and Tips

Some RAD Studio 10.1 Berlin Blogs and Videos

Wed, 04/27/2016 - 08:08

Here is a first batch of blogs posts and videos about RAD Studio 10.1 Berlin. More are coming, including an in-depth webinar next week.

10.1 Berlin New Installer and IDE

Great Installation Experience with new RAD Studio 10.1 Berlin at and follow the video at

Where is the ActiveX Project Type for Delphi 10.1 Berlin? at

Using the Floating Form Designer in Delphi at

New in FireMonkey for 10.1 Berlin

Customizing the style for a user interface control in RAD Studio 10.1 Berlin at

Introducing FireUI App Preview in RAD Studio 10.1 Berlin at Also how to add components to the preview is explained at And a video on FireUI Mobile Previews offering a live preview at a glance at and also how to configure at

Accessing the Address Book on iOS and Android at and also at and in a video at

What’s New Roundup For FireMonkey In #Delphi 10.1 Berlin On Android, Windows, OSX, And IOS at

TListView design mode covered in a video at

IoT and the coming RAD Server...

New IoT components videos at and And the blog post at

Swagger documentation for REST APIs at

10.1 In-Depth Webinar Coming

Tune if for next week webinar: Fire up your business and users with RAD Studio 10.1 Berlin's “Enterprise Strong” Desktop, Mobile, Database, Server, Cloud and IoT Solutions. See

And stay tuned for more information about the 10.1 Berlin version Delphi, C++Builder and RAD Studio.

Categories: News, Blogs, and Tips

RAD Studio 10.1 Berlin Released Tuesday

Thu, 04/21/2016 - 02:41

As you probably know by now, Embarcadero has released Delphi, C++Builder and RAD Studio 10.1 Berlin on Tuesday, at a live event in the German capital city.

The Event in Berlin

This is a picture I took at the very start of the public event.

And this is a picture Matthias took of myself while presenting:

There was also a partners event, in which I gave more of a sales oriented overview. Some partners are always amused about how much I gesticulate, but I'm Italian, what can I do about it (Thanks, Richard)

And in the evening we did a joint launch session, in which we showed a comparison of the old and new installer, IoT components installation, FireUI App Preview on device, Beacon Fencing in action, and a Bluetooth controlled model car.

Remarkably, I was able to do all of these sessions even after my Mac decided to stop booting (SDD at fault). Thanks again to Matthias and Daniel for borrowing me their Macs -- and good I had all of my VMs in an external drive with me. 

The 10.1 Berlin Product Release

Here is some initial information and a number of relevant links, more will follow in the coming days from the entire team at Embarcadero. I'll probably blog about language features and other technical elements of the release.

The Embarcadero web site has been updated with information about the release, and you can find the more technical details in the feature matrix, or in the What's New page of the docwiki. There is also a press release on BusinessWire. There is also a detailed announcement by David I in the Embarcadero community site.

The Launch Webinar, Today

There has been a flurry of blogs and videos about the new features of the product, I'll offer a overview in a future blog post. For now, make sure you attend one of the sessions of todays's webinar (3 PM Central Europe, 11 AM Pacific, tomorrow morning in Asia) where I'll be online with the other PM answering your questions after the session.

PS: Did I mention there is a 10% introductory discount over the next 30 days? For both new licenses and updates

Categories: News, Blogs, and Tips

Delphi Blogs of the Week/Month #41

Mon, 04/18/2016 - 04:50

Another couple of weeks since my last posts collection, here is some references to interesting links -- waiting for tomorrow event in Berlin

The unexpected blog post of the month goes to The Delphi Open Tools API Book at (very interesting, although I had very ltitle time to look into the actual content).

Technical Blog Posts

The hirearchical tree (by we should probably say graph) of all FireMonkey objects, at from one of the developers

Database Application Tools for FireDAC that ship with our products by David I at

Using C++11 Lambda functions with the C++Builder Parallel Programming Library also by David I at

Interfacing A IoT Water Leak Sensor Via Bluetooth With Firemonkey In Delphi 10 Seattle On Android (article by FMX Express) at

Third Party Tools

Visual Arduino programming for kids and professionals using Visuino by Mitov Software - powered by Embarcadero Delphi (and summarized by David I ) at

Upgraded SVG Component Library For Firemonkey In Delphi 10 Seattle On Android And IOS (article by FMX Express) at

A tutorial "On Data Files and Configuration in Delphi Programs" (by Warren Postma) at

Other Blogs Posts

Getting ready for Linux, just in case (by Warren Postma)...

Google, Chrome and Delphi by Allen Bauer at


Again, stay tuned to tomorrow event (on social media like Twitter if you cannot be there) and the webinar on Thursday.

Categories: News, Blogs, and Tips

New highlights of RAD Studio webinar and the Berlin event

Fri, 04/08/2016 - 03:23

On April 21st, Embarcadero is running a webinar offering an overview of the latest release of the development tools products and their directions. I'll be hosting it with Sarina DuPont: "RAD Studio Product Managers Marco Cantu' and Sarina Dupont will cover the overall product directions and provide details about today's product and its future".

The webinar will be repeated 3 times during the day, for different time zones, covering Delphi, C++Builder, and RAD Studio. You can sign up at

Two days before, on April 19th, there will be a public event with the same focus in Berlin, Germany. I'll be in Berlin to give a keynote session at the event, followed by in-depth technical sessions in German language. The program is available at If you live in the area, you're more than welcome, and I'll be available for the entire day to discuss anything Delphi or RAD Studio related (and don't miss the evening part of the event).

There are great new features coming to the product, so don't miss these opportunities to learn about them. Of course, you can buy RAD Studio or Delphi or C++Builder today (check out our great offers) with update subscription, to get your hands on the product today (if you are not already on 10 Seattle) and receive new releases in the future.

Categories: News, Blogs, and Tips

Embarcadero Cool App Contest

Tue, 04/05/2016 - 01:19

Embarcadero has started a contest for "cool apps" with monthly winners. you can read all of the information at:

There are submission rules, you basically have to submit a short video of the app, and you can use any technology (mobile, desktop, multitier, anything).  Submit your cool apps to share the fame as an elite Embarcadero developer.

Categories: News, Blogs, and Tips

Delphi Blogs of the Week/Month #40

Wed, 03/23/2016 - 07:16

Another collection of links to blogs posts, news, and other tidbits of interest to Delphi and RAD Studio developers.

Official Embarcadero Updates

The company has a new web site at exclusively focused on development tools. Database management tools have been moved to the parent company web site, This is part of Embarcadero new Dedicated Developer Strategy.

New Webinar on Updating from Delphi 2007 to Delphi 10 Seattle, tomorrow, by Jim McKeeth at

​Top 10 Ways to Monetize on Windows 10 at

If you live in Germany, you might be interested in this special event April 19th in Berlin, 

Technical Blog Posts

Meet the new Embarcadero MVPs by Jim at

White paper on new JSON features in 10 Seattle, by Pawel at

​Nick got back to periodic summaries at

What's your favorite Delphi Demo by Jim at

Expand your toolkit with GetIt at

Windows 10 Components in 10 Seattle by Jordi at

Not strictly Delphi, but very interesting post, "The Definitive Guide on Win32 to NT Path Conversion", by Google at

More Tidbits and Third Parties

A new Delphi REST library at

DataSnap filters compendium by Daniele at

Pascal Analyzer,

That's all for this week.

Categories: News, Blogs, and Tips

Considerations on StackOverflow 2016 Developer Survey

Mon, 03/21/2016 - 03:46

The popular StackOverflow site has released the results of their developers survey. which includes some interesting information -even if it skewed towards web developers. You can read the complete results at

In this blog post I want to share some considerations about the data, well-aware that the self-selected audience is very much geared towards web development or (to be more precise) full-stack developers (30%) while only 7% claim to be "desktop developers" -- which is RAD Studio core audience.

The most popular language is JavaScript, which is not surprising given the focus, although it is also the most popular back-end development technology, way ahead of Java and PHP. The developers age and their experience is also a bit different than those in our surveys, and varies quite a bit by country. Roughly 50% of the developers who answered (or, maybe, who had time to answer) have less than 5 years experience as developers and in their 20ies; also a large percentage (45% has a site reputation below 100). This is more of a reflection of the StackOverflow users than of the developers at large. My impression is older developers favor different online communities, if any, but this is really a side issue.

Looking into the actual data, with JavaScript leading and JavaScript libraries on client and server side (Angular.js, Node.js) on the rise, it was nice to see that while Delphi was not listed as an option, it was the most typed entry in the "others" category, with over 650 respondents typing the tool name: 

A trend I've seen discussed online is the fact that developers using OS X are growing over those using Linux, a result probably influenced by the fact taht a Mac is needed for iOS development. Windows remains by far the OS most used by developers (around 50%). A fun fact is that VisualBasic also wins a catecory, that for the most dreaded technology. One think I wasn't too happy to read is that the most popular development environment is... Notepad++, but clearly given the web developer audience editors have a more higher role than IDEs. Delphi gets a "write in" mention also in this category.

Finally, I could not avoid noticing the age distribution of the Star Trek fans vs. the Star Wars fans. I would probably have been interesting to correlate age also with developer technologies, but this is what we have:


Categories: News, Blogs, and Tips

Writing a Windows Shell Extension

Wed, 03/16/2016 - 03:48

This is a technical article covering the content of my last week skill sprint about Writing Windows Shell Extensions in Delphi. Not really a new concept, but worth sharing. In any case, resources for the skill sprint (including video reply) are at and the code download is at

Building a Shell Extension

Windows Resource Explorer Extensions, or Shell Extensions, are in-process COM objects that implement given interfaces. In other words, you can write a COM object (part of an ActiveX or COM library) and register it in the system as a shell extensions. You've likely seen applications that adds themselves in Explorer, we can use Delphi to do the same.

The program in question is a “to-do” application tied to files. It has a simple database table storing filenames and notes about these files. The form has a DBGrid component showing only a single column containing the filenames and a memo control hosting the notes related to the current file. The DBGrid is set up as a read-only component. In fact, users should not be able to create new records except by dragging a file onto the form (a portion of the program I'm not going to discuss here, as it doesn't related with COM support) or by using an extra Explorer menu. Here is the dmeo in action, with the active shell extension additional menu item and the target application:

Creating a Context-Menu Handler

Once you have the base program running, you can add a shell extension to the system to let the user simply select a file and “send” it to the application without having to do the dragging operation, which is not always handy when there are many programs running. A context-menu extension is one of the available Windows shell extensions and is activated every time a user right-clicks a file in the Windows Explorer (given the file extensions is associated with the shell extension).

Technically, a context menu is a COM server exposing an internal object that is going to be created and used by the system. A context-menu COM object must implement two different interfaces, IContextMenu and IShellExtInit. The first interface defines specific actions for the context menu, such as defining the number of menu items to add and their text, while the second interface defines a way to access the file or files the user is operating on. This is the resulting definition of the COM server object class:

type   TToDoMenu = class(TComObject, IUnknown,     IContextMenu, IShellExtInit)   private     fFileName: string;   protected     {Declare IContextMenu methods here}     function QueryContextMenu(Menu: HMENU; indexMenu, idCmdFirst, idCmdLast,       uFlags: UINT): HResult; stdcall;     function InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult; stdcall;     function GetCommandString(idCmd: UINT_PTR; uFlags: UINT; pwReserved: PUINT;       pszName: LPSTR; cchMax: UINT): HResult; stdcall;     {Declare IShellExtInit methods here}     function IShellExtInit.Initialize = InitShellExt;     function InitShellExt (pidlFolder: PItemIDList; lpdobj: IDataObject;       hKeyProgID: HKEY): HResult; stdcall;   end;

Notice that the class implements the Initialize method of the IShellExtInit interface with a differently named method, InitShellExt. The reason is that I wanted to avoid confusion with the Initialize method of the TComObject base class, which is the hook we have to initialize the object, as described earlier in this chapter. Let’s examine the InitShellExt method first; it is definitely the most complex one:

function TToDoMenu.InitShellExt(pidlFolder: PItemIDList;   lpdobj: IDataObject; hKeyProgID: HKEY): HResult; stdcall; var   medium: TStgMedium;   fe: TFormatEtc; begin   Result := E_FAIL;   // check if the lpdobj pointer is nil   if Assigned (lpdobj) then   begin     with fe do     begin       cfFormat := CF_HDROP;       ptd := nil;       dwAspect := DVASPECT_CONTENT;       lindex := -1;       tymed := TYMED_HGLOBAL;     end;     // transform the lpdobj data to a storage medium structure     Result := lpdobj.GetData(fe, medium);     if not Failed (Result) then     begin       // check if only one file is selected       if DragQueryFile (medium.hGlobal, $FFFFFFFF, nil, 0) = 1 then       begin         SetLength (fFileName, 1000);         DragQueryFile (medium.hGlobal, 0, PChar (fFileName), 1000);         // realign string         fFileName := PChar (fFileName);         Result := NOERROR;       end       else         Result := E_FAIL;     end;     ReleaseStgMedium(medium);   end; end;

The initial portion of the method transforms the pointer to the IDataObject interface, which we receive as a parameter, into the same data structure used in a file drop operation, so that we can read the file information by using the DragQueryFile function again. This complex way of coding is actually the simplest one you can use! At the end of this operation, we have the value of the file name. Any selection of multiple files is not accepted.

We can now look at the methods of the IContextMenu interface. The first method, QueryContextMenu, is used to add new items to the local menu of the file. In this case, we add a new menu item (calling the InsertMenu API function) only if the ToDoFile application is running. We can determine this by searching for a window corresponding to the TToDoFileForm class, which should be unique in the system. The result of the function is the number of items added to the menu:

function TToDoMenu.QueryContextMenu(Menu: HMENU;   indexMenu, idCmdFirst, idCmdLast, uFlags: UINT): HResult; begin   // add entry only if the program is running   if FindWindow ('TToDoFileForm', nil) <> 0 then   begin     // add a new item to context menu     InsertMenu (Menu, indexMenu,       MF_STRING or MF_BYPOSITION, idCmdFirst,       'Send to ToDoFile');     // Return number of menu items added     Result := 1;   end   else     Result := 0; end;  

Now that items have been added to the menu, a user can select them. While the user moves over the items, a descriptive message is displayed in the status bar of the Windows Explorer. The menu ID (idCmd) we receive in the GetCommandString method is simply the relative number, starting with zero, of the items we have added to the menu. When the cursor is over an item, we simply copy a string with its description to the buffer provided by the system:

function TToDoMenu.GetCommandString(idCmd: UINT_PTR; uFlags: UINT; pwReserved: PUINT;       pszName: LPSTR; cchMax: UINT): HResult; stdcall; begin   if (idCmd = 0) and (uFlags = GCS_HELPTEXT) then   begin     // return help string for menu item     strLCopy (pszName, 'Add file to the ToDoFile database', cchMax);     Result := NOERROR;   end   else     Result := E_INVALIDARG; end;  

The final step is the operation to do once a menu item is selected. The InvokeCommand method receives a pointer to a structure holding the request. This method follows a standard pattern of first checking that the request is valid by looking at the two 16-bit words of the lpici.lpVerb value. After these preliminary (but required) steps, we check the value to see which menu item was activated; or, if the context menu has only one item, as in this case, we simply test for a value of zero. The following is the skeleton of the code, before we add the specific action:

function TToDoMenu.InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult; var   hwnd: THandle;   cds: CopyDataStruct; begin   Result := NOERROR;   // Make sure we are not being called by an application   if HiWord(Integer(lpici.lpVerb)) <> 0 then   begin     Result := E_FAIL;     Exit;   end;   // Make sure we aren't being passed an invalid argument number   if LoWord(lpici.lpVerb) > 0 then   begin     Result := E_INVALIDARG;     Exit;   end;   // execute the command specified by lpici.lpVerb.   if LoWord(lpici.lpVerb) = 0 then   begin   ... // send data to application   end; end; Sending Data to the Application

Because we have the file name the user is operating on, all we have to do in the context-menu handler is send this name to the main form of the ToDoFile application. The problem is that the context-menu handler DLL runs in the Windows Explorer process, so it cannot send the value of a memory pointer to another process. This would simply be useless; as in Win32, different applications have separate memory address spaces. We could have used OLE Automation to communicate with the main program, however in this case I've resorted to a standard Windows technique, the wm_CopyData message. This is a special Windows message, which can be used to send a memory buffer to another application: Windows will resolve all the memory conversion problems for us.

Here is the core of the code of the InvokeCommand method, that was missing above:

// get the handle of the window hwnd := FindWindow ('TToDoFileForm', nil); if hwnd <> 0 then begin // prepare the data to copy cds.dwData := 0; cds.cbData := ByteLength (fFileName); cds.lpData := PChar (fFileName); // activate the destination window SetForegroundWindow (hwnd); // send the data SendMessage (hwnd, wm_CopyData, lpici.hWnd, Integer (@cds)); end else begin // the program should never get here MessageBox(lpici.hWnd, 'FilesToDo Program not found', 'Error', MB_ICONERROR or MB_OK); end;

As the context-menu handler sends data to it, the application has to be extended to handle the wm_CopyData message. In this event handler, we receive the same structure we sent on the other side. As a result, extracting the filename is actually very simple, but keep in mind that this is so only because Windows does a lot of work behind the scenes.

The code added to the form of the ToDoFile application restores the application if it was minimized and retrieves the name of the file:

procedure TToDoFileForm.CopyData(var Msg: TWmCopyData); var Filename: string; begin // restore the window if minimized if IsIconic (Application.Handle) then Application.Restore; // extract the filename from the data Filename := Copy ( PChar (Msg.CopyDataStruct.lpData), 1, Msg.CopyDataStruct.cbData div 2); // now insert a new record (omitted) Registering the Shell Extension

After writing this shell extension, we must register it. With the Run | ActiveX Server | Register command of the RAd Studio IDE, we can register the server in the system, but only if the operating system and the shell extensions are 32bit applications. For a 64bit version of Windows you need to build the COM server with a 64bit target, and perform a manual registration by invoking regsvr32.exe and passing the COM server DLL as parameter. (Yes, this has "32" in the name even for 64 bit systems).

In any case, we still have to provide some extra information to register it as a shell extension. There are several approaches: you can edit the Registry manually, you can write a REG file, or you can add registration information right into the COM server library, which is my preferred approach.

In a Delphi COM server, the default registration takes place in the TComObjectFactory class, when the UpdateRegistry method is executed. We can modify the default registration by inheriting a class from the standard class factory class and overriding this method:

type   TToDoMenuFactory = class (TComObjectFactory)   public     procedure UpdateRegistry (Register: Boolean); override;   end; procedure TToDoMenuFactory.UpdateRegistry(Register: Boolean); var Reg: TRegistry; begin inherited UpdateRegistry (Register); Reg := TRegistry.Create; Reg.RootKey := HKEY_CLASSES_ROOT; try if Register then if Reg.OpenKey('\*\ShellEx\ContextMenuHandlers\ToDo', True) then Reg.WriteString('', GUIDToString(Class_ToDoMenuMenu)) else if Reg.OpenKey('\*\ShellEx\ContextMenuHandlers\ToDo', False) then Reg.DeleteKey ('\*\ShellEx\ContextMenuHandlers\ToDo'); finally Reg.CloseKey; Reg.Free; end; end;

In the initialization section of the COM object unit, we also need to create a new global object of this class instead of the base class factory class:

initialization TToDoMenuFactory.Create (ComServer, TToDoMenu, Class_ToDoMenuMenu, 'ToDoMenu', 'ToDoMenu Shell Extension', ciMultiInstance, tmApartment);

This is all in terms of the code. To see this demo in action, refer to the video in the skill sprint resource page linked above.

Categories: News, Blogs, and Tips

Stepping through Values in a Delphi for Loop

Mon, 02/29/2016 - 02:03

There has been a recent request to add to for loop in the Object Pascal language the ability to add a step, that is an increment different from one. Here is an alternative implementation using a loop and a custom enumerator.

Once I saw the request it reminded me of a demo I have in my Object Pascal Handbook (which originally came from my Delphi 2007 Handbook), The demo is available at blob/master/10/NumbersEnumerator/NumbersEnumerator_MainForm.pas and shows how to build an enumerator over a range of numeric value. The only missing feature is the ability to have a Step property to skip value in the sequence, counting by 2, 3, 4 and so on. The other issue of the demo is it uses an object of a class, which implies the need of creating and freeing the object. Also the way properties are set is a bit cumbersome.

In the new version (listed below) I have moved from using classes to using records, still keeping the enumeration as a nested type, and added a static class function to initialize the data structure in a single call. The goal is to be able to use it as follows:

procedure TForm1.btnTestClick(Sender: TObject); var   I: Integer; begin   for I in TNumbersRange.Range (5, 13, 3) do     Show (IntToStr (I)); end;

Of course, you could also define a global Range () function, rather than a class method.

Below is an image with the type definition, followed by a second one with the core implementation. They are followed by the code itself, which might not be as well formatted and readable (hence the images). Feel free to use it any way you like. 


Here is the record declaration and definition in text format:

type   TNumbersRange = record   public   type     TNumbersRangeEnum = record     private       nPos: Integer;       nStart: Integer;       nEnd: Integer;       nStep: Integer;     public       constructor Create (aRange: TNumbersRange);       function MoveNext: Boolean;       function GetCurrent: Integer;       property Current: Integer read GetCurrent;     end;   private     NStart: Integer;     NEnd: Integer;     NStep: Integer;   public     class function Range (aStart, aEnd, aStep: Integer): TNumbersRange; static;     function GetEnumerator: TNumbersRangeEnum;   end; implementation uses Math; class function TNumbersRange.Range (aStart, aEnd, aStep: Integer): TNumbersRange; begin Result.NStart := aStart; Result.NEnd := aEnd; Result.NStep := aStep; end; { TNumbersRange } function TNumbersRange.GetEnumerator: TNumbersRangeEnum; begin   Result := TNumbersRangeEnum.Create (self); end; constructor TNumbersRange.TNumbersRangeEnum.   Create(aRange: TNumbersRange); begin   nStart := aRange.NStart;   nEnd := aRange.NEnd;   nStep := IfThen (aRange.NStep = 0, 1, aRange.NStep);   nPos := aRange.nStart - nStep; end; function TNumbersRange.TNumbersRangeEnum.   GetCurrent: Integer; begin   Result := nPos; end; function TNumbersRange.TNumbersRangeEnum.   MoveNext: Boolean; begin   Inc (nPos, nStep);   Result := nPos <= nEnd; end;





Categories: News, Blogs, and Tips

ObjectDebugger for Delphi 10 Seattle

Wed, 02/24/2016 - 02:26

I've just uploaded on GitHub my runtime Object Inspector, or ObjectDebugger, for VCL applications.

This is a component I wrote a lot of time ago for a Delphi 3 book (but never published as open source before) that you drop on a form and lets you inspect forms and components properties at runtime, and edit quite a few of them (including string lists and some other complex properties). It offers also access to some private data. As you can see from the source, it is based on the old TypInfo unit, not the newer RTTI code.

The code is at

Feedback and contributions ar more than welcome. Below is a simple screen shot:

PS. Thanks to Alain for asking about the component, which prompted me to make the code available.

Categories: News, Blogs, and Tips

Delphi Blogs of the Week/Month #39

Mon, 02/22/2016 - 04:46

Another collection of relevant news and blog posts on Delphi and RAD Studio, from the last couple of weeks.

Official Updates, Roadmap, Birthday, and New Chinese Year

Embarcadero has released hotfixes for XE7 and XE8 and some missing C++ files for 10 Seattle.

We have a roadmap at

Another Delphi birtday, 21st, as you can read on

Chinese new year, is the year of the FireMonkey, our great multi-device development framework,

Techical Blog Posts

"Updating 32bit code to Delphi 64bit", great summary by Stephen Ball at

"Setting the VCL application icon, by Craig Chapman at

"10 Reasons to use PAServer for Windows Deployment, by Jim McKeeth at

Third Party

EurekaLog 7.4 released, 

Interesting NFC library for Android, at


Categories: News, Blogs, and Tips

RAD Studio iOS Developer Certificate Issue Possibly Caused by Apple WWDR Certificate Expriration

Mon, 02/15/2016 - 07:29

Starting yesterday (Feb 14th), a few RAD Studio iOS developers have been experiencing provisioning issues, due to the expiration of an Apple intermediate certificate, not of their own developer certificate. See the following links for more information and how to fix the issue:

While this is mentioned on Google+ and I could have tweeted a link, I have the impression this is going to hit quite a few developers in the coming days and weeks, so I felt it was better to capture it in a blog post as well. 

Categories: News, Blogs, and Tips

RAD Studio Hotfixes for XE8 and XE7 with 10 Seattle fixes

Fri, 02/12/2016 - 13:48

Yesterday Embarcadero has released two hotfixes for RAD Studio XE7 and RAD Studio XE8 including a number of bug fixes that have already been made available in RAD Studio 10 Seattle.

This effort is part of the Update Subscription benefits (the fixes are made available only to Update Subscription customers) and it is tied to the request coming from customers of increasing the support for older versions.

Here are the CodeCentral download links:

As you can see from the names, these updates have been in development and QA for a little time, but it took some extra effort to ensure their quality.

At the very bottom of the two pages above you'll see the list of fixes covered by each of the two hotifes. These fixes are in many different areas of the tool, from RTL to VCL, from mobile to FireDAC. Notably there is improved support for iOS 9, OS X El Capitan and Windows 10 for the older versions of RAD Studio. There are also some significant fixes to the XE8 generic TList and TListHelper.

Categories: News, Blogs, and Tips