Help! - How to convert unicode string to PChar in D2010?

Hi,

The typecasting used with non unicode strings 'PChar(s)' doesn't work more 
correct with unicode strings in D2010. How can I do this?

Thanks,
John
0
John
5/8/2010 11:14:51 AM
embarcadero.delphi.general 4258 articles. 0 followers. Follow

11 Replies
975 Views

Similar Articles

[PageSpeed] 28

> The typecasting used with non unicode strings 'PChar(s)' doesn't work
> more correct with unicode strings in D2010. How can I do this?

If your question is 'how do I cast from a string to a PChar', then the
answer is 'in exactly the same way as you did in D2-D2007'.
Alternatively, if your question is 'how do I cast from an AnsiString to
a PChar', then it depends on your understanding of the PChar type. In
short, a Delphi 'Char' is a generic type that maps to either AnsiChar
or WideChar; in contrast, the C 'char' type (at least with any
actively-used compiler) is always single byte (in Delphi-speak, it
always maps to AnsiChar).

As a result, if you're calling a C DLL function that expects a char *,
then you should just cast your AnsiString to PAnsiChar (forget about
PChar). Alternatively, if you are calling (say) a Windows API function
using the standard mapping, then you'll need to cast to string first,
then onto PChar. FWIW, this will produce code that cross-compiles
between D2-2007 and D2009+:

{code}
var
  S: AnsiString;
begin
  SetWindowText(Handle, PChar(string(S)));
{code}

If you're calling a DLL that supports both 'Ansi' and wide strings
though, why are you using AnsiString internally? Of course, I may have
still not understood your question though.
0
Chris
5/8/2010 12:04:53 PM
Thanks, Chris. I have to explain:

I use in Delphi normally string type. But I have to call a DLL fucntion 
which needs PChar as parameter. Thus I have until unicode called like 
MyFunc(PChar(s)); but now the function evaluates the passed value as s[1] 
only loosing the rest, e.g. if the passed string is 'abcde' the function 
gets only 'a'. I think this is due to the doublebyte unicode chars where the 
2nd byte seems to contain #0 reporting thus end of sting. I didn't found 
until now the way to convert it.

So far I have read in the D2010 help, now typecasting PChar(s) returns 
corrupted values - as I experience it too.

John
0
John
5/8/2010 12:19:56 PM
> {quote:title=John Schmidt wrote:}{quote}
> Thanks, Chris. I have to explain:
> 
> I use in Delphi normally string type. But I have to call a DLL fucntion 
> which needs PChar as parameter. Thus I have until unicode called like 
> MyFunc(PChar(s)); but now the function evaluates the passed value as s[1] 
> only loosing the rest, e.g. if the passed string is 'abcde' the function 
> gets only 'a'. I think this is due to the doublebyte unicode chars where the 
> 2nd byte seems to contain #0 reporting thus end of sting. I didn't found 
> until now the way to convert it.
> 
> So far I have read in the D2010 help, now typecasting PChar(s) returns 
> corrupted values - as I experience it too.
> 
Iif you have an DLL function that expects PChar you need to remember that
PChar is now unicode, and you need PAnsiChar. And your function really expects
single byte chars. So you should use something like this:

{code: Delphi}

var 
  s: string;
  a: AnsiString;

..... do something with s

a := AnsiString(s); 

DLLFunction(PAnsiChar(a));

{code}

Dalija Prasnikar
0
Dalija
5/8/2010 1:03:58 PM
"John Schmidt" <schmidtjohn@yahoo.com> wrote in message 
news:240640@forums.embarcadero.com...
> Thanks, Chris. I have to explain:
>
> I use in Delphi normally string type. But I have to call a DLL fucntion
> which needs PChar as parameter. Thus I have until unicode called like
> MyFunc(PChar(s)); but now the function evaluates the passed value as s[1]
> only loosing the rest, e.g. if the passed string is 'abcde' the function
> gets only 'a'. I think this is due to the doublebyte unicode chars where 
> the
> 2nd byte seems to contain #0 reporting thus end of sting. I didn't found
> until now the way to convert it.


In this case, where the target requires an *ansi*char, cast it as such:

    PAnsiChar(AnsiString(s))



-- 
Wayne Niddery (TeamB)
0
Wayne
5/8/2010 1:09:22 PM
> DLLFunction(PAnsiChar(a));

I have tried it. The function refutes PAnsiChar telling it's not PChar.

John
0
John
5/8/2010 1:21:58 PM
> In this case, where the target requires an *ansi*char, cast it as such:
>
>    PAnsiChar(AnsiString(s))

Wayne, I have tried it but ir doesn't compile: the DLL function moans PChar 
<> PAnsiChar.

John
0
John
5/8/2010 2:18:33 PM
John Schmidt wrote:

> > In this case, where the target requires an *ansi*char, cast it as
> > such:
> > 
> >    PAnsiChar(AnsiString(s))
> 
> Wayne, I have tried it but ir doesn't compile: the DLL function moans
> PChar <> PAnsiChar.

You have to update the interface unit for the DLL. If it was written
for pre-Delphi 2009 you have to replace every occurrance of PChar with
PAnsiChar in the interface unit. Otherwise the unit will no longer
correctly describe the functions exported by the DLL.

-- 
Peter Below (TeamB)  
Don't be a vampire (http://slash7.com/pages/vampires), 
use the newsgroup archives :
http://codenewsfast.com
http://groups.google.com
0
Peter
5/8/2010 3:42:27 PM
> You have to update the interface unit for the DLL. If it was written
> for pre-Delphi 2009 you have to replace every occurrance of PChar with
> PAnsiChar in the interface unit. Otherwise the unit will no longer
> correctly describe the functions exported by the DLL.

Thanks Peter! It drove me crazy, I have tried  all supported string 
conversions. I have forwarded this to the 
Armadillo/SoftwarePassport-programmers at Silicon Realms. I hope you agree.

Regards,
John
0
John
5/8/2010 5:15:22 PM
"John Schmidt" <schmidtjohn@yahoo.com> wrote in message 
news:240663@forums.embarcadero.com...
>> DLLFunction(PAnsiChar(a));
>
> I have tried it. The function refutes PAnsiChar telling it's not PChar.


The *Delphi* function declaration then needs to be changed to specify 
PAnsiChar as the parameter type.


-- 
Wayne Niddery (TeamB)
0
Wayne
5/8/2010 6:31:54 PM
"John Schmidt" <schmidtjohn@yahoo.com> wrote in message 
news:240696@forums.embarcadero.com...
>
> Thanks Peter! It drove me crazy, I have tried  all supported string
> conversions. I have forwarded this to the
> Armadillo/SoftwarePassport-programmers at Silicon Realms. I hope you 
> agree.


Show the DLL function declaration here, it *also* needs to specify PAnsiChar 
instead of PChar. That's all.

-- 
Wayne Niddery (TeamB)
0
Wayne
5/8/2010 6:35:00 PM
> Show the DLL function declaration here, it *also* needs to specify PAnsiChar 
> instead of PChar. That's all.

That was really all. Now it works. Thanks to you all!

John
0
John
5/10/2010 11:04:46 AM
Reply: