Converting a string to pchar - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Converting a string to pchar

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

  • zibadianzibadian Posts: 6,349Member
    : 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.
    :
    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.
  • UlvertUlvert Posts: 58Member
    : 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.
    :

    I usually just typecast and have never seen any problems with that
    ex.
    MyStr : String;
    MyPChr : PChar;

    MyPChr := PChar(MyStr);

    Good luck!
    //Ulvert

  • KoppisKoppis Posts: 68Member
    : : 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.
    : :
    :
    : 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.
  • theofilustheofilus Posts: 2Member
    Thank you for your responses. I got it working now.

  • zibadianzibadian Posts: 6,349Member
    : : : 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.
    : : :
    : :
    : : 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.
  • zibadianzibadian Posts: 6,349Member
    : : 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.
    : :
    :
    : 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.
  • specijalacspecijalac Posts: 1Member
    : 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.
    :

    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 :) Cheers!
  • zibadianzibadian Posts: 6,349Member
    : : 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.
    : :
    :
    : 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 :) Cheers!
    :
    This won't work, because theofilus needs a PChar variable not a string variable.
Sign In or Register to comment.