[fpc-pascal]Win32 API Call

Thomas Schatzl tom_at_work at yline.com
Fri Dec 7 20:00:27 CET 2001


>Sorry for what's most likely a neophytes question...
>I'm trying to call the Win32 API function GetDiskFreeSpaceEx, but
>when I do the compiler complains and I get the following error:
>Incompatible type for arg no. 4: Got "pLongint", expected
>The 4th argument is the same type as the 2nd and 3rd arguments, which
>according to the API Reference they're supposed to be.

FPC seems to do argument type checking from right to left (e.g. in the order
the parameters are pushed on the stack). Since the compiler is by default
set up to exit after the first error you simply don't see the others. ^^

>Here's the code:
>Program SI;
>  BUF_SIZE = 255;

I think you better use the constant MAX_PATH here which should be defined in
the windows unit.... (well, I think it's 255 anyway but you never know).
[Didn't check the documentation, but those buffers usually ought to be
MAX_PATH in size]

>  pLongint = ^longint;
>  Buffer   = array [1..BUF_SIZE] of char;

I'm not sure here again, but I think 0..MAX_PATH (or BUF_SIZE) is more
appropriate here...

>  BytesAvail   : pLongint;
>  BytesTotal   : pLongint;
>  BytesFree    : pLongint;

Use PULARGE_INTEGER here and it should work fine.

>  CharBuffer   : ^Buffer;
>  new (BytesAvail); new (BytesTotal);
>  new (BytesFree);  new (CharBuffer);
>  GetDiskFreeSpaceEx (CharBuffer,BytesAvail,BytesTotal,BytesFree);

Btw, I'd recommend using ULARGE_INTEGERs (64 bit integers) or the Pascal
Int64 type for the three size parameters and call the function this way:

GetDiskFreeSpaceEx(CharBuffer, @BytesAvail, @BytesTotal, @BytesFree);

Although it doesn't look that good you save the new() and dispose() calls
here. It's more to write too, and you often forget either one new() or
dispose() calls which cause memory leaks or weird program behaviour. Otoh
there might be a function declaration in the windows unit which
automatically dereferences the variables using var parameters. E.g. try

GetDiskFreeSpaceEx(CharBuffer, BytesAvail, BytesTotal, BytesFree);


>Now, I'll be the first to admit that I'm no Win32 expert, but to me I did
>everything correct (but obviously FPC is smarter then I am :-)
>Can anybody point me in the right direction? TIA...


More information about the fpc-pascal mailing list