如何在 delphi 中使用 MultiByteToWideChar?
how to use MultiByteToWideChar in delphi?
我正在尝试使用 MultiByteToWideChar,但我得到了 'undeclared identifier'。它在哪里声明?哪个 'uses' ?
我正在使用 Embarcadero Delphi XE8。
它是一个Windows API函数,所以如果你想调用它你必须使用Winapi.Windows
。
如果您编写跨平台代码,请改为调用 UnicodeFromLocaleChars
。
MultiByteToWideCharWindowsAPI函数(Win32/Win64)定义在Delphi或FreePascal中Windows单元;只需在 uses 子句中添加 Windows
或 Winapi.Windows
。
您可能希望使用 Delphi 中编写的包装函数将 RawByteString(或 AnsiString)转换为 UnicodeString,反之亦然,而不是直接调用 MultiByteToWideChar。由于底层缓冲区的长度计算不正确,直接调用它可能容易出错。
请注意 Delphi RawByteString 或 AnsiString 有一个 属性 来存储 Windows 代码页值,它由下面代码中的 SetCodePage() 调用设置。该代码使用显式类型,PAnsiChar 与 PWideChar 和 RawByteString 与 UnicodeString 以避免歧义。
uses
Windows;
const
CP_UNICODE_LE = 1200;
function StringToWideStringCP(const S: RawByteString; CP: Integer): UnicodeString;
var
P: PAnsiChar;
pw: PWideChar;
I, J: Integer;
begin
Result := '';
if S = '' then
Exit;
if CP = CP_UTF8 then
begin
// UTF8
Result := UTF8ToUnicodeString(S);
Exit;
end;
P := @S[1];
I := MultiByteToWideChar(CP, 0, P, Length(S), nil, 0);
if I <= 0 then
Exit;
SetLength(Result, I);
pw := @Result[1];
J := MultiByteToWideChar(CP, 0, P, Length(S), pw, I);
if I <> J then
SetLength(Result, Min(I, J));
end;
function WideStringToStringCP(const w: UnicodeString; CP: Integer)
: RawByteString;
var
P: PWideChar;
I, J: Integer;
begin
Result := '';
if w = '' then
Exit;
case CP of
CP_UTF8:
begin
// UTF8
Result := UTF8Encode(w);
Exit;
end;
CP_UNICODE_LE:
begin
// Unicode codepage
CP := CP_ACP;
end;
end;
P := @w[1];
I := WideCharToMultibyte(CP, 0, P, Length(w), nil, 0, nil, nil);
if I <= 0 then
Exit;
SetLength(Result, I);
J := WideCharToMultibyte(CP, 0, P, Length(w), @Result[1], I, nil, nil);
if I <> J then
SetLength(Result, Min(I, J));
SetCodePage(Result, CP, False);
end;
我正在尝试使用 MultiByteToWideChar,但我得到了 'undeclared identifier'。它在哪里声明?哪个 'uses' ?
我正在使用 Embarcadero Delphi XE8。
它是一个Windows API函数,所以如果你想调用它你必须使用Winapi.Windows
。
如果您编写跨平台代码,请改为调用 UnicodeFromLocaleChars
。
MultiByteToWideCharWindowsAPI函数(Win32/Win64)定义在Delphi或FreePascal中Windows单元;只需在 uses 子句中添加 Windows
或 Winapi.Windows
。
您可能希望使用 Delphi 中编写的包装函数将 RawByteString(或 AnsiString)转换为 UnicodeString,反之亦然,而不是直接调用 MultiByteToWideChar。由于底层缓冲区的长度计算不正确,直接调用它可能容易出错。
请注意 Delphi RawByteString 或 AnsiString 有一个 属性 来存储 Windows 代码页值,它由下面代码中的 SetCodePage() 调用设置。该代码使用显式类型,PAnsiChar 与 PWideChar 和 RawByteString 与 UnicodeString 以避免歧义。
uses
Windows;
const
CP_UNICODE_LE = 1200;
function StringToWideStringCP(const S: RawByteString; CP: Integer): UnicodeString;
var
P: PAnsiChar;
pw: PWideChar;
I, J: Integer;
begin
Result := '';
if S = '' then
Exit;
if CP = CP_UTF8 then
begin
// UTF8
Result := UTF8ToUnicodeString(S);
Exit;
end;
P := @S[1];
I := MultiByteToWideChar(CP, 0, P, Length(S), nil, 0);
if I <= 0 then
Exit;
SetLength(Result, I);
pw := @Result[1];
J := MultiByteToWideChar(CP, 0, P, Length(S), pw, I);
if I <> J then
SetLength(Result, Min(I, J));
end;
function WideStringToStringCP(const w: UnicodeString; CP: Integer)
: RawByteString;
var
P: PWideChar;
I, J: Integer;
begin
Result := '';
if w = '' then
Exit;
case CP of
CP_UTF8:
begin
// UTF8
Result := UTF8Encode(w);
Exit;
end;
CP_UNICODE_LE:
begin
// Unicode codepage
CP := CP_ACP;
end;
end;
P := @w[1];
I := WideCharToMultibyte(CP, 0, P, Length(w), nil, 0, nil, nil);
if I <= 0 then
Exit;
SetLength(Result, I);
J := WideCharToMultibyte(CP, 0, P, Length(w), @Result[1], I, nil, nil);
if I <> J then
SetLength(Result, Min(I, J));
SetCodePage(Result, CP, False);
end;