Knowing a system-related information is also a relatively important content. Here, we simply obtain some system information. The main content to be obtained is the system version, the name of the operating system, and the currently logged-in user name. Next, let's introduce the related API functions.
Get the operating system version: GetVersionEx();
(This function has only one parameter, which points to a pointer to the OSVERSIONINFO structure.)
Get the computer name: GetComputerName();
Get the current user name: GetUserName();
The following code runs without problems under normal circumstances. If there is any problem, please change the %s in the output to %ls
//Get the operating system version GetVersionEx() function
//GetVersionEx() function is a parameter, this parameter points to the OSVERSIONINFO structure pointer//Get computer name GetComputerName() function
//Get current user name GetUserName() function
#include <windows.h>
#include <stdio.h>
void GetSysInfo();
int main(void)
{
GetSysInfo();
return 0;
}
void GetSysInfo()
{
char szComputerName[MAXBYTE] = { 0 };
char szUserName[MAXBYTE] = { 0 };
unsigned long nSize = MAXBYTE;
OSVERSIONINFO OsVer;
OsVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&OsVer);
if( OsVer.dwPlatformId == VER_PLATFORM_WIN32_NT )
{
if( OsVer.dwMajorVersion == 5 )
{
switch ( OsVer.dwMinorVersion )
{
case 0:
printf("Windows 2000\r\n");
break;
case 1:
printf("Windows XP %s \r\n", OsVer.szCSDVersion);
break;
case 2:
printf("Windows 2003 or Windows 2003 R2\r\n");
break;
default:
printf("Other System \r\n");
}
}
else if ( OsVer.dwMajorVersion == 6 )
{
switch(OsVer.dwMinorVersion)
{
case 0:
printf("Windows Vista\r\n");
break;
case 1:
printf("Windows 7 \r\n");
break;
case 2:
printf("Windows 8 \r\n");
break;
case 3:
printf("Windows 8.1 \r\n");
break;
default:
printf("Other System \r\n");
}
}
else if ( OsVer.dwMajorVersion == 10 )
{
switch ( OsVer.dwMinorVersion )
{
case 0:
printf("Windows 10 \r\n");
break;
default:
printf("Other System \r\n");
}
}
else
{
printf("Sorry. Unknown System! \r\n");
}
}
/* else
{
printf("Sorry. Unknown System! \r\n");
}*/
GetComputerName(szComputerName, &nSize);
printf("Computer Name is %s \r\n", szComputerName);
nSize = MAXBYTE;
GetUserName(szUserName, &nSize);
printf("User Name is %s \r\n", szUserName);
}