分享
 
 
 

如何将C/C++程序转译成Delphi(十一)

王朝delphi·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

4. Macros

In C it's possible to define macros. Macros are not available in Delphi, so functions must be used to translate C-macros. In most cases it's easier to translate a macro based on the information about it from the documentation than trying to translate the code directly.

One example of a macro:

#define PRIMARYLANGID(lgid) ((WORD )(lgid) & 0x03ff)

Here, quite clearly, the macro accepts integers. A valid translation of this macro is

Function PRIMARYLANGID (lgid: DWord): DWord;

Begin

Result := lgid and $03FF

End;

The following macro accepts any datatype as parameter:

#define max(a,b) (((a) > (b)) ? (a) : (b))

It compares the values of parameter a with parameter b and returns the higher value. So that our function can pass any datatype, we can use variants.

Function Max (A, B: Variant): Variant;

Begin

If A > B then

Result := A

else

Result := B

End;

I do recommend implementing the Delphi translation on the basis of the macro's documentation.

To insert into the Tutorial in Section 4 immediately before the line

"I do recommend implementing the Delphi translation on the basis of the macro's documentation."

--

Sometimes it is not possible to translate macros so that there is a 1:1 correspondence between the C code and the Delphi translation.

For example, some header files use macros in #define statements for declaring constants.

Here is an example from the clusapi header file.

#define CLUSPROP_SYNTAX_VALUE( type, format ) ((DWORD) ((type << 16) | format))

The ensuing declarations use a macro named CLUSTER_PROPERTY_SYNTAX to calculate the values of the constants:

typedef enum CLUSTER_PROPERTY_SYNTAX {

CLUSPROP_SYNTAX_ENDMARK = CLUSPROP_SYNTAX_VALUE( CLUSPROP_TYPE_ENDMARK, CLUSPROP_FORMAT_UNKNOWN ),

CLUSPROP_SYNTAX_NAME = CLUSPROP_SYNTAX_VALUE( CLUSPROP_TYPE_NAME, CLUSPROP_FORMAT_SZ ),

CLUSPROP_SYNTAX_RESCLASS = CLUSPROP_SYNTAX_VALUE( CLUSPROP_TYPE_RESCLASS, CLUSPROP_FORMAT_DWORD ),

We cannot do it like this in Delphi and, besides, Delphi does not allow the value of an enumeration entry to be assigned. The macro must be resolved in order to do the translation to Delphi.

We can translate the #define statement into the following Delphi function:

Function CLUSPROP_SYNTAX_VALUE (dwType: DWORD; dwFormat: DWORD): DWORD;

Begin

Result := (dwType shl 16) or dwFormat;

End;

However, function calls are not possible in constant declarations. To resolve that, we declare the constants as follows, without wrapping them together as the C macro抯 enumeration has done:

CONST

CLUSPROP_SYNTAX_ENDMARK = CLUSPROP_TYPE_ENDMARK shl 16 or CLUSPROP_FORMAT_UNKNOWN;

CLUSPROP_SYNTAX_NAME = CLUSPROP_TYPE_NAME shl 16 or CLUSPROP_FORMAT_SZ;

CLUSPROP_SYNTAX_RESCLASS = CLUSPROP_TYPE_RESCLASS shl 16 or CLUSPROP_FORMAT_DWORD;

Actually, this is a case where I would recommend translating the macro as a whole into a function, as it might be useful to make it available to applications.

Back to contents --

Back to contents

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有