I'm using Delphi 4 (can't afford to upgrade) and I'm making a simple program that runs an external program with paramaters provided by the user. The problem is that the paramaters provided by the user are strings but WinExec needs a PChar. Can anyone tell me how I can get WinExec to accept a string or how I can convert the string into a PChar? Or perhaps another way of executing an external program which would accept a string? I've tried ShellExec which I saw mentioned in another post here but it doesn't work. It doesn't seem to recognize it.
Comments
:
ShellExec() is defined in the ShellApi unit. As for converting a string to a pchar (null-terminated string), see the StrPCopy() function. It is well described in the helpfiles.
:
I usually just typecast and have never seen any problems with that
ex.
MyStr : String;
MyPChr : PChar;
MyPChr := PChar(MyStr);
Good luck!
//Ulvert
: :
:
: I usually just typecast and have never seen any problems with that
: ex.
: MyStr : String;
: MyPChr : PChar;
:
: MyPChr := PChar(MyStr);
:
: Good luck!
: //Ulvert
:
I typecast too. If I have understood right, PChar= Pointer to Character array's first letter. So I don't see a reason why it wouldn't work. Don't know if "MyPChr := MyStr[ 1 ];" would do the same trick, haven't tested.
: : :
: :
: : I usually just typecast and have never seen any problems with that
: : ex.
: : MyStr : String;
: : MyPChr : PChar;
: :
: : MyPChr := PChar(MyStr);
: :
: : Good luck!
: : //Ulvert
: :
:
: I typecast too. If I have understood right, PChar= Pointer to Character array's first letter. So I don't see a reason why it wouldn't work. Don't know if "MyPChr := MyStr[ 1 ];" would do the same trick, haven't tested.
:
A PChar is a pointer to a block of memory, which ends with a #0 character and is handled as a string. The code you gave, will/might not work for 2 reasons:
1: MyPChr isn't a pointer but a char. This should be:
[code]
MyPChr := @MyStr[1];
[/code]
2: MyStr might not terminate in a #0 character.
: :
:
: I usually just typecast and have never seen any problems with that
: ex.
: MyStr : String;
: MyPChr : PChar;
:
: MyPChr := PChar(MyStr);
:
: Good luck!
: //Ulvert
:
:
Type-casting will not work if the program is compiled in the $H- mode. Then you need to use the "pascal string"-"null-terminated string" conversion functions.
:
You can use copy function COPY(STRING_VAR,NO_OF_LETTERS,N_FROM_WHICH_LETTER) ;
for example p:=Copy(SWord,1,1) - p is string variable and it contains 1st letter of SWord string var
: :
:
: You can use copy function COPY(STRING_VAR,NO_OF_LETTERS,N_FROM_WHICH_LETTER) ;
: for example p:=Copy(SWord,1,1) - p is string variable and it contains 1st letter of SWord string var
:
This won't work, because theofilus needs a PChar variable not a string variable.