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

###ToDo

Back to contents

6.1. Basics

Let's use the following C-declaration as an example of declaring a function:

WINADVAPI

BOOL

WINAPI

ControlService(

SC_HANDLE           hService,

DWORD               dwControl,

LPSERVICE_STATUS    lpServiceStatus

);

[Options] ReturnValueType [Options] FunctionName ( [ParameterList] )

 ReturnValueType: Specifies the type of the return value of the function. In the example above the type of the return value is BOOL. If the function does not return a value the keyword VOID is used and the translation in Delphi is a Procedure. Please note, that the type identifier can also be "hidden" in an identifier defined using #define. Options: Options can be a token specifying the calling convention and/or other keywords telling the compiler how to handle the function in question. The most important item is the calling convention. In the Windows-API header files, the calling convention is usually "hidden" in an identifier declared using #define, so you have to look what is defined. The example above uses the WINAPI symbol which is declared as __stdcall. This is necessary for the translation, too. Every Options item must be assessed for impact and discarded or implemented. FunctionName: Specifies the name of the function ParameterList: List of parameters passed to the function separated with ",". Parameters are declared via a type identifier or via a type identifier + parameter name combination.

Back to contents

6.2. Calling Conventions

Referring to the example:

WINADVAPI

BOOL

WINAPI

ControlService(

SC_HANDLE           hService,

DWORD               dwControl,

LPSERVICE_STATUS    lpServiceStatus

);

The type of the return value is BOOL. Now let's have a look at the definition of WINAPI. WINAPI is defined in windef.h the following way:

#define WINAPI __stdcall

The __stdcall keyword tells the C-compiler to use the standard-call calling convention for the function, so we have to declare the function using stdcall, too, since the default calling convention is Register in Delphi.

The function accepts three parameters. The first is a parameter with type SC_HANDLE, the second is a DWORD and the third is a pointer to a PServiceStatus structure.

Here is a Delphi translation of the example above:

Function ControlService (hService: SC_Handle;

dwControl: DWORD;

lpServiceStatus: PServiceStatus): Bool; stdcall;

Notes:

1. SC_Handle is defined in WinSvc.pas.
2. The C variable hService appears to conflict somewhat with the Delphi convention that employs H as the initial letter for a handle type. Whilst it seems desirable to use an alternative (hndService:HControlService, for example), Delphi will accept duplicate names in a parameter list (e.g. hwnd: HWND). Since Borland does it and it works, I would recommend staying with that convention.

Let's have a look at another function declaration:

ULONG (FAR PASCAL MAPISENDDOCUMENTS)(

ULONG ulUIParam,

LPSTR lpszDelimChar,

LPSTR lpszFilePaths,

LPSTR lpszFileNames,

ULONG ulReserved

);

Well, this declaration contains a trap. The keyword PASCAL has been used to specify the Pascal-calling convention, which was usually used in the 16bit-Windows. But not under Win32. Look at the following line in the windef.h file:

#define PASCAL __stdcall

The windef.h header declares PASCAL as __stdcall, which specifes the stdcall calling convention, so you have to use stdcall calling convention in this case, too.

Notes

1. It is very important to trace through all the #include files for any #defines which may affect the translation.
2. The FAR keyword can be ignored in Delphi.

Back to contents

• 本文已收录于以下专栏：

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

Conversion Tutorial Draft 4 27 JANUARY, 1998by Andreas Prucha Contents Anatomy of a C Header Convers...
• 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
• 2001年07月24日 13:26
• 690

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

• mysine
• 2001年07月24日 13:45
• 577

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

3.6. Structures, Records3.6.1. Simple StructuresC structures are similar to records in Delphi. Struc...
• 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
• 2001年07月24日 13:28
• 581

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

. Anatomy of a C HeaderBack to contents -->Back to contents ## to do2. Conversion Basics Back to con...
• 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
• 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
• 2001年07月24日 13:30
• 508

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

4. MacrosIn C its possible to define macros. Macros are not available in Delphi, so functions must b...
• mysine
• 2001年07月24日 13:34
• 599

## 如何将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
• 2001年07月24日 13:29
• 596

举报原因： 您举报文章：如何将C/C++程序转译成Delphi(十二) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)