[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 601
  • Last Modified:

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
0
novowi
Asked:
novowi
1 Solution
 
EpsylonCommented:
This should do it:

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

Epsylon.
0
 
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
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now