Skip to content

Commit

Permalink
v1.1.1
Browse files Browse the repository at this point in the history
Use Performace Counter PDH APIs instead of WMI to get temps from ACPI Thermal zone. Actually updates and no admin required
  • Loading branch information
driver1998 committed Sep 19, 2020
1 parent 6920f15 commit 4adbb15
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/PiMon.rc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ BEGIN
DEFPUSHBUTTON "&OK", IDOK, 105, 130, 50, 15
PUSHBUTTON "&Cancel", IDCANCEL, 160, 130, 50, 15
GROUPBOX "Data Source", IDC_STATIC, 10, 10, 200, 35
CONTROL "Use ACPI Thermal Zone for temperature\n (Admin Required)", IDC_ACPI_THERMAL, "Button", BS_AUTOCHECKBOX | WS_TABSTOP | BS_MULTILINE, 20, 20, 180, 20
CONTROL "Use ACPI Thermal Zone for temperature", IDC_ACPI_THERMAL, "Button", BS_AUTOCHECKBOX | WS_TABSTOP, 20, 20, 180, 20
GROUPBOX "Tray Icon Temperature Meter", IDC_STATIC, 10, 55, 200, 65
CTEXT "Preview", IDC_PREVIEW, 20, 70, 72, 12, SS_CENTERIMAGE
PUSHBUTTON "&Foreground", IDC_FOREGROUND, 20, 85, 72, 12
Expand Down
12 changes: 6 additions & 6 deletions src/PiMon.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>wbemuuid.lib;Pathcch.lib;comctl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>pdh.lib;Pathcch.lib;comctl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Manifest>
<SuppressStartupBanner>false</SuppressStartupBanner>
Expand All @@ -173,7 +173,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>wbemuuid.lib;Pathcch.lib;comctl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>pdh.lib;Pathcch.lib;comctl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Manifest>
<SuppressStartupBanner>false</SuppressStartupBanner>
Expand All @@ -194,7 +194,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>wbemuuid.lib;Pathcch.lib;comctl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>pdh.lib;Pathcch.lib;comctl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Manifest>
<SuppressStartupBanner>false</SuppressStartupBanner>
Expand All @@ -220,7 +220,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>wbemuuid.lib;Pathcch.lib;comctl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>pdh.lib;Pathcch.lib;comctl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Manifest>
<SuppressStartupBanner>false</SuppressStartupBanner>
Expand All @@ -245,7 +245,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>wbemuuid.lib;Pathcch.lib;comctl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>pdh.lib;Pathcch.lib;comctl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Manifest>
<SuppressStartupBanner>false</SuppressStartupBanner>
Expand All @@ -270,7 +270,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>wbemuuid.lib;Pathcch.lib;comctl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>pdh.lib;Pathcch.lib;comctl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<Manifest>
<SuppressStartupBanner>false</SuppressStartupBanner>
Expand Down
113 changes: 57 additions & 56 deletions src/data.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#include <windows.h>
#include <strsafe.h>
#include <wbemidl.h>
#include <pdh.h>
#include "rpiq.h"
#include "utils.h"
#include "data.h"

#ifdef _DEBUG
#define DEBUGLOG(x, ...) wprintf(x, __VA_ARGS__)
#else
#define DEBUGLOG(x, ...)
#endif

