DynamicLinkLib (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example creates a dll to be called from a host application. Compile the following code as a console application:

Code

{$APPTYPE CONSOLE}

uses
  SysUtils;

function GetValue: Integer; external 'Project4.dll' name 'GetValue';

var
  Value: Integer;
begin
  try
    Value := GetValue;
    if Value = 42 then
      WriteLn('Woo hoo!  The answer to everything is ', Value)
    else
      WriteLn('Doh!');
    Readln; // So you can read the console window.
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Then go to File/New/Other and under Delphi projects, create a new Dynamic-Link Library project. Copy this code in place of the "begin end.":

Code

function GetValue: Integer;
begin
  Result := 42;
end;

exports
  GetValue;

begin
end.

Uses

See Also