14

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?

Danilo Casa
  • 506
  • 1
  • 9
  • 18
Chau Chee Yang
  • 18,422
  • 16
  • 68
  • 132
  • Please read Barry Kelly's post here: http://stackoverflow.com/questions/1568685/how-to-also-prepare-for-64-bits-when-migrating-to-delphi-2010-and-unicode – paulsm4 Oct 03 '11 at 16:19

2 Answers2

21

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.

kludg
  • 27,213
  • 5
  • 67
  • 118
-3

Gee: why not just use LongInt (where you require 32-bits) and Int64 (otherwise)?

And just use "integer" where it doesn't matter?

It just seems counter-intuitive to use "NativeInt" where you KNOW it's going to mean different things at different times...

PS: You can always define your OWN, custom type, and $ifdef it!

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 5
    Because *NativeUInt* maps to either *Longword* or *UInt64*, depending on the platform. But if code must be cross-platform (Win32 or Win64), it makes sense to use `X: NativeUInt;` instead of `X: {$IFDEF Win32}Longword{$ENDIF}{$IFDEF Win64}UInt64{$ENDIF};`. *NativeInt* and *NativeUInt* were **defined** for that purpose, but apparently not always correctly. – Rudy Velthuis Oct 04 '11 at 14:25