Convert string to PWideChar

I need to convert string variable to PWideChar (Delphi 5). I tried few examples found on the net, but they do not work. Sample code I need is:

  S: string;
  PW: PWideChar;
S := Application.ExeName;
convert S --> PW
CreateProcessWithToken (newToken, 0, PW, nil, 0, nil, nil, si, pi);

where CreateProcessWithToken is described in my other question

When I convert S to PW and call CreateProcessWithToken i get error codes (GetLastError) like:
1783  - The stub received bad data
1734 - The array bounds are invalid

But when I compile
CreateProcessWithToken (newToken, 0, 'C:\Borland\Delphi\Projects\P1\project1.exe', nil, 0, nil, nil, si, pi);
it works correctly.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you first typecast string to widestring and then widestring to pwidechar :)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
or simply define:
s: WideString;
then you have to do only one typecast
pw := PWideChar(s);

With extended syntax enabled ({$X+}), you can assign a string constant to a statically allocated zero-based character array. (Dynamic arrays won't work for this purpose.) If you initialize an array constant with a string that is shorter than the declared length of the array, the remaining characters are set to #0.

so, an easy conversion would to do the following

  TFileName = array[0..259] of WideChar;

  Filename: TFilename;

Filename := Application.Exename;  
CreateProcessWithToken (newToken, 0, Filename, nil, 0, nil, nil, si, pi);


OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

JosephGlosz first of all extended syntax is enabled by default and it's not recommended to disable it, second thing is that CreateProcessWithToken requires null terminated string so before putting there any values it's better to use FillChar(Filename, size_of_array, 0), third thing is that there is predefined constant called MAX_PATH so I would suggest declaration array[0..MAX_PATH] of WideChar and finally your code gives "incompatybile types" compiler error

Bart_MichaelAuthor Commented:
Thank You all for posting. Of course your solutions work, but I have another bug in CreateProcessWithToken parameters, because it behaves strange.

Best regards
open a new question and giv emore info (specifically how you declared your parameters and how you filled si.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.