Providing an IsUnicodeVersion API in Your Delphi Project


I have recently migrated my project from Delphi 2007 to Delphi XE8 (and Seattle). It is a COM+ DLL and all API interfaces stay the same. However, for backward compatibility, I would like the users to be able to use the ANSI version of DLL as well. Therefore, I decide to ship the ANSI version as well, and allow users to be able to detect which version (ANSI or Unicode) they are using.

unicode Providing an IsUnicodeVersion API in Your Delphi Project API delphi

unicode

Delphi 2007 is the last version that is ANSI, which means that the size of Char is 1 and the string is made of 1-size character, e.g. string = array of char = AnsiString. Therefore, the following API should return True under version after D2009 and False if compiled using D2007.

const
  C_TRUE = 1;
  C_FALSE = 0;

function TUtility.IsUnicodeVersion: Integer;
begin
  Result := IfThen(SizeOf(Char) <> 1, C_TRUE, C_FALSE);
end;

The compiler is clever enough to optimize the above code into a simple Result := C_TRUE or Result := C_FALSE since the compiler knows that the value of SizeOf(Char) is constant.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
298 words
Last Post: Self-Promoting Introduction to Job Posts - Algorithm Engineer
Next Post: C++ Coding Exercise - xxd - make hex dump on Windows

The Permanent URL is: Providing an IsUnicodeVersion API in Your Delphi Project

Leave a Reply