Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 477999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:38:51+00:00 2026-05-13T00:38:51+00:00

I’m stuck with calling an external DLL and passing a function (pointer) as parameter.

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:38:51+00:00Added an answer on May 13, 2026 at 12:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a view passing on information from a database: def serve_article(request, id): served_article

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.