Migrating to Unicode

I recently started migrating my current client’s project to Unicode in a proof of concept application whose purpose was to decide on whether or not to upgrade to the latest version of RAD Studio.

The project is just under a quarter million lines of code to give you some perspective on it’s size, and it’s one of the most complex systems I have ever worked on.  It uses hcOPF extensively, incorporates tons of business logic, and it monitors and controls external serial devices.

Anticipated Challenges

I anticipated that the Serial port library used would be one of the most challenging items to port.  It only supports up to Delphi 2005, and doesn’t appear to have been updated since October 2003.  That said, the library was in use in the previous version of the product and proved itself to be reliable, so I chose to adopt it for the new release and had the same results even under Windows 7.

Other third party components used are the JEDI JCL/JVCL, hcOPF, DevExpress, TMS and PngComponents.  I didn’t anticipate any issues with the JEDI, DevExpress, or TMS components since they have been supporting Delphi releases almost as soon as they become available and have a good reputation in the community.  I knew that when EMB acquired the PNG components library they made some changes, in addition to trying to remove the old project’s code off of the Internet.  It took me some time to find the PNGComponents for Delphi 7 when I developed the app, and I must say it implements great designers, and is a pleasure to use.


Since this is a pilot project, using the current source, I wanted to make minimal changes to Unicode enable the application, in order to reduce the testing burden and likelihood of breaking proven code.  I also wanted to keep the source compatible with Delphi 7-2007 in case we chose not to upgrade, without having to discard all the work done for Unicode support in case we decided to upgrade at a later date.

I started by creating the D15 projects for all the components used and getting them installed into the IDE.  This proved to be my first road block as documented.  It took a blog post, a couple calls to tech support and one to sales before I managed to get a timed key to enable Delphi XE’s command line compiler and all of the features of the product.  My advice to anyone attempting to perform an upgrade is to contact EMB to get a timed key to begin with.  I would also advise to download the Delphi XE ISO from here.  When I attempted to upgrade my installation using the timed key, it kept complaining about not being able to resume a broken download, and it didn’t matter what option I chose, it would just repeatedly show the same dialog which doesn’t even tell you what download is broken.  After uninstalling Delphi XE I got the same error attempting to re-install the product.  Only when I installed from the ISO was I able to get the product running again.

Then I read Cary Jensen’s migration paper, and that from Nick Hodges.  I enabled all the Unicode compiler warnings as Marco Cantu recommended and paying special attention to <W1058 Implicit string cast with potential data loss from ’string’ to ‘AnsiString’> errors I proceed to go through the code and decide what needed to remain an AnsiString and what changes were needed to minimize any implicit conversions.

The Reality

The new PNG support in Delphi does not contain the TPNGImageList and TPNGCollection components.  I had used both in my application.  I attempted to compile the D7 version, and quickly abandoned that when I encountered several ‘cannot assign to left side’ errors.   Prior to discovering that  Uwe Raabe had submitted an update to the components to CodeCentral, I changed my application under D7 to load items from Resources. In some cases though, I was using components that required Imagelists so finding Uwe’s port saved a some effort in converting them for use under Delphi XE, and kept my source code compatible with D7.  Thanks Uwe!

To me this exemplifies one of the primary difficulties in upgrading projects to a new version of Delphi.  One that Embarcadero, likes it’s predecessors, doesn’t seem to understand, because if they did, why would they have removed the PNGComponents open source project?  Surely developers don’t upgrade their version of Delphi simply for new image support!  It would also be foolish to think you can stop the sharing of what was once open source on the Internet, or that attempting to do so wouldn’t give you a bad rep with prospective customers.  The reality is that there are Delphi developers out there who use old versions because they don’t like .NET, think the old IDEs are leaner and do everything they need, or are locked into their current IDE because of the sheer size of their code base and thus the magnitude of their porting effort, or because they don’t have source for their components.  By making the migration more difficult, these customers are going to be left behind no matter how many BOGO offers are made.

Anyway, I digress.  I made liberal use of CharInSet(), converted my serial component to use AnsiString/AnsiChar and made numerous other little changes and was amazed that it all ran just fine.  Porting this particular project to Unicode was much easier than I anticipated.  While the product still has to under go complete testing by the QA people, I am happy to report that the unicode migration project took less than the week I estimated, and none of the potential problems I was concerned with have materialized.  For those of you that haven’t taken the leap, I would encourage you to do so.

Tags: ,

8 Responses to “Migrating to Unicode”

  1. Rudy Velthuis Says:

    For what is the timed key necessary? Products like XE Pro, Xe Ent and XE Arch all come with everthing required, AFAICT, including the command line compiler.

  2. Rudy Velthuis Says:

    Ok, I see you upgraded. Forget my previous question.

  3. Fabricio Says:

    That’s my experience too. When 3rd parties lib things are solved, we had very few problems migration to Unicode Delphi. Since we used almost no 3rd party lib in my Delphi work, the migration was smooth.

  4. Jolyon Smith Says:

    Hmmm, are you sure you are done yet?

    Does this project involve a database ? Have you migrated the database to a Unicode schema ? Have you revisited all input validation code to ensure that validations are being correctly performed ?

    Example: “if CharInSet(Key, ['A'..'Z'])” is not a Unicode-complete/safe test for uppercase keypress input.

    If not then all you have is essentially an ANSI app that happens to use a Unicode string type to carry those ANSI strings around. If that was all you are after then yes, you are done. But if you wanted to “migrate to Unicode” then you quite possibly aren’t there yet.

    +1 for the observations re closing down open source projects and impeding migrations.

  5. Larry Hengen Says:


    The project uses ADO for database connectivity to SQL Server 2008. I use hcOPF as an intermediate layer to the database, and it performs the vast majority of my input validations. You’re correct in that our database does not use Unicode data so perhaps the title of my post was somewhat inaccurate.

  6. Jolyon Smith Says:

    I am sure you are not alone Larry - the biggest concern I had/have with the way the transition to Unicode was handled in Delphi was that it promoted the idea that a) it would be simple - just a matter of fixing some compiler warnings and b) that once you have fixed the compiler errors/warnings, you now have a Unicode application. Hurrah! :)

    Unfortunately Unicode is a far bigger and more complex beast than can be tamed by simply changing the string type (in all but the most trivial of apps).

  7. Chris Says:

    ‘PNGComponents’ wasn’t written by the author of TPNGObject/TPNGImage: rather, it was an open source project written on top of the TPNGObject code, which wasn’t itself ‘open source’ in the technical sense (check out the original licence if you don’t believe me). That said, I agree it would have been good if the PNGComponents project could have been integrated into the VCL.

  8. Warren P. Says:

    Well, making it run in Delphi XE, and work properly with existing functionality is the goal and he achieved it. So great for him! :-)

    Once I had done a few projects the rest were easy, and now I consider updating applications to Unicode level Delphi language pretty trivially easy, because I’ve done it LOTS.

    I have even ported a bunch of components up, and found that this also gets pretty easy after a while.


Leave a Reply