我在这里https://nsis.sourceforge.io/WMI_Macro(我已经为自己修改了一些地方)使用WMI宏来读取有关笔记本电脑上GPU的信息。
在没有独立GPU卡的笔记本电脑上,代码运行得很好。
Unicode true
RequestExecutionLevel user
!include 'LogicLib.nsh'
!include 'TextFunc.nsh'
!include 'x64.nsh'
!include 'WinVer.nsh'
Var /GLOBAL Var_GPU
/* Macro to remove leading and trailing white spaces from a string.
https://nsis.sourceforge.io/WMI_Macro
Derived from the function originally posted by Iceman_K at:
http://nsis.sourceforge.net/Remove_leading_and_trailing_whitespaces_from_a_string
--------------------------------------------------------------------------------- */
!ifmacrondef _Trim
!macro _Trim _UserVar _OriginalString
!define Trim_UID ${__LINE__}
Push $R1
Push $R2
Push `${_OriginalString}`
Pop $R1
Loop_${Trim_UID}:
StrCpy $R2 "$R1" 1
StrCmp "$R2" " " TrimLeft_${Trim_UID}
StrCmp "$R2" "$\r" TrimLeft_${Trim_UID}
StrCmp "$R2" "$\n" TrimLeft_${Trim_UID}
StrCmp "$R2" "$\t" TrimLeft_${Trim_UID}
GoTo Loop2_${Trim_UID}
TrimLeft_${Trim_UID}:
StrCpy $R1 "$R1" "" 1
Goto Loop_${Trim_UID}
Loop2_${Trim_UID}:
StrCpy $R2 "$R1" 1 -1
StrCmp "$R2" " " TrimRight_${Trim_UID}
StrCmp "$R2" "$\r" TrimRight_${Trim_UID}
StrCmp "$R2" "$\n" TrimRight_${Trim_UID}
StrCmp "$R2" "$\t" TrimRight_${Trim_UID}
GoTo Done_${Trim_UID}
TrimRight_${Trim_UID}:
StrCpy $R1 "$R1" -1
Goto Loop2_${Trim_UID}
Done_${Trim_UID}:
Pop $R2
Exch $R1
Pop ${_UserVar}
!undef Trim_UID
!macroend
!ifndef Trim
!define Trim `!insertmacro _Trim`
!endif
!endif
/* RunWMIC - Retrieves a single property value from a WMI Class
https://nsis.sourceforge.io/WMI_Macro
--------------------------------------------------------- */
!ifmacrondef _RunWMIC
!macro _RunWMIC _USERVAR _NAMESPACE _CLASSNAME _PROPERTY
!define WMIC_UID ${__LINE__}
ClearErrors
Push $0
Push $1
Push $2
Push ${_USERVAR}
Pop $0 ; _USERVAR
Push ${_CLASSNAME}
Pop $1 ; _CLASSNAME
Push ${_PROPERTY}
Pop $2 ; _PROPERTY
DOS::ExecToStack /OEM 'wmic /namespace:\\${_NAMESPACE} path $1 get $2 /format:textvaluelist.xsl'
Pop $0
StrCmp $0 0 0 Else_${WMIC_UID}
Pop $0
${Trim} $0 $0
StrLen $1 $2
IntOp $1 $1 + 1
StrCpy $0 $0 ${NSIS_MAX_STRLEN} $1
Goto End_${WMIC_UID}
Else_${WMIC_UID}:
SetErrors
StrCpy $0 ''
End_${WMIC_UID}:
Pop $2
Pop $1
Exch $0
Pop ${_USERVAR}
!undef WMIC_UID
!macroend
!ifndef RunWMIC
!define RunWMIC '!insertmacro _RunWMIC'
!endif
!endif
Section
SetDetailsView show
${RunWMIC} $R1 'root\CIMV2' 'Win32_VideoController' 'Caption'
${RunWMIC} $R2 'root\CIMV2' 'Win32_VideoController' 'CurrentHorizontalResolution'
${RunWMIC} $R3 'root\CIMV2' 'Win32_VideoController' 'CurrentVerticalResolution'
StrCpy $Var_GPU '$R1 ($R2x$R3)'
DetailPrint '----------------GPU(s)----------------'
DetailPrint '$Var_GPU'
SectionEnd
但是,当笔记本电脑上的GPU数量超过1个时,结果将不符合预期。
在DetailPrint上:
----------------GPU(s)----------------
Intel(R) UHD Graphics 620
Caption=NVIDIA GeForce MX130 (1920
CurrentHorizontalResolution=x1080
CurrentVerticalResolution=)
如何修复?。