加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 04:27:04 所属栏目:大数据 来源:网络整理
导读:我在我的项目中使用这段代码: var P: TPoint;MyControl.Perform(WM_LBUTTONDOWN,Longint(PointToSmallPoint(P))); 编译器给了我一个警告: [Warning]: Unsafe typecast of 'TSmallPoint' to 'Integer' 但是,Controls.pas中没有任何警告使用相同的代码 – 例
我在我的项目中使用这段代码:
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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读