Lazarus

Programming => General => Topic started by: pcurtis on April 14, 2021, 10:27:10 am

Title: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
Post by: pcurtis 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.
Title: Re: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
Post by: Warfley 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
Title: Re: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
Post by: marcov 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.
Title: Re: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
Post by: pcurtis 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?
Title: Re: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
Post by: 440bx 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.)
Title: Re: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
Post by: pcurtis on April 14, 2021, 11:13:55 am
Thanks all, it works.
Title: Re: How do I call GetNumberOfPhysicalMonitorsFromHMONITOR
Post by: marcov 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