Constant Procedure Variable Type initialization not possible (Delphi-style)
Original Reporter info from Mantis: Norman Hempel
-
Reporter name: Norman Hempel
Original Reporter info from Mantis: Norman Hempel
- Reporter name: Norman Hempel
Description:
It seems to be not possible currently to initialize a procedure variable type in a const section.
Working sample from Delphi:
const
GameObjectClass_Get_Metal_Cost : function(team : integer) : Integer; stdcall = Pointer($004CDFA0);
When using in Freepascal, the compiler reports: Error: Incompatible types: got "Pointer" expected "&LtPos;procedure variable type of function(LongInt):LongInt;StdCall>"
A more "cleaner" approach doesn't work either:
type
TGameObjectClass_Get_Metal_Cost = function(team : integer) : Integer; stdcall;
const
GameObjectClass_Get_Metal_Cost : TGameObjectClass_Get_Metal_Cost = TGameObjectClass_Get_Metal_Cost($004CDFA0);
The only solution to initialize a procedure variable currently is:
type
TGameObjectClass_Get_Metal_Cost = function(team : integer) : Integer; stdcall;
var
GameObjectClass_Get_Metal_Cost : TGameObjectClass_Get_Metal_Cost;
initialization
@GameObjectClass_Get_Metal_Cost := TGameObjectClass_Get_Metal_Cost($004CDFA0);
However this has a few drawbacks. First of all the procedure variable in included in the binary by the compiler, even if never referenced anywhere else (since it is being set in the initialization section). The code gets undesirably long and hard to maintain. And of course the procedure variable is now var instead of const.
Basically it currently seems not possible to initialize a procedure type in a const nor var section in fpc with a value. I guess it is desired to have a way to initialize such a const/var, since it is nothing other than a pointer.
Some background: I am currently working on converting a project from Delphi to FreePascal. It is a hooking DLL that adds various functionality to an existing game. The hook requires to call functions from the game again. Since we can assume that their module position for the main executable never changes from $400000, these functions have static memory adresses. Currently i don't have one or two of these procedure variables defined but more than 1000, so you may understand why i hope for an improvement/fix there. If you want to know which project it is - www.fleetops.net
Mantis conversion info:
- Mantis ID: 21267
- Fixed in version: 3.0.0
- Fixed in revision: 21344 (#eb7e1922)