Recent

Author Topic: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR  (Read 1130 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
« on: April 14, 2021, 10:27:10 am »
i have a C function GetNumberOfPhysicalMonitorsFromHMONITOR

Quote
Syntax
C++

_BOOL GetNumberOfPhysicalMonitorsFromHMONITOR(
  HMONITOR hMonitor,
  LPDWORD  pdwNumberOfPhysicalMonitors
);

Parameters

hMonitor

A monitor handle. Monitor handles are returned by several Multiple Display Monitor functions, including EnumDisplayMonitors and MonitorFromWindow, which are part of the graphics device interface (GDI).

pdwNumberOfPhysicalMonitors

Receives the number of physical monitors associated with the monitor handle.

I would like to call in fpc.

So far I have

Code: Pascal  [Select][+][-]
  1.  function GetNumberOfPhysicalMonitorsFromHMONITOR(HMONITOR: HMONITOR;
  2.     pwNumberOfPhysicalMonitors : DWORD): boolean;
  3.     stdcall; external 'Dxva2.dll' Name 'GetNumberOfPhysicalMonitorsFromHMONITOR';
  4.  
  5. procedure TForm1.FormCreate(Sender: TObject);
  6. var
  7.    h: HMONITOR;
  8.    NoOfPhyicalMonitors : xxxxxxx < What should go here
  9. begin
  10.   h := Screen.Monitors[0].Handle;  
  11.   GetNumberOfPhysicalMonitorsFromHMONITOR(h, NoOfPhyicalMonitors);
  12.   ........
  13.  

How do I declare NoOfPhyicalMonitors.

Thanks in advance.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
« Reply #1 on: April 14, 2021, 10:46:10 am »
LPDWORD is a windows type and stands for "Long Pointer Double Word" and basically is a pointer to a DWord/Cardinal type, which you need to pass by pointer:
Code: Pascal  [Select][+][-]
  1.    h: HMONITOR;
  2.    NoOfPhyicalMonitors : DWord;
  3. begin
  4.   h := Screen.Monitors[0].Handle;  
  5.   GetNumberOfPhysicalMonitorsFromHMONITOR(h, @NoOfPhyicalMonitors);

You also have the function header wrong, you declare pwNumberOfPhysicalMonitors as DWORD not as LPDWORD, PDWord or PCardinal
« Last Edit: April 14, 2021, 10:48:56 am by Warfley »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
« Reply #2 on: April 14, 2021, 10:52:45 am »
Yup. In older header translations you might also see pxxx translated as "var x  : something", but nowadays the trend is to keep it to C as much as possible, and only do the VAR translation for functions that do so historically or certain xtremely much used functions.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
« Reply #3 on: April 14, 2021, 10:58:34 am »
Tried that and got this

Quote
Compile Project, Mode: Debug, Target: project1.exe: Exit code 1, Errors: 4, Hints: 4
unit1.pas(64,66) Error: Incompatible type for arg no. 2: Got "Pointer", expected "LongWord"
unit1.pas(44,15) Hint: Found declaration: GetNumberOfPhysicalMonitorsFromHMONITOR(QWord;LongWord):Boolean; StdCall;

Is my declaration of the function correct?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
« Reply #4 on: April 14, 2021, 11:03:21 am »
Tried that and got this

Quote
Compile Project, Mode: Debug, Target: project1.exe: Exit code 1, Errors: 4, Hints: 4
unit1.pas(64,66) Error: Incompatible type for arg no. 2: Got "Pointer", expected "LongWord"
unit1.pas(44,15) Hint: Found declaration: GetNumberOfPhysicalMonitorsFromHMONITOR(QWord;LongWord):Boolean; StdCall;

Is my declaration of the function correct?
No, the declaration is not correct.  It should be :
Code: Pascal  [Select][+][-]
  1.  function GetNumberOfPhysicalMonitorsFromHMONITOR(HMONITOR: HMONITOR;
  2.     pwNumberOfPhysicalMonitors : PDWORD): boolean;
  3.     stdcall; external 'Dxva2.dll' Name 'GetNumberOfPhysicalMonitorsFromHMONITOR';
Note that pwNumberOfPhysicalMonitors is declared with the type PDWORD, not DWORD (as you declared it)

With that declaration, the example @Warfley provided should work.

HTH.

ETA:

Personally, I'd recommend not specifying the name, that way it's almost instantaneous to know that the declared name matches the dll's name.  Following that convention makes it clear that whenever the name is specified, the function name is an alias for the dll's function name (which can be useful to know.)
« Last Edit: April 14, 2021, 11:06:33 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
« Reply #5 on: April 14, 2021, 11:13:55 am »
Thanks all, it works.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
« Reply #6 on: April 14, 2021, 12:13:25 pm »
Notice that if you find user32 or kernel32 functions missing, a small bugreport (and maybe even a.... patch) into bugrepo will be appreciated.

 

TinyPortal © 2005-2018