Jump to content
Cartman.

Dynamically Call Functions - Call Functions By Name

Recommended Posts

Posted

There are actually a few ways of going about calling your functions dynamically.

Let us look at one (needing the usage of Classes.pas - adds extra size to the stub/output)


program prjCallFuncDynm;

uses
Windows, Classes;

Type
TDCFunction = function: string of object;
TDClass = Class (TPersistent)
function xMain: string;
End;

function TDClass.xMain;
begin
Result := 'Gogaie valorosu?';
end;

function xTDC(xTMN:String;xClass:String): string;
var m: TMethod; xTClass:TPersistentClass;
begin
xTClass := GetClass(xClass);
m.Code := xTClass.MethodAddress(xTMN);
m.Data := pointer(xTClass);
Result := TDCFunction(m);
end;

begin
RegisterClasses([TDClass]);
MessageBox(0,PWideChar(xTDC('xMain','TDClass')),'OpenSC',0);
end.

And a different method not using Classes.pas would be as follows:


program prjCallFuncDynm;

uses
Windows;

Var
pMain:function:String;stdcall;

function xMain:string;stdcall;
begin
Result := 'Gogaie valorosu?';
end;

Exports xMain;

begin
pMain := GetProcAddress(0,'xMain');
MessageBox(0,PWideChar(pMain),'OpenSC',0);
end.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...