Kardaw
Kardaw

Reputation: 401

NSIS - Refresh rate of a monitor to a variable from a CMD command

This is how I get the refresh rate of my primary monitor, by typing this command in CMD:

wmic PATH Win32_videocontroller get currentrefreshrate

Result:

CurrentRefreshRate
144
75

The second line is the refresh rate of my primary monitor. I would like to have this number assigned to a variable called $refreshrate. How to achieve this?

Upvotes: 0

Views: 2173

Answers (2)

Mofi
Mofi

Reputation: 49096

This could be done with following batch file code:

@set "RefreshRate=" & for /F "tokens=2 delims==" %%I in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_VideoController GET CurrentRefreshRate /VALUE 2^>nul') do @if not defined RefreshRate set "RefreshRate=%%I"

Easier to read is:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=2 delims==" %%I in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_VideoController GET CurrentRefreshRate /VALUE 2^>nul') do set "RefreshRate=%%I" & goto HaveRate
echo Failed to determine the refresh rate of primary monitor.
goto EndBatch

:HaveRate
echo Refresh rate of primary monitor is: %RefreshRate%

:EndBatch
endlocal
pause

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • pause /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic path /?
  • wmic path Win32_VideoController /?
  • wmic path Win32_VideoController get /?

Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded wmic command line with using a separate command process started in background with %ComSpec% /c and the command line within ' appended as additional arguments.

Please note that the WMIC output is always Unicode encoded with UTF-16 Little Endian with byte order mark (BOM) which must be taken into account on processing this output.

I don't know anything about NSIS and so don't know how to run a Windows command from within an NSIS script and get its output assigned to a variable of NSIS.

It would be possible to use following command line to run cmd.exe which exits with the refresh rate of first (primary) monitor.

cmd.exe /E:ON /C "@for /F "tokens=2 delims==" %I in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_VideoController GET CurrentRefreshRate /VALUE 2^>nul') do @exit /B %I"

The exit code of cmd.exe is 0 if the command line failed to determine the refresh rate. Otherwise the exit code returned by cmd.exe is the refresh rate value of first (primary) monitor.

Upvotes: 1

Anders
Anders

Reputation: 101636

You don't have to rely on external tools, you can ask the system directly:

For the entire screen:

!include LogicLib.nsh
Section
!define /IfNDef VREFRESH 116
System::Call USER32::GetDC(p0)p.r0
System::Call GDI32::GetDeviceCaps(pr0,i${VREFRESH})i.r1
System::Call USER32::ReleaseDC(p0,pr0)
${If} $1 < 5
    StrCpy $1 "Invalid"
${EndIf}
DetailPrint "Refresh rate=$1"
SectionEnd

Current display where the application (thread) is:

!include LogicLib.nsh
Section
!define /IfNDef ENUM_CURRENT_SETTINGS -1
!define /IfNDef DM_DISPLAYFREQUENCY 0x00400000

System::Call 'USER32::GetClientRect(p0,@r1)' ; Lazy allocation of a DEVMODE
System::Call '*$1(&i68,&i2 68)'
System::Call 'USER32::EnumDisplaySettingsW(p0, i${ENUM_CURRENT_SETTINGS}, pr1)i.r0'
System::Call '*$1(&i72,i.r2)'
${If} $0 <> 0
${AndIf} $2 & ${DM_DISPLAYFREQUENCY}
    System::Call '*$1(&i184,i.r3)'
    DetailPrint "Current display=$3"
${EndIf}
SectionEnd

Primary adapter:

!include LogicLib.nsh
Section
!define /IfNDef ENUM_CURRENT_SETTINGS -1
!define /IfNDef DM_DISPLAYFREQUENCY 0x00400000
!define /IfNDef DISPLAY_DEVICE_PRIMARY_DEVICE 4
System::Call 'USER32::GetClientRect(p0,@r1)' ; Lazy allocation of a DISPLAY_DEVICE 
StrCpy $4 0
loop:
System::Call '*$1(i840)'
System::Call 'USER32::EnumDisplayDevicesW(p0, ir4, pr1, i0)i.r0'
IntOp $4 $4 + 1
${If} $0 <> 0
    System::Call '*$1(i,&w32.r2,&w128,i.r3)'
    ${If} $3 & ${DISPLAY_DEVICE_PRIMARY_DEVICE}
        System::Call '*$1(&i68,&i2 68)' ; DEVMODE
        System::Call 'USER32::EnumDisplaySettingsW(wr2, i${ENUM_CURRENT_SETTINGS}, pr1)i.r0'
        System::Call '*$1(&i72,i.r2)'
        ${If} $0 <> 0
        ${AndIf} $2 & ${DM_DISPLAYFREQUENCY}
            System::Call '*$1(&i184,i.r3)'
            DetailPrint "Primary adapter=$3"
        ${EndIf}
    ${Else}
        Goto loop
    ${EndIf}
${EndIf}
SectionEnd

If you really really want to use WMI:

!include "StrFunc.nsh"  
${Using:StrFunc} StrRep 
!include LogicLib.nsh
Section
nsExec::ExecToStack '"wmic.exe" PATH Win32_videocontroller get  currentrefreshrate /all'
Pop $0
Pop $1
${StrRep} $1 $1 "currentrefreshrate" ""
loop:
    StrCpy $2 $1 1
    ${If} $2 == "$\r"
    ${OrIf} $2 == "$\n"
    ${OrIf} $2 == " "
        StrCpy $1 $1 "" 1
        Goto loop
    ${EndIf}
IntOp $2 $1 + 0 ; First number
${If} $0 = 0
    DetailPrint WMIC=$2
${EndIf}
SectionEnd

Upvotes: 1

Related Questions