Home > Back-end >  VS callback Delphi PC callback function
VS callback Delphi PC callback function

Time:09-30

Written in VS dynamic library, written by Delphi PC, other interface calls are no problem, the dynamic library compiled respectively and the upper machine is no problem, but the dynamic library callback function, the callback the upper machine pop-up "access violation at addressXXXX, write of addressXXXX"
PC and operation is in the dynamic library:
PC:
The callback function declarations TOnResponse=procedure (sCmdBack: PChar) of the object; Callback function declarations and definitions,
The callback function interface function function RegisterHWCallback (aCallback: TOnResponse) : Integer; Stdcall; External 'Hardware. DLL';
The dynamic library:
The callback function declarations typedef void (* TOnResponse) (PCHAR sCmdBack);
The callback function interface function of extern "C" _declspec (dllexport) int _stdcall RegisterHWCallback (TOnResponse aCallback)

Once in the interface function, even if it is empty, jump out of the interface function will pop up "access violation at addressXXXX, write of addressXXXX"
What is solution of the problem, written for the dynamic library before vs upper machine, is no problem, but the link on the other side of the Delphi PC will pop up after the problem,

CodePudding user response:

TOnResponse=procedure (sCmdBack: PChar) of the object;
There is of the object! With dynamic library statement corresponds not top?

CodePudding user response:

The change of dynamic libraries, dynamic database statement should be how to write

CodePudding user response:

TOnResponse=procedure (sCmdBack: PChar) of the object;
To:
TOnResponse=procedure (sCmdBack: PChar); Cdecl;

CodePudding user response:

I'm sorry, is to change the dynamic library, do not change PC, dynamic library is written VS,

CodePudding user response:

I'm sorry, can't do that, unless you let Microsoft changed the compiler,
Your Delphi program, TOnResponse=procedure (sCmdBack: PChar) of the object; It declares a method pointer, because you don't have pointed out that calling convention, the default is Borland register agreement, so for the callback function parameter passed in is right: eax=self (this) in c + + and edx=sCmdBack, function do stack balance; And your VC code defined in the typedef void (* TOnResponse) (PCHAR sCmdBack); Use cdecl calling convention, sCmdBack use stack, stack the caller do balance,
You can see both on? The monkey is eating Fried dough twist - ManNing,

If you must call of Delphi method in the C code, then you cannot use a function pointer call directly, want to write a call transformation function, such as:
Void CallBackThunk (TOnResponse & amp; P, PCHAR sCmdBack);
Pointer with embedded in the internal assembly simulation method calls, but C code does not know the called method is an instance of the pointer, which is a serious hidden trouble, so it is better to define the callback function of Delphi change,

CodePudding user response:

If the PC and dynamic libraries such as stdcall calling conventions, Delphi program still states method Pointers
TOnResponse=procedure (sCmdBack: PChar) of the object; The corresponding VC dynamic library how to declare to correspond to the method Pointers

CodePudding user response:

refer to 6th floor xihuanbilan response:
if PC and dynamic libraries such as stdcall calling conventions, Delphi program still states method Pointers
TOnResponse=procedure (sCmdBack: PChar) of the object; The corresponding VC dynamic library how to declare to correspond to the method Pointers?


So parameter problem solved, and the main problem is the C code does not know the called method instance pointer, you have to modify RegisterHWCallback pass it to C code:
Int _stdcall RegisterHWCallback (TOnResponse aCallback, void * Sender);
The Sender you have to save up, Delphi callback function called when it's used:
Typedef void (TOnResponse __stdcall *) (void * Sender, PCHAR sCmdBack);
  • Related