Delphi 2007 和 XE2:使用 NativeInt
从Delphi XE2开始,NativeInt有了新的含义。在 32 位运行时,NativeInt 是 32 位整数。在 64 位运行时,NativeInt 是 64 位整数。
我有一些使用第三方 DLL(32 位和 64 位)的源文件。这些 DLL 在 32 和 64 平台上分别使用 32 和 64 位整数。
这些源文件在 Delphi 2007 - Delphi XE2 32 位平台上工作没有问题:
例如:
function Test: Integer;
当我尝试将这些源文件迁移到 Delphi XE2 64 位平台时,上述函数不再工作,因为它需要 64 位整数。为了使源代码适用于 32/64 平台,我将其更改为
function Test: NativeInt;
And it Works。
但是,该声明在 Delphi 2007 中不起作用,因为 Delphi 2007 将 NativeInt 视为 64 位整数: SizeOf(NativeInt) = 8
我可以通过使用条件指令 RtlVersion 或 CompilerVersion 来解决该问题,
function Test: {$if CompilerVersion<=18.5}Integer{$else}NativeInt{$ifend};
但这会很乏味,因为有很多声明在源文件中。
有没有更好的方法使源文件在Delphi 2007-XE2 win32和XE2 win64平台上工作?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更好的替代方法是重新声明 NativeInt 类型本身:
每个单元应该执行一次,并且可以作为通用 *.inc 文件的一部分来实现。
A better alternative is to redeclare NativeInt type itself:
It should be done once per unit and can be implemented as a part of common *.inc file.
哎呀:为什么不直接使用 LongInt(需要 32 位)和 Int64(否则)?
并在无关紧要的地方使用“整数”?
使用“NativeInt”似乎是违反直觉的,因为你知道它在不同的时间意味着不同的东西......
PS:
您始终可以定义您自己的自定义类型,然后 $ifdef 它!
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!