Home | API | MFC | C++ | C | Previous

Programming Windows API

System Information Functions

The Win32 API provides system information data about the environment under which the application is running. This information includes the process environment variables, the time, default locale settings for the system and user, system colour settings, drive information, system parameters, OS information and processor type and the computer name. A small selection of these functions are listed below.

For a full description of these functions – https://docs.microsoft.com/en-us/windows/win32/sysinfo/system-information-functions

GetComputerName

Returns the systems computer name

BOOL GetComputerName(LPSTR lpBuffer,LPDWORD nSize);

Where
LpBuffer is a pointer to a buffer that receives the computer name or the cluster virtual server name and nSize specifies the size of the buffer.If the function succeeds, the return value is a nonzero value. If the function fails, the return value is zero.

GetSystemDirectory

Retrieves the path of the system directory.

UINT GetSystemDirectoryA(LPSTR lpBuffer,UINT uSize);

Where
PBuffer is a pointer to the buffer to receive the path and uSize is the maximum size of the buffer. If the function succeeds, the return value is the length. If the function fails, the return value is zero.

GetCurrentDirectory

Retrieves the current directory for the current process.

DWORD GetCurrentDirectory(DWORD nBufferLength,LPTSTR lpBuffer);

Where nBufferLength is the length of the buffer for the current directory string and lpBuffer is a pointer to the buffer that receives the current directory string. If the function succeeds, the return value specifies the number of characters that are written to the buffer, not including the terminating null character. If the function fails, the return value is zero.

GetEnvironmentVariable

Retrieves the contents of the specified variable from the environment block of the calling process.

DWORD GetEnvironmentVariable(LPCTSTR lpName,LPTSTR lpBuffer,DWORD nSize);

Where
lpName – The name of the environment variable.
lpBuffer – A pointer to a buffer that receives the contents of the specified environment variable as a null-terminated string..
nSize – The size of the buffer pointed to by the lpBuffer parameter, including the null-terminating character, in characters.
If the function succeeds, the return value is the number of characters stored in the buffer pointed to by. If the function fails, the return value is zero.

GetLocalTime

Retrieves the current local date and time.

void GetLocalTime(LPSYSTEMTIME lpSystemTime);

where lpSystemTime is a pointer to a SYSTEMTIME structure to receive the current local date and time.

Example


#include <windows.>
#include <tchar.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc = {0};
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = TEXT("myWindowClass");
RegisterClassEx(&wc);
CreateWindowEx(WS_EX_CLIENTEDGE,TEXT("myWindowClass"),TEXT("System info"),WS_THICKFRAME|WS_OVERLAPPEDWINDOW| WS_VISIBLE | WS_OVERLAPPEDWINDOW ,CW_USEDEFAULT, CW_USEDEFAULT, 340, 320, NULL, NULL, hInstance, NULL);
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT Ps;
HDC hdc;
switch(msg)
{
case WM_PAINT://traps paint message
{
hdc = BeginPaint(hwnd, &Ps);
TCHAR OutputText[128]="Machine Name -- ";
TCHAR SystemInfo[MAX_PATH];
DWORD buf = 256;
int stringlength;
GetComputerName(SystemInfo, &buf);
_tcscat(OutputText,SystemInfo);
stringlength=_tcslen(OutputText);
TextOut(hdc, 0,0, OutputText,stringlength);

GetSystemDirectory(SystemInfo,MAX_PATH);
_tcscpy(OutputText,"Windows Directory -- ");
_tcscat(OutputText,SystemInfo);
stringlength=_tcslen(OutputText);
TextOut(hdc, 0,15,OutputText,stringlength);

_tcscpy(OutputText,"Current Directory -- ");
GetCurrentDirectory(MAX_PATH,SystemInfo);
_tcscat(OutputText,SystemInfo);
stringlength=_tcslen(OutputText);
TextOut(hdc, 0,30,OutputText,stringlength);

buf = 256;
GetEnvironmentVariable("ProgramFiles", SystemInfo, buf);
_tcscpy(OutputText,"Program Files directory -- ");
_tcscat(OutputText,SystemInfo);
stringlength=_tcslen(OutputText);
TextOut(hdc, 0,45,OutputText,stringlength);


GetEnvironmentVariable("OS", SystemInfo, buf);
_tcscpy(OutputText,"Current OS -- ");
_tcscat(OutputText,SystemInfo);
stringlength=_tcslen(OutputText);
TextOut(hdc, 0,60,OutputText,stringlength);


TCHAR datevalue[32];
TCHAR month[32];
TCHAR year[32];
SYSTEMTIME lpSystemTime;
GetLocalTime(&lpSystemTime);
_tcscpy(OutputText,"Current Date -- ");
wsprintf(SystemInfo,TEXT("%i"), lpSystemTime.wDay);
_tcscat(OutputText,SystemInfo);
_tcscat(OutputText,".");
wsprintf(SystemInfo,TEXT("%i"), lpSystemTime.wMonth);
_tcscat(OutputText,SystemInfo);
_tcscat(OutputText,".");
wsprintf(SystemInfo,TEXT("%i"), lpSystemTime.wYear);
_tcscat(OutputText,SystemInfo);
stringlength=_tcslen(OutputText);
TextOut(hdc, 0,75,OutputText,stringlength);
EndPaint(hwnd, &Ps);
DeleteDC(hdc);
}
break;

case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

Download


Home | API | MFC | C++ | C | Previous
Creating a Simple Window | Common Elements | Data Types and Character Sets | The Device Context | Graphics Device Interface | Displaying Text | Displaying Graphics | Mapping Modes | Keyboard Input | Working with the Mouse | Menus | Child Windows | ScrollBar Control | The Dialog Box | Windows Message Box | Common Dialog Box | Bitmaps | Common Controls | Creating a Toolbar | Multiple Document Interface | Timers | DLL’s | Creating Custom Controls | Owner Drawn Controls | API Hooking and DLL Injection | File Management Functions | String Manipulation | System Information Functions