Posted: . At: 9:28 AM. This was 2 years ago. Post ID: 15938
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Simple Windows system information program.


This is a very simple Windows System information program that will print some useful system information about the Windows system you are using. This might be very helpful if you are programming on Windows with Visual Studio.

sysinfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 
Simple program to print out information about a Windows system. This will print:
 
Computer name,
User name:
System directory,
Windows directory,
and the free and total space on Drive C:\.
 
*/
 
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
 
TCHAR* envVarStrings[] =
{
	TEXT("OS         = %OS%"),
	TEXT("PATH       = %PATH%"),
	TEXT("HOMEPATH   = %HOMEPATH%"),
	TEXT("TEMP       = %TEMP%")
};
#define  ENV_VAR_STRING_COUNT  (sizeof(envVarStrings)/sizeof(TCHAR*))
#define INFO_BUFFER_SIZE 32767
 
int main()
{
	DWORD i;
	TCHAR  infoBuf[INFO_BUFFER_SIZE];
	DWORD  bufCharCount = INFO_BUFFER_SIZE;
 
	printf("Windows system information.\n\n");
 
	// Get and display the name of the computer. 
	bufCharCount = INFO_BUFFER_SIZE;
	if (!GetComputerName(infoBuf, &bufCharCount))
		_tprintf(TEXT("GetComputerName"));
	_tprintf(TEXT("\nComputer name:      %s"), infoBuf);
 
	// Get and display the user name. 
	bufCharCount = INFO_BUFFER_SIZE;
	if (!GetUserName(infoBuf, &bufCharCount))
		_tprintf(TEXT("GetUserName"));
	_tprintf(TEXT("\nUser name:          %s"), infoBuf);
 
	// Get and display the system directory. 
	if (!GetSystemDirectory(infoBuf, INFO_BUFFER_SIZE))
		_tprintf(TEXT("GetSystemDirectory"));
	_tprintf(TEXT("\nSystem Directory:   %s"), infoBuf);
 
	// Get and display the Windows directory. 
	if (!GetWindowsDirectory(infoBuf, INFO_BUFFER_SIZE))
		_tprintf(TEXT("GetWindowsDirectory"));
	_tprintf(TEXT("\nWindows Directory:  %s"), infoBuf);
 
	/*
	The following code from:
	http://stackoverflow.com/questions/11917946/how-do-i-get-available-disk-space-from-windows-using-c
	*/
 
	BOOL  fResult;
	unsigned __int64 i64FreeBytesToCaller,
		i64TotalBytes,
		i64FreeBytes;
	fResult = GetDiskFreeSpaceEx(L"C:",
		(PULARGE_INTEGER)&i64FreeBytesToCaller,
		(PULARGE_INTEGER)&i64TotalBytes,
		(PULARGE_INTEGER)&i64FreeBytes);
	if (fResult)
	{
		printf("\n\nAvailable disk space on Drive C:\\ \n\n");
	/*	printf("Available space to caller = %I64u MB\n",
			i64FreeBytesToCaller / (1024 * 1024)); */
		printf("Total space on partition:            = %I64u MB\n",
			i64TotalBytes / (1024 * 1024));
		printf("Free space on drive partition:       = %I64u MB\n",
			i64FreeBytes / (1024 * 1024));
	}
 
	return 0;
}

Programming on Windows is very interesting compared to UNIX or Linux, but it gets the job done.

This is one of the included files.

stdafx.cpp
1
2
3
4
5
6
7
8
9
 
// stdafx.cpp : source file that includes just the standard includes
// Jason.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
 
#include "stdafx.h"
 
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

The file above includes this file in turn. This is very strange, but this is how Visual Studio does things.

stdafx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#define _WINSOCK_DEPRECATED_NO_WARNINGS
 
#pragma once
 
#include "targetver.h"
 
#include <stdio.h>
#include <tchar.h>
 
// TODO: reference additional headers your program requires here

I wish Windows had a version of GCC that could install from add-remove programs, instead of having to install Visual Studio to program code. But Windows development is completely different to Linux, Linux has shellcode and assembler to code slimmer programs.

This is another simple Windows program. This is a simple way to print networking information.

NetInfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
 
#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "iphlpapi.lib")
 
void print_adapter(PIP_ADAPTER_ADDRESSES aa)
{
	char buf[BUFSIZ];
	memset(buf, 0, BUFSIZ);
	WideCharToMultiByte(CP_ACP, 0, aa->FriendlyName, wcslen(aa->FriendlyName), buf, BUFSIZ, NULL, NULL);
	printf("adapter_name: %s\n", buf);
}
 
void print_addr(PIP_ADAPTER_UNICAST_ADDRESS ua)
{
	char buf[BUFSIZ];
 
	int family = ua->Address.lpSockaddr->sa_family;
	printf("\t%s ", family == AF_INET ? "IPv4" : "IPv6");
 
	memset(buf, 0, BUFSIZ);
	getnameinfo(ua->Address.lpSockaddr, ua->Address.iSockaddrLength, buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
	printf("%s\n", buf);
}
 
bool print_ipaddress()
{
	DWORD rv, size;
	PIP_ADAPTER_ADDRESSES adapter_addresses, aa;
	PIP_ADAPTER_UNICAST_ADDRESS ua;
 
	rv = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, NULL, &size);
	if (rv != ERROR_BUFFER_OVERFLOW) {
		fprintf(stderr, "GetAdaptersAddresses() failed...");
		return false;
	}
	adapter_addresses = (PIP_ADAPTER_ADDRESSES)malloc(size);
 
	rv = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, adapter_addresses, &size);
	if (rv != ERROR_SUCCESS) {
		fprintf(stderr, "GetAdaptersAddresses() failed...");
		free(adapter_addresses);
		return false;
	}
 
	for (aa = adapter_addresses; aa != NULL; aa = aa->Next) {
		print_adapter(aa);
		for (ua = aa->FirstUnicastAddress; ua != NULL; ua = ua->Next) {
			print_addr(ua);
		}
	}
 
	free(adapter_addresses);
}
 
int main()
{
 
	fprintf(stdout, "Windows network adapter information.\n");
 
	WSAData d;
	if (WSAStartup(MAKEWORD(2, 2), &d) != 0) {
		return -1;
	}
 
	print_ipaddress();
 
	WSACleanup();
 
    return 0;
}

========================================================================
CONSOLE APPLICATION : Netinfo Project Overview
========================================================================

AppWizard has created this Netinfo application for you.

This file contains a summary of what you will find in each of the files that
make up your Netinfo application.

Netinfo.vcxproj
This is the main project file for VC++ projects generated using an Application Wizard.
It contains information about the version of Visual C++ that generated the file, and
information about the platforms, configurations, and project features selected with the
Application Wizard.

Netinfo.vcxproj.filters
This is the filters file for VC++ projects generated using an Application Wizard.
It contains information about the association between the files in your project
and the filters. This association is used in the IDE to show grouping of files with
similar extensions under a specific node (for e.g. “.cpp” files are associated with the
“Source Files” filter).

Netinfo.cpp
This is the main application source file.

/////////////////////////////////////////////////////////////////////////////
Other standard files:

StdAfx.h, StdAfx.cpp
These files are used to build a precompiled header (PCH) file
named Netinfo.pch and a precompiled types file named StdAfx.obj.

/////////////////////////////////////////////////////////////////////////////
Other notes:

AppWizard uses “TODO:” comments to indicate parts of the source code you
should add to or customize.

/////////////////////////////////////////////////////////////////////////////


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.