static const wchar_t* RpiProcessorName[] = {
[CPU_UNKNOWN] = L"Unknown",
[CPU_BCM2835] = L"BCM2835 (ARM11)",
Expand Down Expand Up @@ -272,65 +278,60 @@ wchar_t* GetWindowsVersion() {
}


ULONG GetTemperatureWmi() {
HRESULT hr;
ULONG value = 0;
ULONG GetTemperatureAcpi() {
LONGLONG value = 0;
ULONG status;

DWORD queryId = 0;
PDH_HQUERY hQuery = NULL;
status = PdhOpenQueryW(NULL, (DWORD_PTR)&queryId, &hQuery);
if (status != ERROR_SUCCESS) {
DEBUGLOG(L"PdhOpenQuery() Failed, status=%08x", status);
goto err0;
}

DWORD size = 0;
wchar_t* pathList = NULL;
const wchar_t* wildcardPath = L"\\Thermal Zone Information(*)\\High Precision Temperature";
PdhExpandWildCardPathW(NULL, wildcardPath, pathList, &size, 0);
pathList = malloc(sizeof(wchar_t) * size);
if (pathList == NULL) goto err1;

PdhExpandWildCardPathW(NULL, wildcardPath, pathList, &size, 0);
if (status != ERROR_SUCCESS) {
DEBUGLOG(L"PdhExpandWildCardPath() Failed, status=%08x", status);
goto err2;
}

// Obtain the initial locator to WMI
IWbemLocator* locator = NULL;
hr = CoCreateInstance(&CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, &IID_IWbemLocator, (LPVOID*)&locator);
if (FAILED(hr)) goto err0;

// Connect to WMI through the IWbemLocator::ConnectServer method
BSTR ns = SysAllocString(L"ROOT\\WMI");
IWbemServices* services = NULL;
hr = locator->lpVtbl->ConnectServer(locator, ns, NULL, NULL, NULL, 0, NULL, NULL, &services);
if (FAILED(hr)) goto err1;

// Set security levels on the proxy
hr = CoSetProxyBlanket((IUnknown*)services, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
if (FAILED(hr)) goto err2;

// Start the WMI query
BSTR lang = SysAllocString(L"WQL");
BSTR query = SysAllocString(L"SELECT CurrentTemperature FROM MSAcpi_ThermalZoneTemperature WHERE Active=True");
IEnumWbemClassObject* enumerator = NULL;
hr = services->lpVtbl->ExecQuery(services, lang, query, WBEM_FLAG_BIDIRECTIONAL, NULL, &enumerator);
if (FAILED(hr)) goto err3;

// Get the data from the query
IWbemClassObject* obj = NULL;
ULONG ret = 0;
if (enumerator) {

// Raspberry Pi only have one thermal zone
hr = enumerator->lpVtbl->Next(enumerator, WBEM_INFINITE, 1, &obj, &ret);

if (ret != 0) {
VARIANT temp;
obj->lpVtbl->Get(obj, L"CurrentTemperature", 0, &temp, NULL, NULL);
value = temp.ulVal;
VariantClear(&temp);

obj->lpVtbl->Release(obj);
}

enumerator->lpVtbl->Release(enumerator);
// Raspberry Pi only have one thermal zone
DWORD counterId = 0;
PDH_HCOUNTER hCounter = NULL;
const wchar_t* path = pathList;
status = PdhAddCounterW(hQuery, path, (DWORD_PTR)&counterId, &hCounter);
if (status != ERROR_SUCCESS) {
DEBUGLOG(L"PdhAddCounter() Failed, status=%08x", status);
goto err2;
}

// Cleanup
err3:
if (lang) SysFreeString(lang);
if (query) SysFreeString(query);
status = PdhCollectQueryData(hQuery);
if (status != ERROR_SUCCESS) {
DEBUGLOG(L"PdhCollectQueryData() Failed, status=%08x", status);
goto err2;
}

PDH_RAW_COUNTER raw = { 0 };
DWORD type = PERF_COUNTER_RAWCOUNT;
status = PdhGetRawCounterValue(hCounter, NULL, &raw);
if (status != ERROR_SUCCESS) {
DEBUGLOG(L"PdhGetRawCounterValue() Failed, status=%08x", status);
goto err2;
}
value = raw.FirstValue;

err2:
if (services)
services->lpVtbl->Release(services);
if (pathList != NULL) free(pathList);
err1:
if (ns) SysFreeString(ns);
if (locator)
locator->lpVtbl->Release(locator);
if (hQuery != NULL) PdhCloseQuery(hQuery);
err0:
return value;
return (ULONG)value;
}
3 changes: 1 addition & 2 deletions src/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ULONG GetBoardRevision();
ULONG GetVoltage(int voltageId);
ULONG GetClock(int clockId);
ULONG GetTemperature();
ULONG GetTemperatureWmi();
ULONG GetTemperatureAcpi();
ULONG GetFirmwareRevision();
ULONGLONG GetSerialNumber();
ULONG GetWindowsMemory();
Expand All @@ -43,7 +43,6 @@ enum RpiModel GetPiModel(ULONG BoardRevision);
const wchar_t* GetProcessorName(ULONG BoardRevision);
const wchar_t* GetPiModelName(ULONG BoardRevision);


// Free the string after use
wchar_t* GetBiosVersion();
wchar_t* GetWindowsVersion();
Expand Down
44 changes: 10 additions & 34 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,6 @@ void RpiqError(HWND hwnd) {
#endif
}

void RestartAsAdmin(int nCmdShow) {
if (!IsUserAnAdmin()) {
wchar_t path[MAX_PATH];
GetModuleFileNameW(NULL, path, MAX_PATH);
ShellExecuteW(NULL, L"runas", path, NULL, NULL, nCmdShow);
PostQuitMessage(0);
}
}

void GetInfo(HWND hwnd) {
wchar_t str[200];
ULONG rev = GetBoardRevision();
Expand Down Expand Up @@ -145,21 +136,12 @@ void UpdateData(HWND hwnd, BOOL force) {

// Temperature
{
ULONG value;
if (acpiThermal)
value = GetTemperatureWmi();
else
value = GetTemperature();

ULONG value = acpiThermal ? GetTemperatureAcpi() : GetTemperature();

if (value != tempItems[0].data || force) {
tempItems[0].data = value;

double temp;
if (acpiThermal)
temp = value / 10.0 - 273.15;
else
temp = value / 1000.0;

double temp = acpiThermal ? (value - 2732) / 10.0 : value / 1000.0;

HICON icon = DrawTrayIcon(hwnd, trayBackground, trayForeground, temp);
StringCbPrintfW(str, sizeof(str), L"Temperature: %.3lg \u2103", temp);
UpdateTrayIcon(hwnd, TRAYICON_ID, icon, str);
Expand Down Expand Up @@ -208,7 +190,6 @@ void About(HWND hwnd) {
void SettingsDialog(HWND hwnd) {
HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtrW(hwnd, GWLP_HINSTANCE);
DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_CONFIG), hwnd, (DLGPROC)SettingsDialogProc);
if (acpiThermal) RestartAsAdmin(SW_SHOWNORMAL);
UpdateData(hwnd, TRUE);
}

Expand Down Expand Up @@ -448,16 +429,12 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi

LoadConfig();

// WMI requires administrator privileges...
if (acpiThermal) RestartAsAdmin(nCmdShow);

// Initialize COM for WMI
HRESULT hr;
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr)) return hr;
hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
if (FAILED(hr)) return hr;
#ifdef _DEBUG
FILE* stream;
AllocConsole();
_wfreopen_s(&stream, L"CONOUT$", L"w", stdout);
wprintf(PIMON_APPNAME L" v" PIMON_VERSION L"\n");
#endif

// Register the Explorer restart message
WM_TASKBAR_CREATE = RegisterWindowMessageW(L"TaskbarCreated");
Expand Down Expand Up @@ -495,6 +472,5 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi

KillTimer(hwnd, TIMER_ID);
DeleteTrayIcon(hwnd, TRAYICON_ID);
CoUninitialize();
return 0;
}
7 changes: 0 additions & 7 deletions src/setting.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ INT_PTR CALLBACK SettingsDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM

static HWND hwndPreview;
static HWND hwndAcpiThermal;
static HWND hwndOK;
static HBRUSH bkgBrush = NULL;

CHOOSECOLORW c = { 0 };
Expand All @@ -38,18 +37,13 @@ INT_PTR CALLBACK SettingsDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
MoveWindowToCenterOfScreen(hwnd);
hwndPreview = GetDlgItem(hwnd, IDC_PREVIEW);
hwndAcpiThermal = GetDlgItem(hwnd, IDC_ACPI_THERMAL);
hwndOK = GetDlgItem(hwnd, IDOK);

tmpForeground = trayForeground;
tmpBackground = trayBackground;
tmpAcpiThermal = acpiThermal;
bkgBrush = CreateSolidBrush(tmpBackground);

Button_SetCheck(hwndAcpiThermal, tmpAcpiThermal ? BST_CHECKED : BST_UNCHECKED);

if (GetProcessorType(GetBoardRevision()) != CPU_BCM2711)
Button_Enable(hwndAcpiThermal, FALSE);

return TRUE;
case WM_CTLCOLORSTATIC:
if ((HWND)lParam == hwndPreview) {
Expand Down Expand Up @@ -81,7 +75,6 @@ INT_PTR CALLBACK SettingsDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
return TRUE;
case IDC_ACPI_THERMAL:
tmpAcpiThermal = (Button_GetCheck(hwndAcpiThermal) == BST_CHECKED);
Button_SetElevationRequiredState(hwndOK, tmpAcpiThermal && !acpiThermal && !IsUserAnAdmin());
return TRUE;
case IDOK:
trayForeground = tmpForeground;
Expand Down
6 changes: 3 additions & 3 deletions src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

#ifdef _UNICODE
#define PIMON_APPNAME L"PiMon"
#define PIMON_VERSION L"1.1.0"
#define PIMON_VERSION L"1.1.1"
#else
#define PIMON_APPNAME "PiMon"
#define PIMON_VERSION "1.1.0"
#define PIMON_VERSION "1.1.1"
#endif

#define PIMON_FILE_VERSION 1,1,0,0
#define PIMON_FILE_VERSION 1,1,1,0

#endif

0 comments on commit 4adbb15

Please sign in to comment.