Recent

Author Topic: [SOLVED] Is there a common way of declaring FPC and Delphi procedural variables?  (Read 316 times)

440bx

  • Hero Member
  • *****
  • Posts: 786
Hello,

I cannot find a common way of declaring and using procedural variables that both FPC and Delphi find acceptable.  Everything I've tried, if it's acceptable to one compiler it isn't to the other.

Attached is a sample program that I'd like to be able to compile (and run of course) without having to use {$ifdef FPC}.... {$else}.... {$endif}

In the attached sample, there doesn't seem to be a way to make the following statement (its intent) acceptable to both compilers
Code: Pascal  [Select]
  1.     // Delphi doesn't like the statement below.  It likes it without the
  2.     // typecast but then FPC doesn't like it if its not there.
  3.  
  4.     pointer(NtQueryInformationProcessPtr)
  5.            := GetProcAddress(NtdllHandle, 'NtQueryInformationProcess');
  6.  

Is there some way of declaring NtQueryInformationProcessPtr that would make both compilers happy (without using {$ifdef}s ?

Thank you for your help.
« Last Edit: April 23, 2019, 07:21:15 pm by 440bx »
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 7884
:= is not =. That should be enough.
Hamlet 1.4 (nothing wrong with the Danish, btw)

440bx

  • Hero Member
  • *****
  • Posts: 786
:= is not =. That should be enough.
yeah... := is definitely not =.  Now that's out of the way, do you happen to have one way of declaring procedural variables that both compilers would accept ?
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6993
If I leave the pointer() hack out, it works fine with $Mode delphi

440bx

  • Hero Member
  • *****
  • Posts: 786
If I leave the pointer() hack out, it works fine with $Mode delphi
Is there a way that would not require {$MODE DELPHI} ?

FPC requires the pointer typecast because GetProcAddress returns a pointer.  Without it, there is type mismatch.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

440bx

  • Hero Member
  • *****
  • Posts: 786
found the solution... simple too...

Code: Pascal  [Select]
  1.     NtQueryInformationProcessPtr := TNtQueryInformationProcess(
  2.                GetProcAddress(NtdllHandle, 'NtQueryInformationProcess'));
  3.  
That's acceptable to both compilers.   No directives and no modes needed either. 

ETA:

Marco, thank you.  Your comment made me think about typecasting the output of GetProcAddress instead of typecasting the l-value.
« Last Edit: April 23, 2019, 07:22:59 pm by 440bx »
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

ASBzone

  • Full Member
  • ***
  • Posts: 225
  • Automation leads to relaxation...
    • BrainWaveCC Utilities
found the solution... simple too...

Code: Pascal  [Select]
  1.     NtQueryInformationProcessPtr := TNtQueryInformationProcess(
  2.                GetProcAddress(NtdllHandle, 'NtQueryInformationProcess'));
  3.  
That's acceptable to both compilers.   No directives and no modes needed either. 

ETA:

Marco, thank you.  Your comment made me think about typecasting the output of GetProcAddress instead of typecasting the l-value.


Inquisitive question, @440bx -- why the need to avoid any directives or mode?   What about them complicates the solution for you?[/size]
-ASB: https://www.BrainWaveCC.com

Lazarus v2.0.3 r61159 / FPC v3.2.0-beta-r42011 (via FpcUpDeluxe) -- Windows 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1809 (Build 17763.379)
Other Systems: Windows 10 Pro x64, Version 1803 or greater