Pelles C forum

C language => Tips & tricks => Topic started by: TimoVJL on October 21, 2015, 01:30:39 pm

Title: Processor and speed from registry
Post by: TimoVJL on October 21, 2015, 01:30:39 pm
Code: [Select]
#define UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define TESTCON

#ifdef TESTWIN
#pragma comment(lib, "kernel32.lib user32.lib advapi32.lib")
#pragma comment(linker, "-subsystem:windows")
#elif defined(TESTCON)
#pragma comment(lib, "kernel32.lib user32.lib advapi32.lib")
#pragma comment(linker, "-subsystem:console")
#else
#pragma comment(lib, "advapi32.lib")
#endif

unsigned int GetProcNameString(TCHAR *szProc)
{
HKEY hKey;

if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"), 0, KEY_READ, &hKey))
{
DWORD dwSize = 50*sizeof(TCHAR); //3*4*4+1
if (RegQueryValueEx(hKey, TEXT("ProcessorNameString"), NULL, NULL, (LPBYTE)szProc, &dwSize) == ERROR_SUCCESS)
{
RegCloseKey(hKey);
#ifdef UNICODE
return dwSize/sizeof(TCHAR);
#else
return dwSize;
#endif
}
RegCloseKey(hKey);
}
return 0;
}

unsigned int GetProcSpeed(void)
{
HKEY hKey;

if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"), 0, KEY_READ, &hKey))
{
DWORD dwProcSpeed = 0;
DWORD dwSize = sizeof(dwProcSpeed);

if (RegQueryValueEx(hKey, TEXT("~MHz"), NULL, NULL, (LPBYTE)&dwProcSpeed, &dwSize) == ERROR_SUCCESS)
{
RegCloseKey(hKey);
return dwProcSpeed;
}

RegCloseKey(hKey);
}
return 0;
}

#ifdef TESTWIN
void __cdecl WinMainCRTStartup(void)
{
TCHAR szMsg[100];
DWORD dwSpeed = GetProcSpeed();
int nLen = GetProcNameString(szMsg);
if (dwSpeed < 1000)
wsprintf(&szMsg[nLen - 1], TEXT(" %u MHz"), dwSpeed);
else {
wsprintf(&szMsg[nLen - 1], TEXT("  %u GHz"), dwSpeed);
szMsg[nLen] = szMsg[nLen + 1];
szMsg[nLen + 1] = '.';
}
MessageBox(0, szMsg, 0, MB_OK);
ExitProcess(0);
}
#endif
#ifdef TESTCON
void __cdecl mainCRTStartup(void)
{
TCHAR szMsg[100];
unsigned long dwSpeed = GetProcSpeed();
int nLen = GetProcNameString(szMsg);
int nLen2;
if (dwSpeed < 1000)
nLen2 = wsprintf(&szMsg[nLen - 1], TEXT(" %u MHz\n"), dwSpeed);
else {
nLen2 = wsprintf(&szMsg[nLen - 1], TEXT("  %u GHz\n"), dwSpeed);
szMsg[nLen] = szMsg[nLen + 1];
szMsg[nLen + 1] = '.';
}
unsigned long dwWrite;
WriteConsole(GetStdHandle(-11), szMsg, nLen+nLen2, &dwWrite, NULL);
ExitProcess(0);
}
#endif
Take 2
Code: [Select]
#define UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

//#define TESTWIN
#define TESTCON

#ifdef TESTWIN
#pragma comment(lib, "kernel32.lib user32.lib advapi32.lib")
#pragma comment(linker, "-subsystem:windows,5.1")
#elif defined(TESTCON)
#pragma comment(lib, "kernel32.lib user32.lib advapi32.lib")
#pragma comment(linker, "-subsystem:console,5.1")
#else
#pragma comment(lib, "advapi32.lib")
#endif

unsigned int GetProcNameString(TCHAR *szProc)
{
HKEY hKey;

if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"), 0, KEY_READ, &hKey))
{
DWORD dwSize = 50*sizeof(TCHAR); //3*4*4+1
if (RegQueryValueEx(hKey, TEXT("ProcessorNameString"), NULL, NULL, (LPBYTE)szProc, &dwSize) == ERROR_SUCCESS)
{
RegCloseKey(hKey);
#ifdef UNICODE
return dwSize/sizeof(TCHAR);
#else
return dwSize;
#endif
}
RegCloseKey(hKey);
}
return 0;
}

unsigned int GetProcSpeed(void)
{
HKEY hKey;

if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"), 0, KEY_READ, &hKey))
{
DWORD dwProcSpeed = 0;
DWORD dwSize = sizeof(dwProcSpeed);

if (RegQueryValueEx(hKey, TEXT("~MHz"), NULL, NULL, (LPBYTE)&dwProcSpeed, &dwSize) == ERROR_SUCCESS)
{
RegCloseKey(hKey);
return dwProcSpeed;
}

RegCloseKey(hKey);
}
return 0;
}

