gpt4 book ai didi

delphi - 警告 : Unsafe typecast of 'TSmallPoint' to 'Integer'

转载 作者:行者123 更新时间:2023-12-03 14:41:19 49 4
gpt4 key购买 nike

我在我的项目中使用此代码:

var
P: TPoint;

MyControl.Perform(WM_LBUTTONDOWN, 0, Longint(PointToSmallPoint(P)));

编译器给我一个警告:

[Warning]: Unsafe typecast of 'TSmallPoint' to 'Integer'

但是,Controls.pas 中使用了相同的代码没有任何警告 - 例如在 TControl.BeginDrag 方法中:

....
Perform(WM_LBUTTONUP, 0, Longint(PointToSmallPoint(P)));

我在 Controls.pas 单元中没有看到任何 {$warnings off}

为什么编译器会警告我,但会跳过 Controls.pas 的警告?
这段代码不安全吗?

<小时/>

编辑:在我的项目选项 -> 编译器消息 -> 不安全类型转换已选中(默认情况下未选中)。
也许这就是@David 和@Ken 无法重现警告的原因。

最佳答案

这是因为您在“项目”->“选项”->“编译器消息”中检查了不安全类型转换警告。取消选中此选项是安全的(如其上方的不安全类型不安全代码。(见下文。)

我无法重现该警告,因为我未检查不安全类型转换。它不再适用。 (在开发 Delphi for .NET 时,它被添加到 Delphi 6 或 7 中以实现 .net 兼容性,以便更轻松地编写同时适用于 .NET 和 Win32 的代码;由于 Delphi for .NET 产品已停产,该警告(以及上面的两个)不再适用)。这三个警告中的“unsafe”使用了.NET中“unsafe”的含义,意思是“非托管”。

来自 Delphi 7 帮助文件(搜索“编译器更改”)(强调我的):

The Delphi dcc32 compiler now supports three additional compiler warnings: Unsafe_Type, Unsafe_Code, and Unsafe_Cast. These warnings are disabled by default, but can be enabled with the compiler directive {$WARN UNSAFE_CODE ON}, compiler command line switch (dcc32 -W+UNSAFE_CODE), and, in the IDE, on the Project|Options|Compiler Messages page.

This feature is intended to help you port your code to the managed execution environment of Microsoft's .NET platform. In a managed execution environment, "unsafe" means the operation cannot be verified during the static analysis performed by the Just In Time (JIT) compiler. Such code might pose a security risk, since there is not enough information for the JIT compiler to verify its runtime behavior. Examples of unsafe code include pointer operations and memory overwrites.

关于delphi - 警告 : Unsafe typecast of 'TSmallPoint' to 'Integer' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14076750/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com