不兼容的类型:'PAnsiChar' 和 'PWideChar'

Incompatible types: 'PAnsiChar' and 'PWideChar'

我是 delphi XE8 的新手。我有以下来自我的 delphi 版本 6 的代码,我想 运行 它在 delphi XE8.

1. function UTF8ToStringLen(const src: PChar; const Len: Cardinal): widestring;
2. var
3.   l: Integer;
4. begin
5.   SetLength(Result, Len);
6.   if Len > 0 then
7.   begin                                             
8.     l := MultiByteToWideChar( CP_UTF8, 0, src, Len, PWChar(Result), Len*SizeOf(WideChar));  <--error
9.     SetLength(Result, l);
10.   end;
11. end;
12. 
13. 
14. function StringToUTF8Len(const src: PChar; const Len: Cardinal): string;
15. var
16.   bsiz: Integer;
17.   Temp: string;
18. begin
19.   bsiz := Len * 3;
20.   SetLength(Temp, bsiz);
21.   if bsiz > 0 then
22.   begin
23.     StringToWideChar(src, PWideChar(Temp), bsiz);
24.     SetLength(Result, bsiz);                
25.     bsiz := WideCharToMultiByte(CP_UTF8, 0, PWideChar(Temp), -1, PChar(Result), bsiz, nil, nil);  <--error
26.     if bsiz > 0 then dec(bsiz);
27.     SetLength(Result, bsiz);
28.   end;
29. end;

当我尝试 运行 时,错误指向代码中的第 8 行和第 25 行,并显示一条错误消息

ERROR MESSAGE: Incompatible types: 'PAnsiChar' and 'PWideChar'

我到处搜索解决方案,但我无法解决问题。请帮忙..谢谢.

在 Delphi 2007 年及更早版本中,PCharPAnsiChar 的别名。在 Delphi 2009 及之后的版本中,PCharPWideChar 的别名。因此,通过更改编译器,您可以更改代码的含义。

您只需将 PChar 替换为 PAnsiChar 即可解决此问题,代码将具有其原始含义。

在现代 Unicode Delphi 中,使用 stringUnicodeString 的别名)而不是 COM WideString 会更自然。您还可以使用许多库例程中的一个来执行 UTF-8 转换。

您肯定还有其他问题。我建议您下一步阅读 Marco Cantù's whitepaper on Unicode in Delphi

来自 MDSN:

  • MultiByteToWideChar function

    int MultiByteToWideChar(
     _In_      UINT   CodePage,
     _In_      DWORD  dwFlags,
     _In_      LPCSTR lpMultiByteStr,
     _In_      int    cbMultiByte,
     _Out_opt_ LPWSTR lpWideCharStr,
     _In_      int    cchWideChar
    );
    
  • WideCharToMultiByte function

    int WideCharToMultiByte(
     _In_      UINT    CodePage,
     _In_      DWORD   dwFlags,
     _In_      LPCWSTR lpWideCharStr,
     _In_      int     cchWideChar,
     _Out_opt_ LPSTR   lpMultiByteStr,
     _In_      int     cbMultiByte,
     _In_opt_  LPCSTR  lpDefaultChar,
     _Out_opt_ LPBOOL  lpUsedDefaultChar
    );
    
  • 来自 Windows Data Types:

    LPCSTR
    A pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters.
    This type is declared in WinNT.h as follows:
    typedef __nullterminated CONST CHAR *LPCSTR;

    LPSTR
    A pointer to a null-terminated string of 8-bit Windows (ANSI) characters.
    This type is declared in WinNT.h as follows:
    typedef CHAR *LPSTR;


与您的代码相关的问题是每个函数的参数 lpMultiByteStr 是一个 PAnsiChar 并且您将 PChar 作为参数传递。
PCharDelphi6PAnsiChar的别名,代表[=70中的PWideChar =] XE8

  • 您可以在第 8 行声明您的功能(并相应地调用它),如下所示解决此问题:
    function UTF8ToStringLen(const src: PAnsiChar; const Len: Cardinal): WideString;

  • 要解决第 25 行的问题,请将函数声明更改为:
    function StringToUTF8Len(const src: PAnsiChar; const Len: Cardinal): string;
    和“infamous”行:
    bsiz := WideCharToMultiByte(CP_UTF8, 0, PWideChar(Temp), -1, PAnsiChar(Result), bsiz, nil, nil);

在每种情况下 适用。