I build a Powershell script and with the help of @SKAN RunCMD function I have been able to retrieve the CPU and GPU, temp:
Code: Select all
script =
(
Add-Type -Path "C:\Users\Downloads\OpenHardwareMonitorLib.dll"
$Comp = New-Object -TypeName OpenHardwareMonitor.Hardware.Computer
$Comp.Open()
$Comp.CPUEnabled = 1
$Comp.GPUEnabled = 1
ForEach ($HW in $Comp.Hardware) {
$HW.Update()
'# ' + $hw.HardwareType + ' - ' + $hw.name
If ($hw.HardwareType -eq 'CPU') {
ForEach ($Sensor in $HW.Sensors) {
#$Sensor.Name + ' - ' + $Sensor.SensorType + ' : ' + $Sensor.value
If ($Sensor.Name -eq 'CPU Package' -and $Sensor.SensorType -eq [OpenHardwareMonitor.Hardware.SensorType]::Temperature) {
$Sensor.Name
$Sensor.value
Break
}
}
}
If ($hw.HardwareType -eq 'GpuNvidia') {
ForEach ($Sensor in $HW.Sensors) {
#$Sensor.Name + ' - ' + $Sensor.SensorType + ' : ' + $Sensor.value
If ($Sensor.SensorType -eq [OpenHardwareMonitor.Hardware.SensorType]::Temperature) {
$Sensor.Name
$Sensor.value
Break
}
}
}
$hw.SubHardware
}
$Comp.Close()
)
psex := "powershell -WindowStyle Hidden " script
MsgBox % RunCMD(psex)
ExitApp
RunCMD(CmdLine, WorkingDir:="", Codepage:="CP0", Fn:="RunCMD_Output") { ; RunCMD v0.94
Local ; RunCMD v0.94 by SKAN on D34E/D37C @ autohotkey.com/boards/viewtopic.php?t=74647
Global A_Args ; Based on StdOutToVar.ahk by Sean @ autohotkey.com/board/topic/15455-stdouttovar
Fn := IsFunc(Fn) ? Func(Fn) : 0
, DllCall("CreatePipe", "PtrP",hPipeR:=0, "PtrP",hPipeW:=0, "Ptr",0, "Int",0)
, DllCall("SetHandleInformation", "Ptr",hPipeW, "Int",1, "Int",1)
, DllCall("SetNamedPipeHandleState","Ptr",hPipeR, "UIntP",PIPE_NOWAIT:=1, "Ptr",0, "Ptr",0)
, P8 := (A_PtrSize=8)
, VarSetCapacity(SI, P8 ? 104 : 68, 0) ; STARTUPINFO structure
, NumPut(P8 ? 104 : 68, SI) ; size of STARTUPINFO
, NumPut(STARTF_USESTDHANDLES:=0x100, SI, P8 ? 60 : 44,"UInt") ; dwFlags
, NumPut(hPipeW, SI, P8 ? 88 : 60) ; hStdOutput
, NumPut(hPipeW, SI, P8 ? 96 : 64) ; hStdError
, VarSetCapacity(PI, P8 ? 24 : 16) ; PROCESS_INFORMATION structure
If not DllCall("CreateProcess", "Ptr",0, "Str",CmdLine, "Ptr",0, "Int",0, "Int",True
,"Int",0x08000000 | DllCall("GetPriorityClass", "Ptr",-1, "UInt"), "Int",0
,"Ptr",WorkingDir ? &WorkingDir : 0, "Ptr",&SI, "Ptr",&PI)
Return Format("{1:}", "", ErrorLevel := -1
,DllCall("CloseHandle", "Ptr",hPipeW), DllCall("CloseHandle", "Ptr",hPipeR))
DllCall("CloseHandle", "Ptr",hPipeW)
, A_Args.RunCMD := { "PID": NumGet(PI, P8? 16 : 8, "UInt") }
, File := FileOpen(hPipeR, "h", Codepage)
, LineNum := 1, sOutput := ""
While (A_Args.RunCMD.PID + DllCall("Sleep", "Int",0))
and DllCall("PeekNamedPipe", "Ptr",hPipeR, "Ptr",0, "Int",0, "Ptr",0, "Ptr",0, "Ptr",0)
While A_Args.RunCMD.PID and (Line := File.ReadLine())
sOutput .= Fn ? Fn.Call(Line, LineNum++) : Line
A_Args.RunCMD.PID := 0
, hProcess := NumGet(PI, 0)
, hThread := NumGet(PI, A_PtrSize)
, DllCall("GetExitCodeProcess", "Ptr",hProcess, "PtrP",ExitCode:=0)
, DllCall("CloseHandle", "Ptr",hProcess)
, DllCall("CloseHandle", "Ptr",hThread)
, DllCall("CloseHandle", "Ptr",hPipeR)
, ErrorLevel := ExitCode
Return sOutput
}
I was searching for an alternative method that doesn't make use of PowerShell because this method takes more time to retrieve the data, and found @lexikos .NET Framework CLR lib
But I'm not being able to get it working, looking for any help on this task
Code: Select all
Lib := "C:\Users\Downloads\OpenHardwareMonitorLib.dll"
asm := CLR_LoadLibrary(Lib)
computer := CLR_CreateObject(asm, "OpenHardwareMonitor.Hardware.Computer")
computer.Open()
computer.CpuEnabled := 1
Hardwares := computer.Hardware
msgbox,% ComObjType(Hardwares[0]) ;return 9
msgbox,% ComObjType(Hardwares[0].HardwareType) ; Error: 0x80131509 - Specifically: HardwareType