将字符串转换为Delphi 2009中的PAnsiChar [英] Convert string to PAnsiChar in Delphi 2009

查看:238
本文介绍了将字符串转换为Delphi 2009中的PAnsiChar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将应用程序转换为Delphi 2009,并在一些需要将字符串(广泛)转换为AnsiString的调用中遇到一个有趣的问题。

I'm converting my applications to Delphi 2009 and faced an intriguing issue with some calls that need to convert a string (wide) to AnsiString.

这是一个例子以证明我遇到的问题:

Here's an example to demonstrate the issue I'm having:

var
  s: PAnsiChar;

...

s := PAnsiChar(Application.ExeName);

使用Delphi 2007和以前的版本,s = = PChar(Application.ExeName)将返回应用程序exe路径。

With Delphi 2007 and previous versions, s := PChar(Application.ExeName) would return the application exe path.

与Delphi 2009,s:= PAnsiChar(Application.ExeName)只返回'E'。

with Delphi 2009, s := PAnsiChar(Application.ExeName) returns only 'E'.

我的猜测是因为我将unicode字符串转换为ansi字符串,但是如何转换它,以便PAnsiChar获取完整的字符串?

My guess is that's because I'm converting a unicode string to an ansi string but how can I convert it so that a PAnsiChar gets the full string?

推荐答案

我在这里没有Delphi 2009,所以我无法检查。但是也许你必须尝试:

I have no Delphi 2009 here so I can't check it. But maybe you have to try:

s := PAnsiChar(AnsiString(Application.ExeName));

正如gabr已经指出的那样,这不是一个很好的做法,如果你是100%确定,字符串只包含与ansi范围直接映射的字符。

As gabr already pointed, this is not a very good practice and you will only use it if you are 100% sure, the string only contains characters that have a direct mapping to the ansi range.

这就是为什么你应该得到一个警告,因为你将Unicode转换为Ansi。

That's why you should get a warning because you are converting Unicode to Ansi.

这篇关于将字符串转换为Delphi 2009中的PAnsiChar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