国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

如何將C/C++程序轉譯成Delphi(十一)

2019-11-18 18:50:10
字體:
來源:轉載
供稿:網友

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 $03FFEnd;

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 := BEnd;

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?s 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


上一篇:如何將C/C++程序轉譯成Delphi(十二)

下一篇:如何將C/C++程序轉譯成Delphi(十)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
學習交流
熱門圖片

新聞熱點

疑難解答

圖片精選

網友關注

主站蜘蛛池模板: 宁陵县| 德安县| 加查县| 岗巴县| 盐边县| 乌审旗| 永兴县| 青岛市| 五峰| 靖边县| 凤翔县| 丰原市| 灌南县| 陕西省| 巫山县| 大港区| 海淀区| 通榆县| 靖安县| 前郭尔| 兴城市| 渝北区| 泽州县| 临海市| 巴东县| 新兴县| 峨眉山市| 宜昌市| 措美县| 青冈县| 龙川县| 家居| 南乐县| 通海县| 西畴县| 溆浦县| 墨江| 许昌县| 姚安县| 通渭县| 武义县|