Unicode Strings and

As already mentioned in the previous chapter, the Win32 API has in most cases separate calls for ANSI strings (marked with A) or Unicode strings (marked with W for Wide). To be more precise, these APIs use either a PAnsiChar or a PWideChar. I've already stated that the fact that the Win32 API is so heavily based on UTF-16 makes this format the most obvious choice for a native Windows development tools like Delphi.

In many cases, the ANSI versions of the Windows APIs call the Wide versions performing an extra conversion. In other cases the Wide APIs are indeed slower. Having changed at the same time as the string format, the PChar type alias, and the version of the Windows API mapped means you can convert most of your code in a very simple way: You do absolutely nothing! Consider the following example:

TextOut (Canvas.Handle, 104, 224, PChar(str1), Length (str1));

This works equally well in Delphi 2007 and in Delphi 2009, even if the Windows API call ends up being different. This is true for most Win32 API calls with string parameters, that have been remapped from the A (ASCII) to the W (Wide) version, along with a change at the compiler level of PChar from PAnsiCharto PWideChar.

There are some specific APIs, though, that don't have two separate version and invariably require a PAnsiChar pointer. A typical example is given by the GetProcAddress function, as exported DLL symbols are limited to Ansi. In those cases you might have to convert both the string and to cast it to a different character pointer type. So a line like: | GetProcAddress (hmodule, PChar (strFnName)) would become:

GetProcAddress (hmodule, PAnsiChar ( AnsiString(strFnName)));

Another particular case is that of the CreateProcessW functions. This one exists, but can modify the content of the string with the executable file name and will fail with an exception if you pass a constant string value.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment