我卡在了调用外部DLL并传递函数(指针)作为参数上。
最近我遇到了不同的问题,需要将一些参数传递给DLL,你帮了我。
希望有人知道如何做到这一点....
以下是需要从Delphi调用的DLL中的函数声明:
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; }
请问您能否帮我重写这个Delphi(7)代码呢?
以下是我尝试的内容,但它抛出了异常(“External exception”):
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);
非常感谢!
以下是需要从Delphi调用的DLL中的函数声明:
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; }
请问您能否帮我重写这个Delphi(7)代码呢?
以下是我尝试的内容,但它抛出了异常(“External exception”):
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);
非常感谢!