Astaroth
Astaroth

Reputation: 2291

What is the CCHAR type equivalent in Delphi?

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.

Upvotes: 3

Views: 263

Answers (2)

Martin Liversage
Martin Liversage

Reputation: 106826

I believe the explanation of CCHAR is wrong. The C prefix indicates that this is a count of characters so this is probably a simple copy-paste error done by Microsoft when writing the explanation.

It is stored in a byte and it is used to count the number of bytes of a string of characters. These characters may be wide characters but the CCHAR value still counts the number of bytes used to store the characters.

The natural translation for this type is Byte. If you marshal it to a character type like AnsiChar you will have to convert the character to an integer value (e.g. a byte) before using it.

Upvotes: 4

GolezTrol
GolezTrol

Reputation: 116110

It's a byte, or at least, it is used as a 1 byte integer. In C, chars can be used for this purpose. In Delphi, you couldn't do that without typecasting. So you could use Char, but then you would need to give it the value 'A' or Chr(65) to indicate a string of 65 characters. Now, that would be silly. :-)

To be able to pass it to the API it must have the same size. Apart from that, the callee will not even know how it is declared, so declaring it as a Delphi byte is the most logical solution. A choice backed up by the other declaration you found.

Upvotes: 5

Related Questions