[fpc-pascal]Win32 API Call

Matt Emson memsom at interalpha.co.uk
Wed Jun 23 08:42:08 CEST 2004


> That's awfully odd, because according to the Win32 API reference the
> function GetCurrentHwProfile requires a pointer, which pProfileInfo is
> (tProfileInfo is just the type definition). Guess I have some more reading
> to do.

No, it makes a lot of sense. You could use:

....(var value: TProfileInfo, ...) *or* ....(pvalue: PProfileInfo, ...)

Passing as "var" passes the value as a pointer behind the scenes, but gives
you the chance not to mess about with casting etc. You can't do this is C
really (winapi written in C..) Not back when the Win32API was written. Back
then every C app passed by value or as a pointer to simualte by reference.

>  But in your code it's still a boolean, but it now works (it returns
TRUE).
> And, when I change "Results : longbool" in the var declaration I get
"Hint:
> Type size mismatch, possible loss of data / range check error" at the
> "writeln ('Results: ',Results,' - ',GetLastError);" statement (and with
> either declaration the function returns TRUE, so I assume it's working
with
> both). Does that mean longbool isn't really necessary?

It should be boolean. All Win32API return values which are BOOL translate as
Boolean.

> Another oddity, because the API reference says they're null-terminated
> strings. So shouldn't an ansistring work for that too?

No, no, no!! AnsiString (just use "String", it's the same difference unless
you change the RTL to use WideStrings) is an array of char 1...N, with an
extra hidden 0 element. This is a hangover from shortstrings which used
element 0 to hold their length. A PChar is an array 0...N-1 or char. In my
experiance you *can* pass Strings to API calls if you  pass something like
@(s[1]) or in Delphi pchar(string), but if you use them in this
Record/struct, you'll quickly find that String <> PChar.

> And where did you get those length numbers from, the ones you used for
> those const definitions? I can't seem to find them anywhere, and it
appears
> that in order to get the info to display properly you need to assign
> specific lengths to those char strings.

Generally, with a Win32API call set your PChars to point to a buffer and
tell the API call how long it is. In some structures (records) the seze is
set, or is of a specific fixed sized type.

> >4. This is not important for FPC 1.0.x, but for newer FPC remember to
> >declare WinAPI functions as "stdcall" (FPC 1.0.x just used "stdcall" by
> >default).
>
> I'll make note of that.

To be fair to you, FPC should have always used StdCall for Win32API calls.
It would be less confusing now, anyway.

Matt





More information about the fpc-pascal mailing list