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 @@ -1624,7 +1626,8 @@ /* Procedural variables. (Pointers to routines should cause no problems.) */ if (TREE_CODE (type) == REFERENCE_TYPE && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE - && TREE_CODE (TREE_VALUE (init)) == CALL_EXPR) + && TREE_CODE (TREE_VALUE (init)) == CALL_EXPR + && TREE_OPERAND (TREE_VALUE (init), 1) == NULL_TREE) { tree op = TREE_OPERAND (TREE_VALUE (init), 0); if (TREE_CODE (op) == ADDR_EXPR)