Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing back a String with Pchar array 1

Status
Not open for further replies.

robdon

Programmer
May 21, 2001
252
ES
Hi,

I am new to Delphi, so sorry if this is a 'newbie' question ;) ... pointers just confuse me at the moment.

I have a proc, that passes an array of pointers, and I cannot figure out how to pass back a string, and I've tried the following but it wont compile.

type
TPCharArray = array[0..12] of PChar;

function ReadFileToArray(argc: ShortInt; var argv: TPCharArray): integer; stdcall; export;
var strRob : String;
Begin
strRob := 'robdon'#0;
argv[3] := Pchar(strRob);
End;

If I change the code to
argv[3] := 'robdon'#0;
It works fine, but I want to pass back a variable, that has a string in it.

I cant change the parameters to the proc, cause its a DLL that is called by a 3rd party app.

Thanks for any help,

Rob Donovan.

ProIV Resource Centre
 

What is the error?
What version of Delphi?

I just pasted your code into a D7 app and it compiled ok for me.

 
Hi,

That was quick ;)

My mistake sorry.

I was copying code between Delphi and FreePascal, and it was in FreePascal that I got the error, not Delphi, it was 'Illegal type conversion'.

I guess I should ask somewhere else for pascal questions, sorry, but thanks for the fast respone anyway.

Rob.

ProIV Resource Centre
 
Actually what you did is not the best thing in both cases. Delphi is just being freer from what i can see. Though what you posted compiles in FreePascal 2.0.2.

Proper way to do your code is:
Code:
  var strRob : String;
Begin
  strRob := 'robdon';
  argv[3] := Pchar(strRob);
end;

The PChar type cast will take the Pascal string and convert it to PChar type, which will include the #0 null terminator.

Question: Are you using Delphi mode on FreePascal "-sD"? Perhaps that's your trouble?
 
Hi,

Thanks for the post, that got me on the right tracks.

The version of Pascal I had downloaded was a very old one, version 1.x

I upgraded to 2.0.2 and then the code compiled fine.

However, the -sD switch did not work, but I ended up using -Mdelphi

Thanks again.

Rob D.

ProIV Resource Centre
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top