Using C++ DLL's and null-terminated strings

Posted on 2005-04-19
Last Modified: 2010-04-05

I am trying to interface to a dll that was written in C++ using Delphi 2005 Win32.

All of my interfaces work fine except when a null-terminated string / array of char (C++ : char *returnstring) is used as an var / out parameter.

I essentially have to pass it a pointer / variable to return results in.

I have tried passing a PChar variable, but as soon as the result is returned from the dll i get access violations when trying to access it. This is the case for any assignment to/from it and also when applyting any function to it eg StrLength. I then wrote it to manually assign memory to the variable with GetMem with the same result. I think it might have something to do with the dll becuase the av address and the address it is trying to read is the same. The developer of the dll assured me that he does no memory assignment, allocation or unallocation.

I can access the result with an array of Char, but this variable type is limited in size. I need the variable to go to a maximum of 30Mb and an array of Char starts to give me trouble if it gets to about 750K. I cannot pass a dynamic array and set the size accordingly because the dll won't accept it. So I'm stuck with static arrays.

I'm looking for a way to interface with this out parameter (what variable / pointer type to use) or a way that I can assign 30Mb to a static array of Char.

I've got a deadline to make and this is very urgent so I will pay 1000 points for the solution. I'll register a new question to award the other 500 points as soon as I have a solution.
Question by:rmeere
    LVL 26

    Accepted Solution

    If no allocation is done, then this really isn't a var/out parameter, it should be defined as:

    function funcname(param: PChar; etc....)

    Also, do you pass the buffer size to the dll as another param (common technique). If not, how does the dll determine how large the buffer is? It would help to see the dll decl as well as any documentation in order to assist further.

    LVL 11

    Expert Comment

    Please give the declaration of the C function.
    The usual way is to declare a buffer to hand down.

    procedure DLLProc(Param1: PChar);

      Buffer: array [0..SOMEMAX] of Char;
      S: string;
      // alternatively
      // now assign to a string ta have Delphi take care of it
      S := Buffer;
    LVL 1

    Author Comment

    thanks rllibby. I figured out the problem a couple of minutes before your solution came thru.

    the documentation specifically stated that it was an out parameter and it was only a pointer. I am awarding you the points for this because you basically solved it in the first sentence of your comment.

    The other points are on


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now