TPaxInvoke Methods


TPaxInvoke.Create

Constructor of TPaxInvoke class.
constructor Create(AOwner: TComponent); override;

TPaxInvoke.Destroy

Destructor of TPaxInvoke class.
destructor Destroy; override;

TPaxInvoke.LoadAddress

Loads dll and assigns address of a dll-defind functions to Address property.
procedure LoadAddress(const DllName, ProcName: String);

Example

PaxInvoke1.This := nil;
PaxInvoke1.CallConv := _ccMSFASTCALL;
PaxInvoke1.LoadAddress('CppDll.dll', 'cube');
PaxInvoke1.ClearArguments;
PaxInvoke1.AddArgAsInteger(2);
PaxInvoke1.SetResultAsInteger;
PaxInvoke1.CallHost;
ShowMessage(IntToStr(Integer(PaxInvoke1.GetResultPtr^)));

TPaxInvoke.ClearArguments

Clears arguments.
procedure ClearArguments;

Example

function TForm1.Test(X, Y: Integer): Integer; stdcall;
begin
  result := X + Y;
end;

procedure TForm1.Button7Click(Sender: TObject);

function P(X, Y: Currency): String; stdcall;
begin
  result := CurrToStr(X + Y);
end;

begin
  PaxInvoke1.This := nil; // this is not a method
  PaxInvoke1.CallConv := _ccSTDCALL;
  PaxInvoke1.Address := @P;
  PaxInvoke1.ClearArguments;
  PaxInvoke1.AddArgAsCurrency(2.2);
  PaxInvoke1.AddArgAsCurrency(3.4);
  PaxInvoke1.SetResultAsAnsiString;
  PaxInvoke1.CallHost; // call host-defined function
  ShowMessage(String(PaxInvoke1.GetResultPtr^));
  PaxInvoke1.ClearResult;

  PaxInvoke1.This := Self; // this is a method
  PaxInvoke1.CallConv := _ccSTDCALL;
  PaxInvoke1.Address := @TForm1.Test;
  PaxInvoke1.ClearArguments;
  PaxInvoke1.AddArgAsInteger(2);
  PaxInvoke1.AddArgAsInteger(3);
  PaxInvoke1.SetResultAsInteger;
  PaxInvoke1.CallHost;
  ShowMessage(IntToStr(Integer(PaxInvoke1.GetResultPtr^)));
end;

TPaxInvoke.AddArgAsByte

Adds argument of Byte type.
procedure AddArgAsByte(value: Byte);

TPaxInvoke.AddArgAsWord

Adds argument of Word type.
procedure AddArgAsWord(value: Word);

TPaxInvoke.AddArgAsCardinal

Adds argument of Cardinal type.
procedure AddArgAsCardinal(value: Cardinal);

TPaxInvoke.AddArgAsShortInt

Adds argument of ShortInt type.
procedure AddArgAsShortInt(value: ShortInt);

TPaxInvoke.AddArgAsSmallInt

Adds argument of SmallInt type.
procedure AddArgAsSmallInt(value: SmallInt);

TPaxInvoke.AddArgAsInteger

Adds argument of Integer type.
procedure AddArgAsInteger(value: Integer);

TPaxInvoke.AddArgAsInt64

Adds argument of Int64 type.
procedure AddArgAsInt64(value: Int64);

TPaxInvoke.AddArgAsLongBool

Adds argument of LongBool type.
procedure AddArgAsLongBool(value: LongBool);

TPaxInvoke.AddArgAsWordBool

Adds argument of WordBool type.
procedure AddArgAsWordBool(value: WordBool);

TPaxInvoke.AddArgAsBoolean

Adds argument of Boolean type.
procedure AddArgAsBoolean(value: Boolean);

TPaxInvoke.AddArgAsChar

Adds argument of Char type.
procedure AddArgAsChar(value: Char);

TPaxInvoke.AddArgAsWideChar

Adds argument of WideChar type.
procedure AddArgAsWideChar(value: WideChar);

TPaxInvoke.AddArgAsDouble

Adds argument of Double type.
procedure AddArgAsDouble(value: Double);

TPaxInvoke.AddArgAsSingle

Adds argument of Single type.
procedure AddArgAsSingle(value: Single);

