views:

1620

answers:

3

Hi everyone !

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....

Here's function declaration in DLL (cpp) that needs to be called from Delphi:


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;
}

Could you please be so kind to help me rewrite this in Delphi (7) ?

Here's what I've tried and it throws an exception ("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);   

Thank you very much !

+1  A: 

I foresee a problem with the declaration of TByte as a Dynamic Array type. A dynamic array is itself a pointer.

Either declare it as an array

type 
  PByte = ^TByte;
  TByte = Array[0..1.) of Byte;

or remove the PByte declaration

type
  TByte = Array of Byte;
  TFunc = function(var buffer:TByte; var pSize: Cardinal): Integer; cdecl

function Allocate(var buffer:TByte; Var pSize: Cardinal): Integer; cdecl;
 begin
  SetLength(buffer,pSize*2);
 end;
PA
+1 original diagnosis is correct I think, the second code dragment is still wrong I think, it still uses dyn arrays while communicating with C
Marco van de Voort
IT uses a dyn array, but it does not pass its pointer but its reference which is a pointer indeed, so it works. Both code snippets work correctly.
PA
tnx PA, it comiles, but "pSize" should be multiplied by 2 before passing to setLength, becouse it's 'var' argument.I'll check if it can allocate and deallocate memory properly.P.S. does SetLength(buffer,0) free memory or should there be GetMem and FreeMem ?I suppose deallocate function is without 'var':function Deallocate(data: TByte): Integer; cdecl;
benjamin
You can use Dynamic Array but the you need to pass a pointer to the first element instead of a pointer to the array.
Remko
A: 

As shunty already pointed out, both Allocate and Deallocate should be procedures in delphi. Furthermore, the psize parameter should be a pointer like the c declaration states. And remember you must implement both functions to get this working properly or one memory manager will allocate while the other will deallocate the same memory, so the implementation shown in your example will keep failing even when the implementation of Allocate is correct.

Glenner003
+6  A: 

If you're not sure what you're doing, always start with the most literal translation. The function prototype says it receives a pointer to a pointer to a char, so that's what you should use:

type
  PTR_Allocate = procedure(param1: ^^Char; param2: ^LongWord); cdecl;

Once you're sure it's right, then start replacing things with their more Delphi-like equivalents. If you skip this first step, you might never get it right because you'll just keep making changes to something that started out wrong.

So, are you sure the above is right? Not quite. Char in Delphi can have different meanings depending on the product version. You're using Delphi 7, but you might upgrade, so you might share this code with someone else, so you should be explicit about what size Char you want. Use AnsiChar when you need a one-byte type.

type
  PTR_Allocate = procedure(param1: ^^AnsiChar; param2: ^LongWord); cdecl;

Now we can start making it look more like Delphi. One level of pointer parameter can be replaced with a "var" or "out" directive. Do that to each parameter:

type
  PTR_Allocate = procedure(out param1: ^AnsiChar; var param2: LongWord); cdecl;

Pointer-to-AnsiChar is such a common type that Delphi already has a name for it: PAnsiChar. Use the idiomatic name:

type
  PTR_Allocate = procedure(out param1: PAnsiChar; var param2: LongWord); cdecl;

And finally, you might wish to take some liberty with the whole notion that there are characters involved at all. You're clearly allocating memory for arbitrary byte buffers, so Byte is probably a better choice than any character type. Recent Delphi versions declare a pointer-to-byte type, so use that:

type
  PTR_Allocate = procedure(out param1: PByte; var param2: LongWord); cdecl;


Now on to SetAllocateFunction. It says it receives a PTR_Allocate parameter, which is a pointer to a function. Delphi's procedure types are implicitly pointers, so the type we've declared above is already exactly right for the Delphi equivalent. Don't pass it by reference with an extra "var" directive or you will have the problems you've seen, even before your program attempts to allocate any memory. This is something the other answers have overlooked.

procedure SetAllocateFunction(param: PTR_Allocate); cdecl;

Don't add an underscore to the start of the name, either, unless you want to make it inconvenient to call in your own code. If it's exported from the DLL using a different name, then use a "name" clause when you write the function's implementation:

procedure SetAllocateFunction; extern 'foo.dll' name '_SetAllocateFunction';


Finally, how to implement the allocation function. Start with something that matches the signature for PTR_Allocate, and then go ahead and implement it using as literal a translation as possible from the original C++ code.

procedure Allocate(out pbuffer: PByte; var psize: LongWord; cdecl;
begin
  psize := psize * 2;
  GetMem(pbuffer, psize);
end;

You can set it with the function from before:

SetAllocateFunction(Allocate);

Notice I didn't need a separate variable and I haven't used the @ operator. If you need to use the @ operator to mention a function pointer, in most cases, you're doing it wrong. You usually don't need it. Using that operator can hide errors in your program, such as signature mismatches, because the default setting is for the @ operator to be untyped. Using it removes the type from the function pointer, and untyped pointers are compatible with everything in Delphi, so they fit with any other function-pointer type, including the ones with wrong signatures.

Only use @ on a function pointer when the compiler has already indicated that it has tried to call the function, such as by mentioning how you don't have enough parameters or by mentioning the function's return type.

Rob Kennedy
Great explanation in every way ! It seems to work.Thank you !
benjamin
+1: this is a very detailed answer, explaining every step. Big thumbs up!
Remko