delphi – 警告:不安全的“TSmallPoint”类型转换为“整数”

前端之家收集整理的这篇文章主要介绍了delphi – 警告:不安全的“TSmallPoint”类型转换为“整数”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的项目中使用这段代码
var
  P: TPoint;

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

编译器给了我一个警告:

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

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

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

Controls.pas中没有看到任何{$warnings off}.

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

编辑:在我的项目选项中 – >编译器消息 – >检查不安全的类型转换(默认情况下未选中).
也许这就是为什么@David和@Ken无法重现警告.

解决方法

这是因为您在Project->选项 – >编译器消息中选中了不安全的类型转换警告.这是非常安全的取消选中(不安全的类型和不安全的代码上面)(见下文)

我无法重现警告,因为我没有选中不安全的类型.它不再适用. (当Delphi for .NET开发Delphi时,它被添加到Delphi 6或7中,以实现.NET兼容性,以便更容易地编写适用于.NET和Win32的代码;由于Delphi for .NET产品已被停用,所以该警告(和上面的两个)不再适用).这三个警告中的“不安全”使用“不安全”的.NET含义,意思是“不受管理”.

从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.

原文链接:https://www.f2er.com/delphi/101458.html

猜你在找的Delphi相关文章