TPaxInvoke.AddArgAsExtended

Adds argument of Extended type.
procedure AddArgAsExtended(value: Extended);

TPaxInvoke.AddArgAsCurrency

Adds argument of Currency type.
procedure AddArgAsCurrency(value: Currency);

TPaxInvoke.AddArgAsAnsiString

Adds argument of AnsiString type.
procedure AddArgAsAnsiString(const value: AnsiString);

TPaxInvoke.AddArgAsWideString

Adds argument of WideString type.
procedure AddArgAsWideString(const value: WideString);

TPaxInvoke.AddArgAsShortString

Adds argument of ShortString type.
procedure AddArgAsShortString(const value: ShortString);

TPaxInvoke.AddArgAsPChar

Adds argument of PChar type.
procedure AddArgAsPChar(value: PChar);

TPaxInvoke.AddArgAsPWideChar

Adds argument of PWideChar type.
procedure AddArgAsPWideChar(value: PWideChar);

TPaxInvoke.AddArgAsPointer

Adds argument of Pointer type.
procedure AddArgAsPointer(value: Pointer);

TPaxInvoke.AddArgAsRecord

Adds argument of a Record type.
procedure AddArgAsRecord(var value; Size: Integer);

TPaxInvoke.AddArgAsRecordByVal

Adds argument of a record type. Argument is passed by value.
procedure AddArgAsRecordByVal(var value; Size: Integer);

TPaxInvoke.AddArgAsArray

Adds argument of array type.
procedure AddArgAsArray(var value; Size: Integer);

TPaxInvoke.AddArgAsArrayByVal

Adds argument of array type. Argument is passed by value.
procedure AddArgAsArrayByVal(var value; Size: Integer);

TPaxInvoke.AddArgAsDynArray

Adds argument of a dynamic array type.
procedure AddArgAsDynArray(var value);

TPaxInvoke.AddArgAsObject

Adds argument of a class type.
procedure AddArgAsObject(value: TObject);

TPaxInvoke.AddArgAsClassRef

Adds argument of a class reference type.
procedure AddArgAsClassRef(value: TClass);

TPaxInvoke.AddArgAsVariant

Adds argument of Variant type.
procedure AddArgAsVariant(const value: Variant);

TPaxInvoke.AddArgAsSet

Adds argument of a set type.
procedure AddArgAsSet(var value; Size: Integer);

TPaxInvoke.AddArgAsInterface

Adds argument of an interface type.
procedure AddArgAsInterface(const value: IUnknown);

TPaxInvoke.SetResultAsVoid

Sets result type as void type (for procedures).
procedure SetResultAsVoid;

TPaxInvoke.SetResultAsByte

Sets result type as Byte type.
procedure SetResultAsByte;

TPaxInvoke.SetResultAsWord

Sets result type as Word type.
procedure SetResultAsWord;

TPaxInvoke.SetResultAsCardinal

Sets result type as Cardinal type.
procedure SetResultAsCardinal;

TPaxInvoke.SetResultAsShortInt

Sets result type as ShortInt type.
procedure SetResultAsShortInt;

TPaxInvoke.SetResultAsSmallInt

Sets result type as SmallInt type.
procedure SetResultAsSmallInt;

TPaxInvoke.SetResultAsInteger

Sets result type as Integer type.
procedure SetResultAsInteger;

TPaxInvoke.SetResultAsBoolean

Sets result type as Boolean type.
procedure SetResultAsBoolean;

TPaxInvoke.SetResultAsWordBool

Sets result type as WordBool type.
procedure SetResultAsWordBool;

TPaxInvoke.SetResultAsLongBool

Sets result type as LongBool type.
procedure SetResultAsLongBool;

TPaxInvoke.SetResultAsChar

Sets result type as Char type.
procedure SetResultAsChar;

TPaxInvoke.SetResultAsWideChar

Sets result type as WideChar type.
procedure SetResultAsWideChar;

TPaxInvoke.SetResultAsDouble

Sets result type as Double type.
procedure SetResultAsDouble;

TPaxInvoke.SetResultAsSingle

Sets result type as Single type.
procedure SetResultAsSingle;

TPaxInvoke.SetResultAsExtended

Sets result type as Extended type.
procedure SetResultAsExtended;

