Interfacing with the Windows System Using win32

The GetSystemInfo() function enables us to get information about the hardware on which our program is running. The GetSystemInfo() function is a void function. It takes one argument, a pointer to a SYSTEM_INFO structure.

The SYSTEM_INFO structure stores information about the current computer system. The structure’s wProcessorArchitecture field is of type WORD and indicates the processor architecture of the installed operating system. If it has the value PROCESSOR_ARCHITECTURE_AMD64 than it is an x64 machine, either AMD or Intel.

#include <stdio.h>
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{

	printf("PROCESSOR_ARCHITECTURE_AMD64 = %d\n", PROCESSOR_ARCHITECTURE_AMD64);
	printf("PROCESSOR_ARCHITECTURE_ARM = %d\n", PROCESSOR_ARCHITECTURE_ARM);
	printf("PROCESSOR_ARCHITECTURE_INTEL = %d\n", PROCESSOR_ARCHITECTURE_INTEL);

	return 0;
}

Note that the dwOemId field is supposed to contain the Original Equipment Manufacturer ID, but this field is deprecated. Likewise, the dwProcessorType field is also obsolete, we should use the wProcessorArchitecture field instead.

#include <stdio.h>
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
	SYSTEM_INFO structSInfo;
	GetSystemInfo(&structSInfo);

	printf("The value for wProcessorArchitecture is %d\n", structSInfo.wProcessorArchitecture);
	
	switch(structSInfo.wProcessorArchitecture){
		case PROCESSOR_ARCHITECTURE_AMD64:
			printf("x64 architecture.\n");
			break;
		case PROCESSOR_ARCHITECTURE_ARM:
			printf("ARM architecture.\n");
			break;
		case PROCESSOR_ARCHITECTURE_INTEL:
			printf("x86 architecture.\n");
			break;
	}

	creturn 0;
}

The lpMinimumApplicationAddress of the SYSTEM_INFO structure is a pointer to the lowest memory address accessible to applications and DLLs. The lpMaximumApplicationAddress is a pointer to the highest address accessible.

#include <Windows.h>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[]) { 

  SYSTEM_INFO structSysInfo; GetSystemInfo(&structSysInfo); 
  printf("Pointer to the lowest memory address: %p\n", structSysInfo.lpMinimumApplicationAddress);
  printf("Pointer to the highest memory address: %p\n", structSysInfo.lpMaximumApplicationAddress);
  printf("Number of processors: %d\n", structSysInfo.dwNumberOfProcessors); 
 
  return 0; 

}

The GetVersionEx() function returns operating system version information via an OSVERSIONINFO structure that contains fields indicating major and minor version and build numbers. The function itself returns a Boolean value of True or False.

#include 
#include 

int _tmain(int argc, _TCHAR* argv[])
{
	OSVERSIONINFO structOSVInfo;
	//don't forget to set this field
	structOSVInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

	if(GetVersionEx(&structOSVInfo)){
		printf("Version information obtained.\n");
 	} else {
		printf("Version information not obtained.\n");
		printf("Last Error: %d\n", GetLastError());
		exit(EXIT_FAILURE);
	}

	printf("Version: %d.%d\n", structOSVInfo.dwMajorVersion, structOSVInfo.dwMinorVersion);

	return 0;
}

Note that we need to initialize the dwOSVersionInfo field of the OSVERSIONINFO structure to be sizeof(OSVERSIONINFO).

The GetLogicalDriveStrings() function fills a buffer with strings that specify valid drives in the system. If the function fails, the return value is 0, if the function succeeds, the return value is the length in characters of the strings copied into the buffer. The function takes two arguments, the first argument is the length of the buffer in characters, and the second argument is the buffer itself.

#include <stdio.h>
#include <string.h>
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    char szBuffer[512];

    if(GetLogicalDriveStrings(strlen(szBuffer),szBuffer)==0){
        printf("Error getting logical drives.\n");
        exit(EXIT_FAILURE);
    } else {
        printf("Logical drives acquired.\n");
    }

    char * szPtr = szBuffer;

    //read through the buffer
    while(*szPtr!=NULL){
        printf("%s\n", szPtr);
        //move the pointer to the next null-terminated
        //string
        szPtr += strlen(szPtr) + 1;
    }

    return 0;
}

Now that we know which drives are available, we can find out what type of device each drive represents by calling the GetDriveType() function. The GetDriveType() function determines the type of the drive from the root directory that is passed to it as a string argument and returns a numeric value indicating the drive type.

#include <stdio.h>
#include <string.h>
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    
    char szDriveNames[256];

    if(GetLogicalDriveStrings(strlen(szDriveNames), szDriveNames)==false){
        printf("Error getting drive names.\n");
        exit(EXIT_FAILURE);
    }

    char *szPtrNames = szDriveNames;
    char szDriveName[5];

    while(*szPtrNames!=NULL){
        printf("Drive %s: \t", szPtrNames);

        switch(GetDriveType(szPtrNames)){
            case DRIVE_FIXED:
                printf("HDD/SSD");
                break;
            case DRIVE_REMOTE:
                printf("Remote");
                break;
            case DRIVE_CDROM:
                printf("CD/DVD");
                break;
        }
        printf("\n");

        szPtrNames += strlen(szPtrNames) + 1;
    }

    return 0;
}

The GetVolumeInformation() function retrieves information about the file system and volume associated with the specified root directory. The function returns a Boolean value indicating its success or failure.

#include <stdio.h>
#include <string.h>
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    char szRoot[] = "C:\\";
    char szVolName[256];
    DWORD dwNameSize = sizeof(szVolName);
    DWORD dwVolID, dwMaxComLength, dwFileSysFlags;
    char szFileSys[256];
    DWORD dwFileSysNameSize = sizeof(szFileSys);
    BOOL returnVal;

    returnVal = GetVolumeInformation(
        szRoot, //root of volume
        szVolName, //volume name buffer
        dwNameSize, //length of volume name buffer
        &dwVolID, //volume serial number
        &dwMaxComLength, //maximum component length
        &dwFileSysFlags, //file system attributes
        szFileSys, //file system name buffer
        dwFileSysNameSize //length of file system name buffer
        );
    
    if(!returnVal){
        printf("Couldn't retrieve volume information.\n");
        exit(EXIT_FAILURE);
    } else {
        printf("Volume information retrieved.\n");
    }

    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s