升级到Delphi 6 - 兼容性问题之二

本文介绍了Delphi 6版本的主要变化。包括二进制Form文件可能不兼容老版本,需存为文本格式;编译宏$WRITEABLECONST默认关闭,应避免使用可赋值常量;Cardinal类型负数值处理机制改变;单元DsgnIntf改名,相关引用需更新;组件编辑器类的继承结构有变化,可能需修改老工程。
 

 

一.             潜在的二进制Form文件的不兼容

 

过去,新版本Delphi创建的二进制Form文件(或称DFM文件)可以被老版本的Delphi读取。但是现在不行了。某些二进制Form文件可能不能被老版本正确的读取,其原因是Delphi 6内部的字符串的流化和原先不同。过去,流化操作假设一个本地特殊的字符集。而现在新的流化操作假设字符集为UTF-8。由此带来的问题就是,如果Delphi 6的二进制Form文件中包含有码值大于127的字符出现(比如版权符®),则该文件就不能被Delphi 的老版本正确读取。

如果你想在老版本的Delphi 中打开Delphi 6 Form文件,那么请先将该Form文件存为文本格式而非二进制格式。

 

Potential binary form file incompatibilities

In the past, binary form files (or DFM files) created with newer versions of Delphi could be read by older versions. This is no longer true in Delphi 6; some binary form files may be read incorrectly because of the way that Delphi 6 performs internal string streaming. In the past, streaming was performed assuming a locale specific character set. Now streaming assumes that the character set is UTF-8. As a consequence, if there are characters with a code greater than 127 (such as the copyright symbol ? in a Delphi 6 binary form file, that file cannot be read by older versions of Delphi.

If you intend to use a Delphi 6 form file (including older form files imported into and modified with Delphi 6) in an older version of Delphi, the file should be saved in text format instead of binary format.

 

二.             有关可赋值的常量

 

编译宏$WRITEABLECONST现在的缺省值改为关,这是为了防止Delphi的工程中运用可赋值的常量。可赋值的常量,也就是定义一个常量,但是却允许在运行期间改变其值。例子如下:

 

const

 

   foo: Integer = 12;

begin

   foo := 14;

end.

在以往的Delphi版本中,有这么一个特性:常量不是真正的常量。使用编译宏$WRITEABLECONST OFF,则以上的代码中的BeginEnd之间的Foo的赋值将引发一个编译错误。若要避免它,只需将Foo的声明改为 var

你可能有将常量用作一个可以初始化的局部变量的代码,比如:

 

procedure MyProc;

 

const

   somedata: Integer = 12;

begin

   Inc(somedata, 3);

end;

你要做的是将局部常量移到过程的外部声明,使其成为一个全局的变量。然后代码变为:

 

var

 

   somedata: Integer = 12;

procedure MyProc;

begin

   Inc(somedata, 3);

end;

对于过度依赖于常量的代码(比如ActiveX 控件的包装器),可以通过在源文件中插入一个{$WRITEABLECONST ON}的编译命令来修正。这一特性,在RTL VCL CLX,和 DB 等核心的源代码中被禁止使用,但是在周边的单元比如ActiveX 控件的包装器中倒可以接受。

总而言之,你应该意识到“可赋值的常量”这个说法的自相矛盾。Delphi的以往版本中的这一特性,只是为了与老的16位的编译器的兼容而保留,但现在对于Delphi的开发者来说已经毫无意义了。要养成好的编程习惯,尽量避免使用可赋值的常量。

 

Change in writeable constants

The $WRITEABLECONST compiler switch ( aka $J ) now has a default state of OFF, which will prevent Delphi projects from having writeable constants. Writeable constants refers to the use of a typed constant as a variable modifiable at runtime. Here is an example:

 

const

 

   foo: Integer = 12;

begin

   foo := 14;

end.

 

In prior releases of Delphi, this was an acceptable construct; constants weren't really constant. With $WRITEABLECONST OFF, this code will now produce a compile error on the assignment to the foo variable in the begin..end block. To fix it, simply change the const declaration to a var declaration.

You may have code that uses a typed const as an initialized local variable with global lifetime, like this:

 

procedure MyProc;

 

const

   somedata: Integer = 12;

begin

   Inc(somedata, 3);

end;

You will need to move the local const out of the procedure and declare it for what it is: a global variable. After making this change, the code segment above becomes:

 

var

 

   somedata: Integer = 12;

procedure MyProc;

begin

   Inc(somedata, 3);

end;

Code that relies heavily on the typed const quirk (such as the ActiveX control wrapper generator) can insert a {$WRITEABLECONST ON} directive in the source file as a quick fix. This practice is forbidden in the RTL, VCL, CLX, and DB source code and discouraged in the IDE sources, but acceptable for fringe units such as ActiveX control wrappers.

In general, you should note that the phrase "writeable constant" is an oxymoron. Prior versions of Delphi allowed them by default to maintain compatibility with an older 16-bit compiler, which is no longer important for most Delphi developers. Use good programming practice; avoid writeable constants.

 

三.             Cardinal类型的负数值

过去,Delphi处理Cardinal类型的负数值时使用32位的机制,这样使得结果为一些零头的值(Cardinal类型允许的最大的值与当前值的差加一)。以下为例子:

var

 

   c: Cardinal;

   i: Int64;

begin

   c := 4294967294;

   i := -c;

   WriteLn(i);

end;

在以往版本的Delphi中,I的值应当是2。但是这种情况下这个值明显不对。Delphi 6中,Cardinal类型是先转化为64位的有符号类型,然后取其负数值,所以最终结果I的值为-4294967294

可能有已经存在的代码依赖于原先错误的Cardinal负数值的实现方法。读者应当对Delphi 的这一新特性引起足够的重视。花足够多的时间来检验你的代码中是否使用了对Cardianl的值的取负数是很值得的,同时确信一点,Delphi的这个新的特性对你的程序的正确性不构成影响。

 

Unary negation of Cardinal type

In the past, Delphi handled unary negation of Cardinal type numbers using 32 bit operations, which could lead to some odd results. Here is an example of code which uses unary negation:

 

var

 

   c: Cardinal;

   i: Int64;

begin

   c := 4294967294;

   i := -c;

   WriteLn(i);

end;

    In previous versions of Delphi, the value of i displayed would be 2. This is obviously incorrect behavior for this case. In Delphi 6, the unary negation is handled after promoting the Cardinal type to a 64 bit signed type, so the final value of i displayed is -4294967294.

       It is possible that existing code may rely on the incorrect behavior of unary negation. Delphi users should be aware of this new behavior. It may be worth your time to check your code for instances of unary negation of Cardinal variables, and make sure that your application responds to the new behavior appropriately.

 

四.             单元DsgnIntf改名及相关变化

工程中对于DsgnIntf的引用,需要更新到一个新的名字:DesignIntf。可能还得加上DesignEditorsVCLEditors RTLConsts 到你的引用列表中。并且你还得将designide加入到你的PackageRequires的列表中。对dsnide50的引用可能得改为DesignIde,如果Delphi没有自动更改的话。

       任何引用了IDesigner的运行期Package,需要用IdesignerHook来防止运行期时需要designide。运动期代码中,IDesignerHook 足以应付。设计时间的代码也可以使用IDesigner,但是代码要象下例一样:

 

var

 

  RealDesigner: IDesigner;

...

SomeDesignerHook.QueryInterface(IDesigner,RealDesigner);

...

IDesignerHook 的一个实例上获得真正的IDesigner接口。IDesignerHook的使用只需要引用ClassesForms两个单元。IDesigner还需要用DesignIntf,该单元被包含在许多其它Packatge中,而其中的一些可能导致不可再次分发。

Borland在此希望感谢Field的测试员Matt Palcic,感谢他让我们对此引起注意。

 

DsgnIntf renamed and related changes

 

References to DsgnIntf in your project should be changed to the new Delphi 6 name, DesignIntf. You may also need to add DesignEditors, VCLEditors and RTLConsts to your uses clause. You will also need to add designide to your package's requires list. References to dsnide50 should probably also be changed to designide if that isn't changed automatically by Delphi.

 

Any runtime packages that use IDesigner need to use IDesignerHook to avoid a requirement of designide at runtime. In runtime code, IDesignerHook should suffice. Design-time code can use IDesigner, but should use something like

 

var

 

  RealDesigner: IDesigner;

...

SomeDesignerHook.QueryInterface(IDesigner,RealDesigner);

...

To get the real IDesigner interface from an instance of IDesignerHook. IDesignerHook only requires Classes and Forms to be available. IDesigner requires DesignIntf, which includes many other packages, some of which may not be redistributable.

 

Borland wishes to thank field tester Matt Palcic for bringing these changes to our attention.

 

五.             组件编辑器的变化

Delphi 6中,类TComponentEditor有了不同的祖先。在Delphi 5中,它从TInterfacedObject

继承而来;现在它从一个新的类,TbaseComponentEditor继承而来。同时,TComponentEditorClass也变为TbaseComponentEditor的类类型,而不是TComponentEditor的类类型。这些体系结构上的变化可能需要你修改你的老的Delphi的工程。

 

Component editor changes

The class TComponentEditor has a different ancestry in Delphi 6. In Delphi 5, it descended from TInterfacedObject; it now descends from a new class, TBaseComponentEditor. Also, the class TComponentEditorClass is now a class of TBaseComponentEditor instead of TComponentEditor. These changes in hierarchy may require you to modify your older Delphi projects.

 

Borland wishes to thank field tester Clive Walden for bringing these changes to our attention.

 

 

 

 

 

Delphi实例开发教程》源代码包说明 __________________________________________________________________ (一)源代码程序包内容: 源代码程序包的目录结构如下: \(根目录) | |————Readme.txt(说明文件必须放在这个地方) | |————\本书大案例(目录) | | |————|————DataBase(目录,存放大案例的数据库文件为“date”和数据库连接文件TEST.UDL) | | |————|————Materials(目录,存放大案例的登录logo) | | |————|————EXE(目录,存放可执行文件,为channelplay.exe) | | |————|————Setup(目录,存放安装文件) | | |————|————Source(目录,存放源代码,这个目录可以进一步细分) | | |————|————|————code(保存源程序) | | |————|————|————dcu(保存中间编译文件) | | |————|————|————exe(保存可执行文件) 其中安装程序文件夹里面是本案例系统的安装程序,与程序源代码无关。用户既可以以它来安装信息搜索系统程序,也可以直接从setup文件夹中直接运行程序。(当然,两者前提是数据库配置好,具体的配置方法参考案例书第4章的案例分析与完善) 另外在source文件夹中还有三个文件夹,code,dcu与exe是在编写程序代码是为了方便管理而设置的(这里的exe文件夹中文件和大案例下的EXE文件夹一样)。在一般情况下,如果对编写的delphi项目工程进行设置而直接保存到一个文件中,那么在运行程序代码是就会在此文件夹中产生中间编译文件和最后的运行程序。如此则这同一个文件夹中就仅有项目文件、单元文件也会有在编译过程中产生的编译文件和最后程序等。这样就方便程序的管理和查看。所以在编写delphi项目工程之前最后设置三个文件夹分别用于保存源程序代码、中间的编译文件和应用程序,以方便管理。(具体设置是先打开delphi7,选择[Project]->[options],打开一个对话框,directories/Conditionals页,在Output directory里填写保存应用程序的文件夹路径,在Unit Output directory填写保存中间编译文件的文件夹路径。这两个路径最后写与源程序代码文件夹相关的相对路径。如本书的项目工程,源程序代码保存在code文件夹中,那么Output directory里填写“..\exe”,而Unit Output directory里填写“..\dcu”。) (二)下面介绍大案例code文件夹里各个文件的内容和关系:(code文件夹里的几个文件夹都与本程序无关,可以删除) 1)ChannelPlayer.dpr-------本案例的工程文件 它说明项目中各个单元文件的运行创建,并启动生成应用程序 2)MainFrm.dfm-----------------主窗体的窗体文件 它保存软件主界面窗体所作的属性 MainFrm.pas------------------主窗体的单元文件 它保存与软件主界面窗体相关的程序代码 以下各组文件的内容可以参考本书的第二章的实例分析与完善 3)MaintainFrm.dfm---------------对已保存的定制进行修改维护的窗体文件 MaintainFrm.pas-------------对已保存的定制进行修改维护的单元文件 4)BrowserFrm.dfm----------浏览器的窗体文件 BrowserFrm.pas----------浏览器的单元文件 5)CustomSearchFrm.dfm-------实现定制搜索功能窗体文件 CustomSearchFrm.pas-----------实现定制搜索功能单元文件 6)DisplayFrm.dfm----------------设计显示和操作搜索信息界面的窗体文件 DisplayFrm.pas--------------设计显示和操作搜索信息界面的单元文件 7)DisplayFra.dfm------------显示的搜索到的网站的窗体文件 DisplayFra.pas------------显示的搜索到的网站的单元文件 8)InputFra.dfm--------------进行搜索前对一些基本输入控件处理的窗体文件 InputFra.pas---------------进行搜索前对一些基本输入控件处理的单元文件 9)RollingNewsFrm.dfm---------用于滚动新闻设置的窗体文件 RollingNewsFrm.pas---------用于滚动新闻设置的单元文件 10)SettingFrm.dfm-----------用于系统的设置的窗体文件 SettingFrm.pas-----------用于系统的设置的单元文件 11)SiteArrangementFrm.dfm---整理“站内搜索”子模块中用户设置的窗体文件 SiteArrangementFrm.pas---整理“站内搜索”子模块中用户设置的单元文件 12)SiteSearchFrm.dfm--------主要用于实现站内搜索的窗体文件 SiteSearchFrm.pas--------主要用于实现站内搜索的单元文件 13)SplashFrm.dfm-----------实现系统开始运行闪屏的窗体文件 13)SplashFrm.pas-----------实现系统开始运行闪屏的单元文件 14)ViewFra.dfm--------------设定查询数据库保存的搜索结果信息条件输入的窗体文件 ViewFra.pas--------------设定查询数据库保存的搜索结果信息条件输入的单元文件 15)ViewInfoFrm.dfm----------对数据库中信息查询的窗体文件 ViewInfoFrm.pas-----------对数据库中信息查询的单元文件 16)UnitSearch.pas-----------实现百度、新浪等搜索引擎线程定义的单元文件 以上是code文件夹的主要文件,也是本案例工程的所有代码文件,其中ChannelPlayer.dpr是工程文件,记录本工程的信息;其他的窗体都是在本系统程序使用时动态调用(各个文件的调用关系可以参考本书第一章实例分析与完善的概要模块设置部分)。code文件夹中其他文件是在程序编译运行时候有delphi7自动生成,用户可以管。其中*.~后缀的文件是相应文件名的备份,它们也是由delphi7自动生成。 (三)source文件夹中dcu文件夹中保存的文件是程序在编译的时候生成的中间文件,它们都对应code文件夹中的每个*.pas单元文 件。 (四)source文件夹中exe文件夹的文件: ChannelPlayer.exe---是本项目工程运行是自动生成的可执行运用程序 (五)系统需求: 1. 硬件要求: 基本配置为: ¢ CPU:Intel Pentium II-class 300 MHz (Intel Pentium III-class 600 MHz recommended) 这表明需要至少300MHz的奔III处理器,笔者所使用的是雷鸟1G,应该说 性能还是可以的。 ¢ RAM:96MB(128MB recommended) 实际上,128M内存运行起来还是觉得够,最好能够有256M以上内存。笔 者使用的是256M DDR内存。 ¢ Available hard disk space(for install):250MB ¢ Available hard disk space(post install):155MB 事实上,这个要求仅仅是针对安装delphi7所提出的 要求。 ¢ Video:800×600,256 colors 只要是14英寸显示器就可以达到这个要求。 ¢ CD-ROM:required 这只是目前计算机的标准配置,实际上,如果是采用光盘安装的话,CD-ROM 根本就派上用场。 ¢ Operating System:Microsoft Windows 2000(or up) ¢ Microsoft Internet Explorer 5.5(or up) 2. 本系统工程的开发环境 本信息搜索系统程序是在WindowsXP的Delphi7环境下开发编写的,并且通过测试。另外本系统也能在windows98,me,2000 的delphi7环境下开发编写。对于delphi的版本,虽然delphi6delphi7相差大,但由于本系统的网络部分用到的一些网络控件在delphi6没有,所以如果想使用delphi6编写的程序员要用delphi6的网络控件代替delphi7的网络控件。对于刚出的delphi8,由于它是基于net框架的,主要用于网页编写,编写应用程序的方式一样,而且delphi7的很多控件都没有,所以一般能在delphi8中开发运行(除非重新编写所有的代码)。 (六)注意事项: 本系统要注意开发环境的选取,如上面所说的,最好使用delphi7以下的版本,能使用delphi8。另外还要注意系统程序所用到的数据库的设置,先是把数据库还原,然后设置TEST.UDL里的连接参数,使程序能与数据库建立关系。这样之后才能运行程序。最后,由于本系统是借助几大门户网站的搜索功能实现搜索,所以要注意这些门户网站的更新,根据它们的更新来更新本系统。 (七)技术支持信息: 本系统的运行与操作: 本系统在运行之前要先设置好与数据库的连接(参考第4章的实例分析与完善)。然后运行程序会出现一个主界面,界面中间是有关新闻的滚动。点击“定制搜索”功能可进入定制搜索界面,在界面的左边可以选择是搜索以前的关键字还是重新输入,如果重新输入着在“关键字”栏里填写关键字,然后选择相关设置就可以搜索。对于搜索到的信息可以直接单击进入网页,也可以右键点击选择同的操作。保存搜索信息公能用于对本次搜索信息保存到数据库中。点击主界面的“查看信息”功能可以进入查看信息界面,其界面与定制搜索界面相识。 点击主界面的“滚动新闻”功能和“系统设置”功能可以设置主界面的新闻滚动和本系统的信息。 _________________________________________________________________ 技术支持的联系方式: 如果用户对于本系统程序有什么疑问可以发邮件到: hsw_gm@21cn.com tenny_2000@163.com sqwen@yeah.net kukocpoplee@tom.com Jingfei2000@21cn.com _________________________________________________________________
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值