Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

How to define a callback in a DLL?

How do I create a callback procedure in my DLL that other programs can set?  A simple example showing the basic concept is all I am looking for.

Thanks.
0
wolsen
Asked:
wolsen
1 Solution
 
BlackTigerXCommented:
callback in a DLL... let's see if this is what you need:

this is the .DLL code:

library DLLTest;

uses
  SysUtils,
  Classes;

type
  TCallbackFunction = function(X:Integer):Integer; stdcall;

  function FuncWithCallBack(CallbackFunction:TCallbackFunction):Boolean; stdcall;
  begin
    Result:=CallbackFunction(5)>0
  end;

exports
  FuncWithCallBack;
 
begin
end.

and this is the code to call it sending the function that will be called back:

type
  TCallbackFunction = function(X:Integer):Integer; stdcall;
  TFuncWithCallback = function (CallbackFunction:TCallbackFunction):Boolean; stdcall;

{$R *.dfm}

function SomeFunc(X:Integer):Integer; stdcall;
begin
  Result:=X-5
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  aHandle:THandle;
  myFunc:TFuncWithCallback;
begin
  aHandle:=LoadLibrary('DLLTest.dll');
  try
    myFunc:=GetProcAddress(aHandle, 'FuncWithCallBack');
    if (Assigned(myFunc)) then
      if (myFunc(SomeFunc)) then
        ShowMessage('good')
      else
        ShowMessage('bad');
  finally
    FreeLibrary(aHandle)
  end
end;
0
 
wolsenAuthor Commented:
Thank you that looks like just what I need.  I will try it out and see if it works.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now