Download Pelles C here: http://www.smorgasbordet.com/pellesc/
/*
boot_time_wmi_local.c
Display Windows boot time and uptime (local time)
Works with Pelles C, uses WMI (Win32_OperatingSystem.LastBootUpTime)
*/
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdio.h>
#include <wbemidl.h>
#include <wchar.h>
#pragma comment(lib, "wbemuuid.lib")
// Convert WMI "yyyymmddHHMMSS.mmmmmmsUUU" to SYSTEMTIME (local)
BOOL ParseWmiTime(LPCWSTR wmiTime, SYSTEMTIME *st)
{
if (!wmiTime || wcslen(wmiTime) < 14)
return FALSE;
swscanf(wmiTime, L"%4hu%2hu%2hu%2hu%2hu%2hu",
&st->wYear, &st->wMonth, &st->wDay,
&st->wHour, &st->wMinute, &st->wSecond);
st->wMilliseconds = 0;
return TRUE;
}
int main(void)
{
HRESULT hr;
IWbemLocator *pLoc = NULL;
IWbemServices *pSvc = NULL;
IEnumWbemClassObject *pEnum = NULL;
IWbemClassObject *pObj = NULL;
ULONG ret;
VARIANT vt;
SYSTEMTIME stBoot;
FILETIME ftBoot, ftNow;
ULONGLONG boot64, now64, diff64, diffSec;
ULONGLONG days, hrs, mins, secs;
// Initialize COM
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr)) {
printf("CoInitializeEx failed: 0x%lx\n", hr);
return 1;
}
// Initialize COM security
hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE, NULL);
if (FAILED(hr)) {
printf("CoInitializeSecurity failed: 0x%lx\n", hr);
CoUninitialize();
return 1;
}
// Create WMI locator
hr = CoCreateInstance(&CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
&IID_IWbemLocator, (LPVOID *)&pLoc);
if (FAILED(hr)) {
printf("CoCreateInstance failed: 0x%lx\n", hr);
CoUninitialize();
return 1;
}
// Connect to ROOT\CIMV2
hr = pLoc->lpVtbl->ConnectServer(pLoc,
L"ROOT\\CIMV2",
NULL, NULL, 0, 0, 0, 0,
&pSvc);
if (FAILED(hr)) {
printf("ConnectServer failed: 0x%lx\n", hr);
pLoc->lpVtbl->Release(pLoc);
CoUninitialize();
return 1;
}
// Set proxy security levels (cast to IUnknown for Pelles C)
hr = CoSetProxyBlanket((IUnknown *)pSvc,
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)) {
printf("CoSetProxyBlanket failed: 0x%lx\n", hr);
pSvc->lpVtbl->Release(pSvc);
pLoc->lpVtbl->Release(pLoc);
CoUninitialize();
return 1;
}
// Query LastBootUpTime
hr = pSvc->lpVtbl->ExecQuery(pSvc, L"WQL",
L"SELECT LastBootUpTime FROM Win32_OperatingSystem",
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL, &pEnum);
if (FAILED(hr)) {
printf("ExecQuery failed: 0x%lx\n", hr);
pSvc->lpVtbl->Release(pSvc);
pLoc->lpVtbl->Release(pLoc);
CoUninitialize();
return 1;
}
// Read result
hr = pEnum->lpVtbl->Next(pEnum, WBEM_INFINITE, 1, &pObj, &ret);
if (SUCCEEDED(hr) && ret) {
VariantInit(&vt);
hr = pObj->lpVtbl->Get(pObj, L"LastBootUpTime", 0, &vt, 0, 0);
if (SUCCEEDED(hr) && vt.vt == VT_BSTR) {
if (ParseWmiTime(vt.bstrVal, &stBoot)) {
SystemTimeToFileTime(&stBoot, &ftBoot);
printf("Boot time (local): %04u-%02u-%02u %02u:%02u:%02u\n",
stBoot.wYear, stBoot.wMonth, stBoot.wDay,
stBoot.wHour, stBoot.wMinute, stBoot.wSecond);
// Compute uptime
GetSystemTimeAsFileTime(&ftNow);
boot64 = ((ULONGLONG)ftBoot.dwHighDateTime << 32) | ftBoot.dwLowDateTime;
now64 = ((ULONGLONG)ftNow.dwHighDateTime << 32) | ftNow.dwLowDateTime;
if (now64 > boot64) {
diff64 = now64 - boot64;
diffSec = diff64 / 10000000ULL;
days = diffSec / 86400;
hrs = (diffSec % 86400) / 3600;
mins = (diffSec % 3600) / 60;
secs = diffSec % 60;
printf("Uptime: %llu days, %llu hours, %llu minutes, %llu seconds\n",
days, hrs, mins, secs);
}
} else {
printf("Could not parse WMI time.\n");
}
} else {
printf("Failed to read LastBootUpTime property.\n");
}
VariantClear(&vt);
pObj->lpVtbl->Release(pObj);
}
// Cleanup
pEnum->lpVtbl->Release(pEnum);
pSvc->lpVtbl->Release(pSvc);
pLoc->lpVtbl->Release(pLoc);
CoUninitialize();
return 0;
}
Maybe I'll fix it for the next activity.QuoteI bought an very cheap HP laptop with Intel® Core™ i3-N305 processor, so i can test code with it too.
Quote from: Vortex on November 21, 2025, 08:03:44 AMKindly, could you share your inspection results with us?
Page created in 0.048 seconds with 15 queries.