?
Solved

Convert string to PWideChar

Posted on 2008-09-20
6
Medium Priority
?
9,303 Views
Last Modified: 2008-09-24
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.
0
Comment
Question by:Bart_Michael
6 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 500 total points
ID: 22529483
you first typecast string to widestring and then widestring to pwidechar :)
pw:=pwidechar(widestring(s));

Open in new window

0
 
LVL 21

Assisted Solution

by:ziolko
ziolko earned 500 total points
ID: 22529495
or simply define:
s: WideString;
then you have to do only one typecast
pw := PWideChar(s);

ziolko.
0
 
LVL 6

Expert Comment

by:JosephGlosz
ID: 22530553
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 21

Expert Comment

by:ziolko
ID: 22531772
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
 
LVL 1

Author Comment

by:Bart_Michael
ID: 22558775
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
 
LVL 28

Expert Comment

by:2266180
ID: 22558825
open a new question and giv emore info (specifically how you declared your parameters and how you filled si.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question