Another Basic Windows System Information Program Example
Create a new Win32 console application project and give a suitable project name.
Add the source file and give a suitable name.
Add the following source code.
// Extracting some computer information program example
#include<windows.h>
#include<stdio.h>
#define TOTALBYTES 2048
void PrintSystemName()
{
WCHAR compName[40];
DWORD size = 40;
if(GetComputerName(compName,&size)!=0)
wprintf(LComputer Name : %s ,compName);
}
void PrintUserName()
{
WCHAR uName[40];
DWORD size = 40;
if(GetUserName(uName,&size) != 0)
wprintf(L\nLogged user name : %s,uName);
}
void PrintSystemUpTime()
{
unsigned int t,d,h,m,s;
// Retrieves the number of milliseconds that have elapsed since
// the system was started, up to 49.7 days.
t = GetTickCount();
// Get the seconds
t/=1000;
//days
d = t/86400;
t = t%86400;
// hours
h = t/3600;
t = t%3600;
// minutes
m = t/60;
t = t%60;
// seconds
s = t;
wprintf(L\nSystem Up Time(D:H:M:S) = %u:%u:%u:%u,d,h,m,s);
}
void PrintDrivesInfo()
{
int drives,i=0;
__int64 nFree, nTotal, nHDFree=0, nHDTotal=0;
WCHAR dName[40],volName[40];
wprintf(L\n\nHard Disk Info:);
wprintf(L\n---------------);
// Returns the drive bitmasks
drives = GetLogicalDrives();
// Iterate all the available drives
while(drives != 0)
{
// Do the logical AND
if((drives&1) == 1)
{
// Iterate starting from drive A
wsprintf(dName,L%c:\\,'A'+i);
// Retrieves information about the amount of space that is available on a
// disk volume, which is the total amount of space, the total amount of
// free space, and the total amount of free space available to the user
// that is associated with the calling thread.
if(GetDiskFreeSpaceEx(dName,(PULARGE_INTEGER)&nFree,(PULARGE_INTEGER)&nTotal,NULL)!= 0)
{
wprintf(L\n%s,dName);
nHDFree += nFree;
nHDTotal += nTotal;
wprintf(L Free : %6.2I64fGB Total : %6.2I64fGB , nFree/(1024*1024*1024.0),nTotal/(1024*1024*1024.0));
// Retrieves information about the file system and volume
// associated with the specified root directory.
if(GetVolumeInformation(
dName,
NULL,
0,
NULL,
NULL,
NULL,
volName,
sizeof(volName)) !=0)
wprintf(L%s,volName);
}
}
drives>>=1;
i++;
}
// Total storage and free, uncomment to see it
// wprintf(L\n===========================================);
// wprintf(L\n Free : %6.2I64fGB Total : %6.2I64fGB,nHDFree/(1024*1024*1024.0),nHDTotal/(1024*1024*1024.0));
// wprintf(L\n===========================================);
}
void PrintMonitorResolution()
{
int width,height;
// GetSystemMetrics() function retrieves the dimensions — widths and heights — of
// Windows display elements and system configuration settings in pixels
width = GetSystemMetrics(SM_CXSCREEN);
height = GetSystemMetrics(SM_CYSCREEN);
wprintf(L\n\nMonitor Resolution : %dx%d,width,height);
}
void PrintOSInfo()
{
WCHAR windirName[MAX_PATH];
OSVERSIONINFO verInfo = {sizeof(OSVERSIONINFO)};
wprintf(L\n\nOS Info: );
wprintf(L\n--------);
wprintf(L\nVersion : );
// Retrieves information about the current operating system.
GetVersionEx(&verInfo);
if(verInfo.dwMajorVersion == 4 && verInfo.dwMinorVersion == 10)
wprintf(L Windows 98 %s,verInfo.szCSDVersion);
if(verInfo.dwMajorVersion == 5 && verInfo.dwMinorVersion == 0)
wprintf(L Windows 2000 %s,verInfo.szCSDVersion);
if(verInfo.dwMajorVersion == 5 && verInfo.dwMinorVersion == 1)
wprintf(L Windows XP %s,verInfo.szCSDVersion);
if(verInfo.dwMajorVersion == 5 && verInfo.dwMinorVersion == 2)
wprintf(L Windows 2003 %s,verInfo.szCSDVersion);
// Get Windows directory
GetWindowsDirectory(windirName,55);
wprintf(L\nWindows Directory : %s ,windirName);
// Get Windows system directory
GetSystemDirectory(windirName,55);
wprintf(L\nSystem Directory : %s ,windirName);
// Get the Windows temporary directory
GetTempPath(MAX_PATH,windirName);
wprintf(L\nTemp Directory : %s \n,windirName);
}
void PrintProcessorInfo()
{
// Pointer to HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor
HKEY hKey,tempKey;
WCHAR subKeyName[40];
DWORD BufferSize = TOTALBYTES;
LPBYTE valBuf = (LPBYTE)malloc(BufferSize);
int i=0,t;
DWORD size=100;
wprintf(L\n\nProcessor Info:);
wprintf(L\n---------------);
// Embedding the assembly in C/C++ code
__asm
{
// 01h for getting number of core present in the physical processor
mov eax,01h
cpuid
mov t,ebx
}
wprintf(L\nNumber of logical processors(cores) : %d,(t>>16)&0xff);
// Read the processor from registry
// Open the registry key
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
LHARDWARE\\DESCRIPTION\\System\\CentralProcessor,
0,
KEY_READ,
&hKey)== ERROR_SUCCESS)
{
// Enumerate the keys
while(RegEnumKey(hKey,i++,subKeyName,40) != ERROR_NO_MORE_ITEMS)
{
if(RegOpenKeyEx(hKey,subKeyName,0,KEY_READ,&tempKey) == ERROR_SUCCESS)
{
size = 100;
// Retrieves the type and data for the ProcessorNameString.
if(RegQueryValueEx(tempKey,
LProcessorNameString,
NULL,
NULL,
valBuf,
&size) == ERROR_SUCCESS)
wprintf(L\nProcessor %s :\n %s,subKeyName,valBuf);
size = 100;
// Retrieves the type and data for the Identifier.
if(RegQueryValueEx(
tempKey,
LIdentifier,
NULL,
NULL,
valBuf,
&size) == ERROR_SUCCESS)
wprintf(L %s,valBuf);
RegCloseKey(tempKey);
}
}
RegCloseKey(hKey);
}
free(valBuf);
}
void PrintMemoryInfo()
{
MEMORYSTATUSEX ms={sizeof(MEMORYSTATUSEX)};
// Retrieves information about the system's current
// usage of both physical and virtual memory.
GlobalMemoryStatusEx(&ms);
wprintf(L\n\nMemory Info:);
wprintf(L\n------------);
wprintf(L\nTotal Physical Memory : %8.2I64fMB \nAvailable Physical Memory : %8.2I64fMB \nUsed Physical Memory : %8.2I64fMB \n\n,ms.ullTotalPhys/(1024*1024.0),ms.ullAvailPhys/(1024*1024.0),ms.ullTotalPhys/(1024*1024.0)-ms.ullAvailPhys/(1024*1024.0));
}
int wmain(int argc, WCHAR **argv)
{
wprintf(LSYSTEM INFORMATION SAMPLE\n-------------------\n);
PrintSystemName();
PrintUserName();
PrintSystemUpTime();
PrintDrivesInfo();
PrintMonitorResolution();
PrintOSInfo();
PrintProcessorInfo();
PrintMemoryInfo();
wprintf(L\nCurrently Running Processes:\n----------------------------);
// Just ask tasklist command
system(tasklist);
}
Build and run the project. The following screenshot is an output sample.