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:

var
  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
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23734423.html

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
3221225477 STATUS_ACCESS_VIOLATION

But when I compile
CreateProcessWithToken (newToken, 0, 'C:\Borland\Delphi\Projects\P1\project1.exe', nil, 0, nil, nil, si, pi);
it works correctly.
LVL 1
Bart_MichaelAsked:
Who is Participating?
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.

2266180Commented:
you first typecast string to widestring and then widestring to pwidechar :)
pw:=pwidechar(widestring(s));

Open in new window

0

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
ziolkoCommented:
or simply define:
s: WideString;
then you have to do only one typecast
pw := PWideChar(s);

ziolko.
0
JosephGloszCommented:
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

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

var
  Filename: TFilename;

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


 



0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

ziolkoCommented:
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

ziolko.
0
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
0
2266180Commented:
open a new question and giv emore info (specifically how you declared your parameters and how you filled si.
0
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
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.