unsigned int GetProdNameString(TCHAR *szProd, int nLen)
{
HKEY hKey;

if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"), 0, KEY_READ, &hKey))
{
DWORD dwSize = nLen;
if (RegQueryValueEx(hKey, TEXT("ProductName"), NULL, NULL, (LPBYTE)szProd, &dwSize) == ERROR_SUCCESS)
{
RegCloseKey(hKey);
#ifdef UNICODE
return dwSize/sizeof(TCHAR);
#else
return dwSize;
#endif
}
RegCloseKey(hKey);
}
return 0;
}

#ifdef TESTWIN
void __cdecl WinMainCRTStartup(void)
{
TCHAR szMsg[100];
DWORD dwSpeed = GetProcSpeed();
int nLen = GetProcNameString(szMsg);
int nLen2;
if (dwSpeed < 1000)
nLen2 = wsprintf(&szMsg[nLen - 1], TEXT(" %u MHz, "), dwSpeed);
else {
nLen2 = wsprintf(&szMsg[nLen - 1], TEXT("  %u GHz, "), dwSpeed);
szMsg[nLen] = szMsg[nLen + 1];
szMsg[nLen + 1] = '.';
}
GetProdNameString(&szMsg[nLen+nLen2-1], sizeof(szMsg));
MessageBox(0, szMsg, 0, MB_OK);
ExitProcess(0);
}
#endif
#ifdef TESTCON
void __cdecl mainCRTStartup(void)
{
TCHAR szMsg[100];
unsigned long dwSpeed = GetProcSpeed();
int nLen = GetProcNameString(szMsg);
int nLen2;
if (dwSpeed < 1000)
nLen2 = wsprintf(&szMsg[nLen - 1], TEXT(" %u MHz, "), dwSpeed);
else {
nLen2 = wsprintf(&szMsg[nLen - 1], TEXT("  %u GHz, "), dwSpeed);
szMsg[nLen] = szMsg[nLen + 1];
szMsg[nLen + 1] = '.';
}
unsigned long dwWrite;
//WriteConsole(GetStdHandle(-11), szMsg, nLen+nLen2, &dwWrite, NULL);
nLen2 += GetProdNameString(&szMsg[nLen+nLen2-1], sizeof(szMsg));
szMsg[nLen+nLen2++] = 13;
szMsg[nLen+nLen2] = 10;
WriteConsole(GetStdHandle(-11), szMsg, nLen+nLen2, &dwWrite, NULL);
ExitProcess(0);
}
#endif
GetPhysMem()
Code: [Select]
#define UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>

#pragma pack(push, 4)
typedef struct {
UCHAR  Type;
UCHAR  ShareDisposition;
USHORT Flags;
ULONGLONG Start;
ULONG Length;
} MEMORY, *PMEMORY;
#pragma pack(pop)

LONGLONG GetMemResources(LPBYTE pData)
{
LONGLONG llMem = 0;
LPBYTE pPtr;
PDWORD pDW;
pDW = (PDWORD)pData;
if (*pDW != 1) return 0;
DWORD nCnt = *(PDWORD)(pData + 0x10); // Count
pPtr = pData + 0x14;
DWORD nRLen = 0;
if (*(pData + 0x14) == *(pData + 0x24)) nRLen = 16;
if (*(pData + 0x14) == *(pData + 0x28)) nRLen = 20;
PMEMORY pMem;
for (DWORD nIdx = 0; nRLen, nIdx < nCnt; nIdx++) {
pMem = (PMEMORY)(pPtr + nRLen * nIdx);
if (pMem->Type == 3) llMem += pMem->Length;
if (pMem->Type == 7 && pMem->Flags == 0x200) llMem += ((LONGLONG)pMem->Length)<<8;
pMem += nRLen;
}
return llMem;
}

LONGLONG GetPhysMem(void)
{
HKEY hKey;
DWORD dwType;
DWORD dwSize;
LPBYTE pData;
LONGLONG llMem = 0;

if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("HARDWARE\\RESOURCEMAP\\System Resources\\Physical Memory"), 0, KEY_READ, &hKey))
{
if (!RegQueryValueEx(hKey, TEXT(".Translated"), NULL, &dwType, NULL, &dwSize))
{
HANDLE hHeap = GetProcessHeap();
pData = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, dwSize);
RegQueryValueEx(hKey, TEXT(".Translated"), NULL, &dwType, pData, &dwSize);
llMem = GetMemResources(pData);
HeapFree(hHeap, HEAP_ZERO_MEMORY, pData);
}
RegCloseKey(hKey);
}
return llMem;
}
Title: Re: Processor and speed from registry
Post by: jj2007 on October 21, 2015, 03:09:21 pm
#define UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define TEST
#pragma comment(linker, "advapi32.lib /Subsystem:Windows" )

unsigned int GetProcNameString(TCHAR *szProc)

Compiles much better  ;)
Title: Re: Processor and speed from registry
Post by: TimoVJL on October 21, 2015, 06:12:37 pm
Thanks ;)
Only test program should define that -subsystem:windows