How to translate the CC ++ program into Delphi (12)

zhaozj2021-02-17  54

5. Conditionals

### Todo

Back to Contents

6. Functions

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 pservestatus structure.

Here Is A Delphi Translation of The Example Above:

Function ControlService (HService: sc_handle;

DWControl: DWORD;

LPSERVICESTATUS: PSERVICESTATUS: BOOL; stdcall;

NOTES:

SC_Handle is defined in WinSvc.pas 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 (eg 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 Convertion, So You Have To Use Stdcall Calling Convertion in this case, too.notes

IT is Very Important to Trace Through All The #include Files for Any #defines Which May Affect The Translation. The Far Keyword Can Be ignored in Delphi.

Back to Contents

转载请注明原文地址:https://www.9cbs.com/read-30418.html

New Post(0)