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

  • Home
  • SEARCH
  • 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 3440962
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:29:41+00:00 2026-05-18T08:29:41+00:00

I have a DLL in which I have a function that returns a pchar.

  • 0

I have a DLL in which I have a function that returns a pchar. (as to avoid having to use borlndmm) What I was doing originally was casting a string as a pchar and returning that

Result := pChar(SomeFuncThatReturnsString)

But I was getting expected results 90% of the time and the other times I would get back nothing.

I then got to thinking that I needed to allocate the memory for the pchar and that doing it my original way was having a pchar point to memory that was not always going to be what was there when the function was called originally. So I now have this

Result := StrAlloc(128);
Strcopy(Result,PAnsiChar(Hash(Hash(Code,1,128),2,128)));

But this leaves me with having to clean up the allocated memory on the programs end which I do with

StrDispose(Pstr);    

So the $64 question is: Do I have to allocate memory when returning a PChar from a function inside a DLL or can I just cast it to a PChar?

  • 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-18T08:29:42+00:00Added an answer on May 18, 2026 at 8:29 am

    The typical approach to this issue is to have the app allocate the memory and then pass it to the DLL to fill in (even better if the DLL allows the app to query how much memory it needs to allocate so it does not have to over-allocate memory):

    function GetAString(Buffer: PChar; BufLen: Integer): Integer; stdcall;
    var
      S: String;
    begin
      S := SomeFuncThatReturnsString;
      Result := Min(BufLen, Length(S));
      if (Buffer <> nil) and (Result > 0) then
        Move(S[1], Buffer^, Result * SizeOf(Char));
    end;
    

    This allows the app to decide when and how to allocate the memory (stack versus heap, reusing memory blocks, etc):

    var
      S: String;
    begin
      SetLength(S, 256);
      SetLength(S, GetAString(PChar(S), 256));
      ...
    end;
    
    var
      S: String;
    begin
      SetLength(S, GetAString(nil, 0));
      if Length(S) > 0 then GetAString(PChar(S), Length(S));
      ...
    end;
    
    var
      S: array[0..255] of Char;
      Len: Integer;
    begin
      Len := GetAString(S, 256);
      ...
    end;
    

    If this is not an option for you, then you need to have the DLL allocate the memory, return it to the app for use, and then have the DLL export an additional function that the app can call when it is done to pass the pointer back to the DLL for freeing:

    function GetAString: PChar; stdcall;
    var
      S: String;
    begin
      S := SomeFuncThatReturnsString;
      if S <> '' then
      begin
        Result := StrAlloc(Length(S)+1);
        StrPCopy(Result, S);
      end else
        Result := nil;
    end;
    
    procedure FreeAString(AStr: PChar); stdcall;
    begin
      StrDispose(AStr);
    end;
    
    var
      S: PChar;
    begin
      S := GetAString;
      if S <> nil then
      try
        ...
      finally
        FreeAString(S);
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a native VC++ project that uses a dll (which is not in
I have a DLL which provided a decoding function, as follows: function MyDecode (Source:
We have inherited VB6 dll which we need to make changes to. We have
I have a .NET dll which needs to read it's config settings from it's
I have created a .NET DLL which makes some methods COM visible. One method
I have a MS AJAX toolkit DLL which I want to add to the
I have a few C# .dll projects which are common to many applications. Currently,
We have a project with many dll files which get loaded when the application
I have two projects, the DLL project which has all my logic and data
I am debugging a third-party DLL for which I don't have the source code.

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.