Calling Windows DLLs Example

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

This is an example script for the process of calling a Windows dll

//
// Sample script for Optuma tool programming
//
const { windows consts }
  MB_ICONQUESTION = $20;
  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;
begin
  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])
  else
    result := '(could not get drive information)';
end;

// 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);
begin
    Tool.Name := 'dllcall';
    Tool.MouseClicks := 1;
    Tool.Hint := '';
    Tool.ToolType := ttDataList;
end;

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

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

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

// 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);
begin

end;

Discussion