简体   繁体   中英

using a delphi callback function in a C dll

I'm using a C dll in a Delphi XE2 program without problem. One of the DLL function takes a function as argument.

Here is the prototype of the function:

var
 LMX_MySetOption: function(LmxHandle: LMX_HANDLE;
                             eOption: _LMX_SETTINGS;
                            callback: TCallBackProcedure): LMX_STATUS cdecl 
                                                {$IFDEF WIN32} stdcall {$ENDIF};

The original prototype in C of the function was:

LMX_STATUS LMX_SetOption(LMX_HANDLE LmxHandle, LMX_SETTINGS eOption, 
                                               const void *pSetting);

TCallBackProcedure is defined as follow:

type
 TCallBackProcedure = procedure(bla : Pointer) stdcall;

I'm calling the function this way:

LMX_MySetOption(LmxHandle, LMX_OPT_HEARTBEAT_EXIT_FUNCTION, UserExitRoutine);

The UserExitRoutine is definede as follow:

procedure UserExitRoutine(bla : Pointer) stdcall;
begin
...
end;

It's not working (access violation) I can't modify the C dll.

Many thanks for any idea!

If is a C procedure don't use stdcall use cdecl .

And you can simply declare this

 function LMX_MySetOption(LmxHandle: LMX_HANDLE;
                             eOption: _LMX_SETTINGS;
                            callback: Pointer): LMX_STATUS;cdecl;external 'yourmodule.dll'; 

procedure callback(bla:Pointer);cdecl;
begin
   //Some code
end;

LMX_MySetOption(LmxHandle, LMX_OPT_HEARTBEAT_EXIT_FUNCTION, @callback);

It should work...if it doesn't maybe you don't know the exact nr of parameters that the function has...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM