Win32 Windows Volume Program and Code Example 6

 

 

 

Getting the System Information Program Example

 

Create a new Win32 console application project and give a suitable project name.

 

Getting the System Information Program Example - creating a new Win32 console mode application project

 

 

 

Add the source file and give a suitable name.

 

Getting the System Information Program Example - adding a new C++ source file

 

Add the following source code.

 

#include <windows.h>

#include <stdio.h>

 

WCHAR* envVarStrings[] =

{

  LOS         = %OS%,

  LPATH       = %PATH%,

  LHOMEPATH   = %HOMEPATH%,

  LTEMP       = %TEMP%

};

#define  ENV_VAR_STRING_COUNT  (sizeof(envVarStrings)/sizeof(WCHAR*))

#define INFO_BUFFER_SIZE 32767

 

void printError( WCHAR* msg );

 

int wmain( )

{

  DWORD i;

  WCHAR  infoBuf[INFO_BUFFER_SIZE];

  DWORD  bufCharCount = INFO_BUFFER_SIZE;

 

  // Get and display the name of the computer.

  bufCharCount = INFO_BUFFER_SIZE;

  if(!GetComputerName( infoBuf, &bufCharCount))

    printError(LGetComputerName());

  wprintf(L\nComputer name:      %s, infoBuf);

 

  // Get and display the user name.

  bufCharCount = INFO_BUFFER_SIZE;

  if(!GetUserName(infoBuf, &bufCharCount))

    printError(LGetUserName());

  wprintf(L\nUser name:          %s, infoBuf);

 

  // Get and display the system directory.

  if(!GetSystemDirectory(infoBuf, INFO_BUFFER_SIZE))

    printError(LGetSystemDirectory());

  wprintf(L\nSystem Directory:   %s, infoBuf);

 

  // Get and display the Windows directory.

  if(!GetWindowsDirectory(infoBuf, INFO_BUFFER_SIZE))

    printError(LGetWindowsDirectory());

  wprintf(L\nWindows Directory:  %s, infoBuf);

 

  // Expand and display a few environment variables.

  wprintf(L\n\nSome of Environment Variables:);

  for(i = 0; i < ENV_VAR_STRING_COUNT; ++i)

  {

    bufCharCount = ExpandEnvironmentStrings(envVarStrings[i], infoBuf,INFO_BUFFER_SIZE);

    if( bufCharCount > INFO_BUFFER_SIZE )

      wprintf(L\n\t(Buffer too small to expand: \%s\),envVarStrings[i]);

    else if(!bufCharCount)

      printError(LExpandEnvironmentStrings());

    else

      wprintf(L\n   %s, infoBuf);

  }

  wprintf(L\n);

 

  return 0;

}

 

void printError(WCHAR* msg)

{

  DWORD eNum;

  WCHAR sysMsg[256];

  WCHAR* p;

 

  eNum = GetLastError();

  FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM |

         FORMAT_MESSAGE_IGNORE_INSERTS,

         NULL, eNum,

         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),

         sysMsg, 256, NULL);

 

  // Trim the end of the line and terminate it with a null

  p = sysMsg;

  while(( *p > 31) || (*p == 9))

    ++p;

  do

  {

        *p-- = 0;

  } while((p >= sysMsg) && (( *p == '.') || (*p < 33)));

 

  // Display the message

  wprintf(L\n\t%s failed with error %d (%s), msg, eNum, sysMsg);

}

 

 

Build and run the project. The following screenshot is an output sample.

 

Getting the System Information Program Example - a sample basic system information output

 

 

  < Windows Volume 5 | Win32 Programming Index | Windows Volume Index | Windows Volume 7 >