Delphi 2007 and XE2: Using NativeInt

Chau Chee Yang picture Chau Chee Yang · Oct 3, 2011 · Viewed 9.4k times · Source

Since Delphi XE2, NativeInt has new meaning. At 32 bits runtime, NativeInt is 32 bits integer. At 64 bits runtime, NativeInt is 64 bits integer.

I have some source files that use third party DLL (both 32 and 64 bits). These DLL use 32 and 64 bits integer in both 32 and 64 platform respectively.

These source files works in Delphi 2007 - Delphi XE2 32 bits platform without problem:

e.g.:

function Test: Integer;

When I attempt to migrate those source files to Delphi XE2 64 bits platform, the above function no longer works as it require 64 bits integer. In order to make the source works for both 32/64 platforms, I change to

function Test: NativeInt;

And it works.

However, the declaration doesn't work in Delphi 2007 as Delphi 2007 treat NativeInt as 64 bits integer: SizeOf(NativeInt) = 8

I may solve the problem by using conditional directive RtlVersion or CompilerVersion to

function Test: {$if CompilerVersion<=18.5}Integer{$else}NativeInt{$ifend};

But that would be tedious as there are many declaration in source files.

Is there a better ways to make the source files work in Delphi 2007-XE2 win32 and XE2 win64 platform?

Answer

kludg picture kludg · Oct 3, 2011

A better alternative is to redeclare NativeInt type itself:

{$if CompilerVersion<=18.5}
type
  NativeInt = Integer;
{$ifend}

It should be done once per unit and can be implemented as a part of common *.inc file.