Thus if you have a void* x parameter in C/C++ it's perfectly valid to use const X in FPC/Delphi as long as it's not necessary for the parameter to be NULL/Nil.
Now I'm puzzled.
Did you actually mean it's valid to use var / constref (not const, as far as FPC is concerned) instead of pointer (if the C function expects pointer and you know you will never need to pass nil)?
it is correct to specify "var" or "constref" for a parameter that is a pointer as long as the parameter cannot be nil. "var" and "constref" pass the address of the parameter.
What he is failing to see, is that the definition of WriteFile taking an untyped parameter is incorrect and NOT equivalent to using "var" or "constref". Specifically, if WriteFile was defined as follows:
type
TBUFFER = packed array[0..0] of byte;
PBUFFER = ^TBUFFER;
// now presume a "Buffer" variable of type PBUFFER (note the P, Buffer is a pointer to a TBUFFER)
// has been allocated (using HeapAlloc or whatever other memory allocation function)
var
Buffer : PBUFFER; // pointer to a dynamically allocated memory block
function WriteFile(const FileHandle : THANDLE;
var DataInBuffer : TBUFFER; // "var" or "constref", makes no difference in this case.
NumberOfBytesToWrite : DWORD;
out NumberOfBytesWritten : DWORD;
Overlapped : POverlapped)
: BOOL; stdcall; external 'kernel32';
That would work. The function could be invoked by dereferencing Buffer, (Buffer^ for the parameter DataInBuffer). Failing to dereference Buffer would cause a compile error because Buffer is a PBUFFER while the API, as defined above requires a TBUFFER, _not_ a PBUFFER (because the parameter is declared as "var".)
when the API is defined as it is currently, it's a dismally incorrect _mess_ (current definition):
function WriteFile( FileHandle : THANDLE;
const DataInBuffer; // NOTE: untyped
NumberOfBytesToWrite : DWORD;
var NumberOfBytesWritten : DWORD;
Overlapped : POverlapped)
: BOOL; stdcall; external 'kernel32';
What happens is, when you pass "Buffer", the compiler takes the address of "Buffer" (because it is untyped) and passes the address of the pointer to the function instead of the pointer itself and, since the parameter is untyped, the compiler doesn't have a clue that what should be passed is a pointer to a TBUFFER. No error, no warning, no nothing.
Added the following lines to the original post:The problem is the use of an untyped parameter, if the parameter had been typed (as is absolutely required in C) then it would be possible to define the API function correctly, either with "var" or "constref".
end of additionsThat definition is dismally incorrect.