Delphi 7 中的宽字符串到字符串的转换 [英] Widestring to string conversion in Delphi 7

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

问题描述

我的应用程序是用 Delphi 7 编写的非 Unicode 应用程序.

my app is a non-unicode app written in Delphi 7.

我想用这个函数将 unicode 字符串转换为 ANSI :

I'd like to convert unicode strings to ANSI with this function :

function convertU(ws : widestring) : string;
begin
  result := string(ws);
end;

我也使用此代码来设置要转换的正确代码页.

I use also this code to set the right codepage to convert.

initialization
  SetThreadLocale(GetSystemDefaultLCID);
  GetFormatSettings;

它在 VCL 主线程中运行良好,但在 TThread 中不起作用,由于函数 convertU,我得到了一些问号 '?'.

It works great in the VCL main thread but not in a TThread, where I get some questions marks '?' as result of function convertU.

为什么不在 TThread 中?

Why not in a TThread ?

推荐答案

initialization 块内调用 SetThreadLocale()TThread.如果要设置线程的语言环境,则必须在 TThread.Execute() 方法内部调用 SetThreadLocale().

Calling SetThreadLocale() inside of an initialization block has no effect on TThread. If you want to set a thread's locale, you have to call SetThreadLocale() inside of the TThread.Execute() method.

更好的选择是完全不依赖 SetThreadLocale().通过直接调用 WideCharToMultiByte() 进行您自己的转换,以便您可以指定要转换为的特定 Ansi 代码页.

A better option is to not rely on SetThreadLocale() at all. Do your own conversion by calling WideCharToMultiByte() directly so you can specify the particular Ansi codepage to convert to.

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

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