• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

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


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.
1 Solution
Russell LibbySoftware Engineer, Advisory Commented:
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.

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;
rmeereAuthor Commented:
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 http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21394605.html


Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now