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 !
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:
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.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:
Pointer-to-AnsiChar is such a common type that Delphi already has a name for it: PAnsiChar. Use the idiomatic name:
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:
Now on to
SetAllocateFunction
. It says it receives aPTR_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.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:
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.
You can set it with the function from before:
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.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
or remove the PByte declaration
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.
You only need to change the type of the pointer to PByte = ^byte;