Calling Windows DLLs Example

Author: Optuma Team Last updated: Mar 2, 2021 20:04

// Sample script for Optuma tool programming
const { windows consts }
  MB_YESNO = $4;
  IDYES = 6;
  VK_SHIFT = $10;

{ declaration of external functions }

function MessageBox(hwnd: pointer; text, caption: AnsiString; msgtype: integer): integer;
  stdcall; external 'User32.dll' name 'MessageBoxA';

function GetDiskFreeSpace(root: AnsiString; var secPerCluster, bytesPerCluster, freeClusters,
  totalClusters: integer): boolean; stdcall; external 'Kernel32.dll' name 'GetDiskFreeSpaceA';

function FindWindow(className, windowName: AnsiString): integer;
  stdcall; external 'User32.dll' name 'FindWindowA';

function GetKeyState(virtKey: integer): smallint; stdcall; external 'User32.dll';

function FreeSpaceInfo(drive: String): string;
var a, b, c, d: word;
  if GetDiskFreeSpace(drive, a, b, c, d) then
    result := Format('%d sections per cluster; %d bytes per cluster; %d free clusters; %d total clusters', [a, b, c, d])
    result := '(could not get drive information)';

// DefineTool is where the settings for the Tool are defined
// This procedure is called once when the tool is loaded
// Normally this procedure does not need to be changed
procedure DefineTool(Tool : TTool);
    Tool.Name := 'dllcall';
    Tool.MouseClicks := 1;
    Tool.Hint := '';
    Tool.ToolType := ttDataList;

// Init is called to initialise the tool
// This procedure is called once when the tool is added to a chart
procedure Init(Tool : TTool);
  if MessageBox(nil, 'Are you sure?', 'Confirmation', MB_ICONQUESTION + MB_YESNO) = IDYES then
    showmessage('MessageBox: user answered YES')
    showmessage('MessageBox: user answered NO');

  showmessage('Drive C: free space: ' + FreeSpaceInfo('C:'));

  if GetKeyState(VK_SHIFT) < 0 then
    showmessage('Shift is pressed')
    showmessage('Shift is not pressed');

// Process is called to calculate and drawn the tool on screen
// This procedure is called when new data is received or loaded and
// when a selection point is moved by the user
procedure Process(Tool : TTool; ProcessStart : Integer; ProcessEnd : Integer; DataIn : TDataList);