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

原创 2001年07月24日 13:34:00

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.

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

如何将C/C++程序转译成Delphi

Conversion Tutorial Draft 4 27 JANUARY, 1998by Andreas Prucha Contents Anatomy of a C Header Convers...
  • mysine
  • mysine
  • 2001年07月24日 13:22
  • 616

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

2.2. Unit dependencies Back to contents -->C and C++ use #include to include header files in another...
  • mysine
  • mysine
  • 2001年07月24日 13:26
  • 690

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

7.2. Dynamic LinkingDynamic Linking is used to link the DLL at runtime.Handling Static and Dynamic L...
  • mysine
  • mysine
  • 2001年07月24日 13:45
  • 577

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

5. Conditionals###ToDoBack to contents -->Back to contents 6. Functions6.1. BasicsLets use the follo...
  • mysine
  • mysine
  • 2001年07月24日 13:35
  • 608

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

. Anatomy of a C HeaderBack to contents -->Back to contents ## to do2. Conversion Basics Back to con...
  • mysine
  • mysine
  • 2001年07月24日 13:24
  • 618

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

1. Anatomy of a C HeaderBack to contents -->Back to contents ## to do2. Conversion Basics Back to co...
  • mysine
  • mysine
  • 2001年07月24日 13:28
  • 1092

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

Here is a real-world example from the winnt.h, which is not possible to translate to a native enumer...
  • mysine
  • mysine
  • 2001年07月24日 13:30
  • 508

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

3.6. Structures, Records3.6.1. Simple StructuresC structures are similar to records in Delphi. Struc...
  • mysine
  • mysine
  • 2001年07月24日 13:31
  • 571

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

2.3. #defines as constantsC and C++ use #defines in several ways. In a C header file #define can be ...
  • mysine
  • mysine
  • 2001年07月24日 13:28
  • 581

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

3.4. StringsIn C, as in Delphi, a string is an array of char types. Often, a string declaration is u...
  • mysine
  • mysine
  • 2001年07月24日 13:29
  • 596
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何将C/C++程序转译成Delphi(十一)
举报原因:
原因补充:

(最多只允许输入30个字)