title: (Delphi) Call DLL with function pointer parameter
body: <p>I'm stuck with calling an external DLL and passing a function (pointer) as parameter.
  I've recently had different problem of passing some arguments to DLL and you helped.
  Hope, someone know how to do this as well....</p>
  
  <p>Here's function declaration in DLL (cpp) that needs to be called from Delphi:</p>
  
  <pre>
  <code>
  typedef void (*PTR_Allocate)(char**, unsigned long*);
  typedef void (*PTR_Deallocate)(char*);
  
  extern "C" export_dll_function void SetAllocateFunction(PTR_Allocate);
  extern "C" export_dll_function void SetDeallocateFunction(PTR_Deallocate);
  
  void Allocate(char** pbuffer, unsigned long* psize)
  {
      *psize = *psize * 2;
      *pbuffer = new char[*psize];
  }
  
  void Deallocate(char* buffer)
  {
      delete[] buffer;
  }
  </code>
  </pre>
  
  <p>Could you please be so kind to help me rewrite this in Delphi (7) ?</p>
  
  <p>Here's what I've tried and it throws an exception ("External exception"):</p>
  
  <pre>
  <code>
  type
     PByte = ^TByte;
     TByte = array of byte;
     TFunc = function(var pbuffer: PByte; var psize: Cardinal): integer; cdecl;
     Procedure _SetAllocateFunction(var f: TFunc); cdecl;
  
  implementation
  
  function Allocate(var pbuffer: PByte; var psize: Cardinal): Integer; cdecl;
  begin
    psize := psize * 2;
    GetMem(pbuffer, psize);
  end;
  
     var Func: TFunc;
     Func := @Allocate;
     _SetAllocateFunction(Func);   
  </code>
  </pre>
  
  <p>Thank you very much !</p>