简体   繁体   English

如何在 delphi 中使用 MultiByteToWideChar?

[英]how to use MultiByteToWideChar in delphi?

I am trying to use MultiByteToWideChar but i get 'undeclared identifier'.我正在尝试使用 MultiByteToWideChar,但我得到“未声明的标识符”。 Where is it declared?它在哪里声明? which 'uses'?哪个“使用”?

I am using Embarcadero Delphi XE8.我正在使用 Embarcadero Delphi XE8。

It is a Windows API function, so if you want to call it you must use Winapi.Windows .它是一个 Windows API 函数,所以如果你想调用它,你必须使用Winapi.Windows

If you write cross platform code then call UnicodeFromLocaleChars instead.如果您编写跨平台代码,则改为调用UnicodeFromLocaleChars

The MultiByteToWideChar Windows API function (Win32/Win64) is defined in Delphi or FreePascal in the Windows unit; MultiByteToWideChar Windows API函数(Win32/Win64)定义在Windows单元的Delphi或FreePascal中; just add Windows or Winapi.Windows to the uses clause.只需将WindowsWinapi.Windows添加到 uses 子句即可。

You may wish to use wrapper function written in Delphi to convert RawByteString (or AnsiString) to UnicodeString and vice versa, rather than calling the MultiByteToWideChar directly.您可能希望使用用 Delphi 编写的包装函数将 RawByteString(或 AnsiString)转换为 UnicodeString,反之亦然,而不是直接调用 MultiByteToWideChar。 Calling it directly may be prone to errors due to incorrect calculation of the lengths of the underlying buffers.由于底层缓冲区的长度计算不正确,直接调用它可能容易出错。

Please note that Delphi RawByteString or AnsiString have a property to store the Windows code page value, and it is set by the SetCodePage() call in the code below.请注意,Delphi RawByteString 或 AnsiString 具有存储 Windows 代码页值的属性,它由下面代码中的 SetCodePage() 调用设置。 The code uses explicit types, PAnsiChar vs PWideChar and RawByteString vs UnicodeString to avoid ambiguity.该代码使用显式类型、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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM