0

I want to get the current resolution of the machine and store it to use for comparison with a standard resolution. We are keeping standard resolution as 1280x1024 or 1920x1080.

I am able to get current resolution as variable $hdc from the amazing answer here https://superuser.com/a/1437714/1280078 but how do I use $hdc further to compare with above resolution/widths.

I used below code in powershell but it does not seem to work for the if condition and only goes to the else condition. What am I doing wrong here?

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class PInvoke {
    [DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hwnd);
    [DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
}
"@
while ($true) #To execute following in infinite loop
{
#Check the width of the machine and store in variable $hdc
$hdc = [PInvoke]::GetDC([IntPtr]::Zero)
[PInvoke]::GetDeviceCaps($hdc, 118) # Get width

#Compare with standard width in standard resolution
if(($hdc -eq 1280) -or ($hdc -eq 1920)) { #NOT Working
   write-host("Resolution is correct")
}else {
    Start-sleep -s 60
    #Wait for resolution to be set on startup and check width again
    write-host("Resolution is not correct, checking again...")
    [PInvoke]::GetDeviceCaps($hdc, 118) # Get width
    
    #Compare with standard widths again, send  email if NOT equal
    if(($hdc -eq 1280) -or ($hdc -eq 1920)) {
        write-host("Resolution is correct")
    }else {
        write-host("Resolution is not correct, triggering email notification...")
        break #break loop if email is triggered
}
}
}

1 Answers1

2

The width isn't stored in the variable $hdc, it is retrieved by running the line [PInvoke]::GetDeviceCaps($hdc, 118). You could do something like $width = [PInvoke]::GetDeviceCaps($hdc, 118) and then replace your $hdc with $width in your if.

So, in other words, something like this should work.

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class PInvoke {
    [DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hwnd);
    [DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
}
"@
while ($true) #To execute following in infinite loop
{
    #Check the width of the machine and store in variable $hdc
    $hdc = [PInvoke]::GetDC([IntPtr]::Zero)
    $width = [PInvoke]::GetDeviceCaps($hdc, 118) # Get width

    #Compare with standard width in standard resolution
    if(($width -eq 1280) -or ($width -eq 1920)) { #NOT Working
       write-host("Resolution is correct")
    }else {
        Start-sleep -s 60
        #Wait for resolution to be set on startup and check width again
        write-host("Resolution is not correct, checking again...")
        $width = [PInvoke]::GetDeviceCaps($hdc, 118) # Get width
    
        #Compare with standard widths again, send  email if NOT equal
        if(($width -eq 1280) -or ($width -eq 1920)) {
            write-host("Resolution is correct")
        }else {
            write-host("Resolution is not correct, triggering email notification...")
            break #break loop if email is triggered
}
}
}
notjustme
  • 2,376
  • 2
  • 20
  • 27