On 8 Mar 2005 at 5:09, Waldek Hebisch wrote:
Prof A Olowofoyeku wrote:
program two; uses windows; var h : THandle = LoadLibrary ('winstl32.dll'); v : function : integer = GetProcAddress (h, 'Chief32DllVersion'); begin Writeln ('Handle=', h); Writeln ('V is assigned=', Assigned (v)); { TRUE } Writeln ('Version=', v); { wrong result } Writeln ('DLL Unloaded=',FreeLibrary (h)); { TRUE } end.
What is the possible problem?
- With all programs, declarations of "h" and "v" are accepted by GPC.
But they are not accepted by Delphi, FreePascal, VirtualPascal, or BP ("cannot evaluate this expression" or "constant expression expected"). Is this an EP thing or is it a GPC extension?
BTW: gcc (Mingw) doesn't accept this type of initialisation.
GPC extension (motivated by EP).
- As you can see from the comments, the assignment of "v" to
GetProcAddress ... seems to succeed (in that the variable is indeed assigned), but silently produces a wrong result, meaning that it didn't really get assigned as it should.
There is a bug: instead of assigning result of the call to GetProcAddress GPC is assigning (address of) GetProcAddress itself.
One can easily fix this example (so that GPC reports type error), but as usual parameterless functions are a tricky case. Below partial fix:
--- p/typecheck.c.bb 2005-02-28 13:40:33.000000000 +0100 +++ p/typecheck.c 2005-03-08 04:45:14.612619688 +0100
[...]
Works for me, thanks.
Another problem with function types. Delphi and BP do not accept this program;
program bug1; {$X+} type tfunc = function : pointer; var bar : tfunc; function foo : integer; begin foo := 1024 * 2; end; begin bar := tfunc (foo); { this is where Delphi and BP barf } writeln (integer (bar)); end.
GPC compiles the code, and then the program proceeds to crash when it is run. FreePascal compiles the code, and runs it correctly without crashing. Delphi accepts this instead "bar := tfunc (@foo);", and GPC of course accepts it. Both run it correctly. BP does not accept it - but that is not imporant.
The question is: should GPC compile "bar := tfunc (foo);" only for the program to crash when it gets there? If GPC refuses to compile it, that would be consistent with both BP and Delphi. If GPC compiles it and the program runs correctly, that would be fine also. But if the program will crash, then perhaps GPC should refuse to compile the line. Any comments? Thanks.
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.greatchief.plus.com/