3

The ShortNameLength member of FILE_BOTH_DIR_INFORMATION structure is declared as follows:

typedef struct FILE_BOTH_DIR_INFORMATION {
  ...
  CCHAR  ShortNameLength;
  ...
};

From the explanation of CCHAR type, CCHAR is a 8-bit Windows (ANSI) character. So, it is equivalent to AnsiChar in Delphi, right? However, the description of ShortNameLength member of FILE_BOTH_DIR_INFORMATION structure says,

ShortNameLength specifies the length, in bytes, of the short file name string.”

The statement makes me think that the CCHAR equivalent is Byte in Delphi. Another example is the NumberOfProcessors member of SYSTEM_BASIC_INFORMATION which is declared in winternl.h as follows:

typedef struct _SYSTEM_BASIC_INFORMATION {
  BYTE Reserved1[24];
  PVOID Reserved2[4];
  CCHAR NumberOfProcessors;
}

Once again, the CCHAR type seems to be used in a Byte context, rather than AnsiChar context.

Now, I confuse, whether to use AnsiChar or Byte as a CCHAR equivalent in Delphi.

Note

JwaWinType.pas of JEDI Windows API declares CCHAR as AnsiChar.

4

2 回答 2

5

它是一个字节,或者至少,它被用作一个 1 字节整数。在 C 中,字符可用于此目的。在 Delphi 中,如果不进行类型转换,您将无法做到这一点。所以你可以使用Char,但是你需要给它一个值'A'Chr(65)指示一个 65 个字符的字符串。现在,那将是愚蠢的。:-)

为了能够将它传递给 API,它必须具有相同的大小。除此之外,被调用者甚至不知道它是如何声明的,因此将其声明为 Delphi 字节是最合乎逻辑的解决方案。您找到的另一个声明支持的选择。

于 2012-11-10T14:39:26.620 回答
4

我相信 的解释CCHAR是错误的。C前缀表示这是字符,因此这可能是 Microsoft 在编写说明时所做的简单复制粘贴错误。

它存储在一个字节中,用于计算一串字符的字节数。这些字符可能是宽字符,但该CCHAR值仍然计算用于存储字符的字节数。

这种类型的自然翻译是Byte. 如果您将其编组为字符类型AnsiChar,则必须在使用之前将字符转换为整数值(例如字节)。

于 2012-11-10T14:40:54.653 回答