3
votes

I'm new in delphi, my program developed in delphi working with a dll developed in C++, I need working with pointer functions that throw exceptions of Access Violation address and after many test I don't know how resolve It.

this is defintion of the pointer function in delphi that translate since header c++

type
  TMICRCallback   = function: Integer of Object;  stdcall;
  TStatusCallback = function(dwParam: Cardinal): Integer of Object; stdcall;

  type
   TBiMICRSetReadBackFunction =
      function(const nHande:        Integer;
               pMicrCB:             TMICRCallback;
               var pReadBuffSize:    Byte;
               var readCharBuff:     Byte;
               var pStatus:          Byte;
               var pDetail:          Byte
      ): Integer; stdcall;
var
   BiMICRSetReadBackFunction: TBiMICRSetReadBackFunction;

type
   TBiMICRSetReadBackFunction =
      function(const nHande:        Integer;
               pMicrCB:             TMICRCallback;
               var pReadBuffSize:    Byte;
               var readCharBuff:     Byte;
               var pStatus:          Byte;
               var pDetail:          Byte
      ): Integer; stdcall;
var
   BiMICRSetReadBackFunction: TBiMICRSetReadBackFunction;

this is a code that call the pointer functions

type
  function CBMICRRead : Integer; stdcall;
  function CBMICRStatus(dwStatus: LongWord) : Integer;  stdcall;


  Respuesta      : TMICRCallback;
  Estado         : TStatusCallback;

  BiSetStatusBackFunction(m_hApi, Estado);

 BiMICRSetReadBackFunction (m_hApi,
                                    Respuesta,
                                    m_MICRReadBuffSize,
                                    m_MICRReadBuff[0],
                                    m_MICRReadStatus,
                                    m_MICRReadStDetail); 

This is the C++ side of the interface:

typedef int (CALLBACK* MICRCallback)(void);
typedef int (CALLBACK* StatusCallback)(DWORD);


int WINAPI BiSetStatusBackFunction(int  nHandle,
                               int (CALLBACK *pStatusCB)(DWORD dwStatus));

int WINAPI BiMICRSetReadBackFunction(int    nHandle, 
                                     int    (CALLBACK *pMicrCB)(void),
                                     LPBYTE pReadBuffSize,   
                                     LPBYTE readCharBuff,    
                                     LPBYTE pStatus,         
                                     LPBYTE pDetail);        
1
Only use regular functions as callbacks (not "of Object") (search for "implicit self parameter", e.g. stackoverflow.com/questions/7706637/…). - Sertac Akyuz
It would help if you showed the matching portions of your C++ interface - David Heffernan
Here is the official documentation about the implicit parameter of a class method. - Sertac Akyuz

1 Answers

4
votes

You must avoid Object as passing parameters from/to DLL function call result.

TMICRCallback   = function: Integer;  stdcall;
TStatusCallback = function(dwParam: Cardinal): Integer; stdcall;