ReNamer:Pascal Script:Import functions

From den4b Wiki
Revision as of 16:02, 8 February 2017 by Den4b (talk | contribs) (Text replacement - "</source>" to "</syntaxhighlight>")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This article describes how to declare and call external functions from within Pascal Script.

You would need to know few bits of information about the external function that you are trying to use. You can usually find all this information in the documentation to the libraries where function is stored. But first, we will see the syntax for external declarations in Pascal Script.

function <MY-NAME>(<PARAMETERS>):<RETURN> external '<NAME>@<DLL> <CALL>';

And similar declaration for the procedure (function without return):

procedure <MY-NAME>(<PARAMETERS>) external '<NAME>@<DLL> <CALL>';

Now, what do all those tags in brackets mean:

<MY-NAME> Name which will be used in Pascal Script to refer to the function. This you can come with your-self.
<PARAMETERS> List of parameters with their types, taken from function specification and converted into syntax of Pascal Script.
<NAME> Original name of the exported function.
<DLL> Library name where the function is stored. Usually file with *.DLL extension.
<CALL> Calling convention used by the function. This is a tricky one. Calling conventions can differ based on programming language, API specifications, developer's choice, etc. Here is a list of calling conventions with some advices:
  • register - default, used by Delphi / Pascal Script.
  • stdcall - standard calling convention for Windows API.
  • cdecl - used by shared libraries written in C or C++.
  • pascal - maintained for backward compatibility.

Examples

Below is a simple example of 2 imported functions from Windows API:

function MyGetTickCount: Longint;
  external 'GetTickCount@kernel32.dll stdcall';

procedure MySleep(Milliseconds: Cardinal);
  external 'Sleep@kernel32.dll stdcall';

begin
  FileName := IntToStr(MyGetTickCount);
  MySleep(10);
end.

The script will assign a current timestamp to each filename, with a small pause after each processed file.