TPaxInvoke.SetResultAsCurrency

Sets result type as Currency type.
procedure SetResultAsCurrency;

TPaxInvoke.SetResultAsAnsiString

Sets result type as AnsiString type.
procedure SetResultAsAnsiString;

TPaxInvoke.SetResultAsWideString

Sets result type as WideString type.
procedure SetResultAsWideString;

TPaxInvoke.SetResultAsShortString

Sets result type as ShortString type.
procedure SetResultAsShortString;

TPaxInvoke.SetResultAsPChar

Sets result type as PChar type.
procedure SetResultAsPChar;

TPaxInvoke.SetResultAsPWideChar

Sets result type as PWideChar type.
procedure SetResultAsPWideChar;

TPaxInvoke.SetResultAsPointer

Sets result type as a pointer type.
procedure SetResultAsPointer;

TPaxInvoke.SetResultAsArray

Sets result type as an array type.
procedure SetResultAsArray(Size: Integer);

TPaxInvoke.SetResultAsDynArray

Sets result type as a dynamic array type.
procedure SetResultAsDynArray;

TPaxInvoke.SetResultAsRecord

Sets result type as a record type.
procedure SetResultAsRecord(Size: Integer);

TPaxInvoke.SetResultAsObject

Sets result type as a class type.
procedure SetResultAsObject;

TPaxInvoke.SetResultAsClassRef

Sets result type as a class reference type.
procedure SetResultAsClassRef;

TPaxInvoke.SetResultAsInterface

Sets result type as an interface type.
procedure SetResultAsInterface;

TPaxInvoke.SetResultAsInt64

Sets result type as Int64 type.
procedure SetResultAsInt64;

TPaxInvoke.CallHost

Calls a host-defined function or method.
procedure CallHost;

TPaxInvoke.GetResultPtr

Returns pointer to a result value.
function GetResultPtr: Pointer;

You have to apply the type cast to extract a result value.

Example

type
  TCharRec = record
    X, Y: Char;
  end;

function MyHostFunc(const U, V: TCharRec): String; stdcall;
begin
  result := U.X + V.Y;
end;

var
  R: TCharRec;
  S: String;
begin
  R.X := 'a';
  R.Y := 'b';

  PaxInvoke1.Address := @ MyHostFunc;
  PaxInvoke1.This := nil; // this is not a method, but global function
  PaxInvoke1.ClearArguments;
  PaxInvoke1.AddArgAsRecord(R, SizeOf(R));
  PaxInvoke1.AddArgAsRecord(R, SizeOf(R));
  PaxInvoke1.SetResultAsAnsiString;
  PaxInvoke1.CallConv := _ccSTDCALL;
  PaxInvoke1.CallHost; // call host-defined function
  S := String(PaxInvoke1.GetResultPtr^);
  ShowMessage(S);

TPaxInvoke.ClearResult

If result type is a dynamic type such as AnsiString, Interface, Variant or dynamic array, you have to apply the ClearResult method to avoid a memory leak.
procedure ClearResult;

Example

type
  ITest = interface
  ['{E7AA427A-0F4D-4A96-A914-FAB1CA336337}']
    procedure Proc(const S: String);
  end;

  TTest = class(TInterfacedObject, ITest)
  public
    procedure Proc(const S: String);
  end;

procedure TTest.Proc(const S: String);
begin
  ShowMessage(S);
end;

function GetIntf(I: ITest): ITest;
begin
  if Assigned(I) then
    result := I
  else
    result := TTest.Create;
end;

var
  I, J: ITest;
begin
  J := TTest.Create;

  PaxInvoke1.This := nil; // this is not a method
  PaxInvoke1.CallConv := _ccREGISTER;
  PaxInvoke1.Address := @GetIntf;
  PaxInvoke1.ClearArguments;
  PaxInvoke1.AddArgAsInterface(J);
  PaxInvoke1.SetResultAsInterface;
  PaxInvoke1.CallHost; // call host-defined function
  IUnknown(I) := IUnknown(PaxInvoke1.GetResultPtr^);
  I.Proc('hello');

  PaxInvoke1.ClearResult; // IUnknown(PaxInvoke1.GetResultPtr^)._Release;
end;


Copyright © 2006-2017 Alexander Baranovsky. All rights reserved.