String to pWideChar ?

I am in utter confusion.
How can I convert a string into a pWideChar ?

The StringToWideChar function does not return anything. What am I doing wrong ?
var
str:string;
pWC:pWideChar;
res:DWORD;

begin
SetLength(str,20);
pWC:=AllocMem(20);
str:='hello';
SetLength(str,length(str));
res:=StringToWideChar(str,pWC,length(str);

end;

By this point, pWC should contain str.

It doesn't !

Any ideas ?

Thanks,
Oliver
novowiAsked:
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.

EpsylonCommented:
This should do it:

pWC:=AllocMem(20);
str:='hello';
StringToWideChar(str,pWC,length(str)+1);
ShowMessage(pWC);

Epsylon.
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
EpsylonCommented:
This is better actually:

pWC:=AllocMem(20);
str:='hello';
StringToWideChar(str,pWC,20);
ShowMessage(pWC);
0
RadlerCommented:
Hi Epsylon,

There are a cause to avoid StrAlloc ?

T++, Radler.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

LischkeCommented:
I think the better way to go were to use WideString until the PWideChar is actually needed. Then you simply would write PWideChar(MyWideString). This frees you from keeping an eye on memory allocation/deallocation. Converting between string and wide string can simply be done by using an assignment:

var
  Str: String;
  WStr: WideString;

  Str := 'Hello';
  WStr := Str; // this converts implicitely to wide string

Then when you need the PWideChar version you can write:

  MessageBoxW(Handle, PWideChar(WStr), ...);

You never need to consider when to allocate or free the wide string.

Ciao, Mike
0
EpsylonCommented:
StrAlloc will also work fine, doesn't really matter here...
0
novowiAuthor Commented:
You got it man,

It was the +1.

Thanks a lot,

Oliver
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.