JackTrapper/scrypt-for-delphi

"Incompatible types: 'PWideChar' and 'Pointer'" in call of FormatMessage()

Opened this issue · 0 comments

The call to FormatMessage() can only be compiled with {$TYPEDADDRESS OFF}. Otherwise

Len := FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER or
FORMAT_MESSAGE_FROM_SYSTEM or
// FORMAT_MESSAGE_IGNORE_INSERTS or
// FORMAT_MESSAGE_ARGUMENT_ARRAY or
FORMAT_MESSAGE_FROM_HMODULE,
Pointer(Hand),
NTStatusMessage, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
@Buffer, 0, nil);

will result in a compiler error: [dcc32 Error] SCrypt.pas(438): E2010 Incompatible types: 'PWideChar' and 'Pointer' (translated from German). This is due to @Buffer being a pointer to PChar while FormatMessage() formally expects a PChar, though because of the combination of flags provided in the first parameter the provided pointer is actually treated as a pointer to PChar internally.

There are two solutions to this problem:

  1. Put a {$TYPEDADDRESS OFF} at the top of the unit, or
  2. put a typecast around @Buffer, i.e., PChar(@Buffer)

I think the second solution is preferrable, because it makes the special handling of this argument more explicit.