Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delphi callback on the delphi application from C++ Dll help

Status
Not open for further replies.

allogallo

Technical User
Dec 20, 2010
1
IT
Hi
I'm writing an application delphi that use an external dll compiled in c++.
This Dll is a collection of function to control a USb external device.
For report a status the Dll have a function of callback.
I have the problem of receive callback because when the dll call my function i have a memory error.
Probably there is a problem on my application but i don't find it, it is the fist time that solve this problem.

I defined the dll and callback function:

var testdll:Thandle;
TesterDll_Driver_SetStatusCallback : function(TesterDll_StatusUpdateCallbackPointer: status_cb):integer; stdcall;
// other functions

I defined on type my callback procedure:

type status_cb = procedure (status_flag:integer); stdcall;

In the application load the dll:

testdll := safeLoadLibrary('TesterDll.dll');
if testdll >= 32 then { success }
begin
// other functions
TesterDll_Driver_SetStatusCallback := GetProcAddress(testdll,'TesterDll_Driver_SetStatusCallback');

My callback procedure:
procedure status_cbp(status_flag:integer) ; stdcall;
begin
//// code
end;



and finally set the callback function:

TesterDll_Driver_SetStatusCallback(status_cbp);


Someone can help me?

Paolo
 
Make sure the definitions you use match what the DLL is expecting. Other than that, you really haven't given us enough to help you.

A simple callback example.

Code:
library mydll;
   uses sysutils;
  type
    cbproc = function(num1, num2: integer): integer;

  procedure domath(var invar: integer; mycallback: cbproc);
    begin
      invar := invar * 3;
      invar := mycallback(invar, 2);
    end;


  exports
    domath  index 1;

  end.

Code:
{$APPTYPE CONSOLE}
program main; uses windows;
type
  cbproc = function(num1, num2: integer): integer;
  dmproc = procedure(var invar: integer; mycallback: cbproc);
var
  libhandle: DWord;
  dllproc: dmproc;
  invar: integer;

function add_two_numbers(num1, num2: integer): integer;
  begin
    Result := Num1 + Num2;
  end;

begin
  write('Input invar: ');
  readln(invar);
  writeln;
  writeln('DLL function multiplies by three.');
  writeln('Callback function as called from DLL adds two.');
  writeln;
  libhandle := LoadLibrary('MYDLL.DLL');
  if libhandle <> 0 then
    begin
      @DLLProc := GetProcAddress(libhandle, 'domath');
      if @DLLProc <> nil then
        begin
          dllproc(invar, @add_two_numbers);
        end;
      FreeLibrary(libhandle);
    end;
  writeln('Invar is now: ', invar);
  readln;
end.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top