导航:首页 > 版本升级 > c获取系统版本号

c获取系统版本号

发布时间:2024-11-04 12:15:24

⑴ 如何用C语言获取主机操作系统版本

redhat系统可直接从文件中读取:cat /etc/redhat-release
windows:
GetUserName(LPWSTR lpBuffer, LPDWORD nSize);
lpBuffer是一个数组的地址,用来存贮得到的用户名,nsize是个指针,指向此存贮此数组大小的单元,就是数组能存贮的字符数量
GetVersionExW(LPOSVERSIONINFOW);
参数LPOSVERSIONINFO是一个指向OSVERSIONINFO类型变量的指针,
具体判断代码如下
定义 OSVERSIONINFO osvi;
然后调用函数取得版本号,判断如下
switch (osvi.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT:
if (osvi.dwMajorVersion == 5&&osvi.dwMinorVersion == 0)
{
strOSVersion = TEXT("Windows 2000");
}
else if (osvi.dwMajorVersion == 5&&osvi.dwMinorVersion == 1)
{
strOSVersion = TEXT("Windows XP");
}
else if(osvi.dwMajorVersion == 6&&osvi.dwMinorVersion == 0)
{
strOSVersion = TEXT("Windows Vista");
}
else if (osvi.dwMajorVersion == 4&&osvi.dwMinorVersion == 0)
{
strOSVersion = TEXT("Windows NT");
}
break;
case VER_PLATFORM_WIN32_WINDOWS:
if (osvi.dwMajorVersion == 4&&osvi.dwMinorVersion == 10)
{
strOSVersion = TEXT("Windows 98");
}
else if (osvi.dwMajorVersion == 4&&osvi.dwMinorVersion == 90)
{
strOSVersion = TEXT("Windows ME");
}
else
{
strOSVersion = TEXT("Windows 95");
}
break;
default:
break;
}

⑵ 用C程序查看linux/win操作系统信息

获取 Windows 操作系统版本信息的 C 程序 [zt]
From:[url]http://kevin2296.blog.163.com/article/-aBzn-rfC7hw.html[/url]

[php]
// OSinfo.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include "winsock.h"
#include <string>
#include <iostream>
using std::cout;using std::string;using std::iostream;
using std::endl;using std::ios;using std::fstream;
using std::ofstream;using std::cin;
#pragma comment( lib, "Ws2_32.lib" )
#define MAX_VALUE_NAME 256

//char osinfo[MAX_PATH][MAX_PATH];
char patchname[MAX_VALUE_NAME];

//全局变量
char *LocalIP=""; //定义IP地址变量

//获取本机ip
bool GetLocalIp()
{
WSADATA wsaData;
char name[255];//定义用于存放获得的主机名的变量
PHOSTENT hostinfo;
//调用MAKEWORD()获得Winsock版本的正确值,用于加载Winsock库
if ( WSAStartup( MAKEWORD(2,2), &wsaData ) == 0 )
{
//现在是加载Winsock库,如果WSAStartup()函数返回值为0,说明加载成功,程序可以继续
if( gethostname ( name, sizeof(name)) == 0)
{
//如果成功地将本地主机名存放入由name参数指定的缓冲区中
if((hostinfo = gethostbyname(name)) != NULL)
{
//这是获取主机名,如果获得主机名成功的话,将返回一个指针,指向hostinfo,hostinfo
//为PHOSTENT型的变量,下面即将用到这个结构体
LocalIP = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
//调用inet_ntoa()函数,将hostinfo结构变量中的h_addr_list转化为标准的点分表示的IP
//地址(如192.168.0.1)
// printf("the ip is:%s\n",LocalIP);//输出IP地址
}
}
WSACleanup( );//卸载Winsock库,并释放所有资源

}
return TRUE;
}

//显示系统版本
bool DisplaySystemVesion(FILE *fp)
{
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx;
//利用OSVERSIONINFOEX结构调用GetVersionEx()函数
//如果调用失败,可尝试使用OSVERSIONINFO结构
ZeroMemory(&osvi,sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize=sizeof(OSVERSIONINFOEX);
if(!(bOsVersionInfoEx=GetVersionEx((OSVERSIONINFO*)&osvi)))
{
//
osvi.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
if(!GetVersionEx((OSVERSIONINFO*)&osvi))
return FALSE;
}
switch(osvi.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT:
//get os information
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
{
printf("Microsoft Windows Server 2003 family ");
//fprintf( fp, "Microsoft Windows Server 2003 family ");
fprintf( fp, "WindowsServer2003");
// strcpy(osinfo[0],"Microsoft Windows Server 2003 family ");
}

if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
{
printf("Microsoft Windows XP ");
//fprintf( fp, "Microsoft Windows XP ");
fprintf( fp, "WindowsXP");
// strcpy(osinfo[0],"Microsoft Windows XP ");
}
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
{
printf("Microsoft Windows 2000 ");
//fprintf( fp, "Microsoft Windows 2000 ");
fprintf( fp, "Windows2000");
// strcpy(osinfo[0],"Microsoft Windows 2000 ");
}
if ( osvi.dwMajorVersion <= 4 )
{
printf("Microsoft Windows NT ");
//fprintf( fp, "Microsoft Windows NT ");
fprintf( fp, "WindowsNT");
// strcpy(osinfo[0],"Microsoft Windows NT ");
}
//
if(bOsVersionInfoEx)
{
if(osvi.wProctType==VER_NT_WORKSTATION)
{
printf("Professional ");
// fprintf(fp,"Professional ");
}
if(osvi.wProctType==VER_NT_SERVER)
{
printf("Server ");
// fprintf(fp,"Server ");
}
//printf("\n");
fprintf(fp,"\n");
}
else
{
HKEY hKey;
char szProctType[80];
DWORD dwBufLen;
RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Control\\ProctOptions",0,KEY_QUERY_VALUE,&hKey);
RegQueryValueEx(hKey,"ProctType",NULL,NULL,(LPBYTE)szProctType,&dwBufLen);
RegCloseKey(hKey);

if(lstrcmpi("WINNT",szProctType)==0)
{
printf("Workstation ");
// fprintf(fp,"Workstation ");
}
if(lstrcmpi("SERVERNT",szProctType)==0)
{
printf("Server ");
// fprintf(fp,"Server ");
}
//printf("\n");
fprintf(fp,"\n");
}

// Display service pack (if any) and build number.
if( osvi.dwMajorVersion == 4 &&
lstrcmpi( osvi.szCSDVersion, "Service Pack 6" ) == 0 )
{
HKEY hKey;
LONG lRet;
// Test for SP6 versus SP6a.
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009",
0, KEY_QUERY_VALUE, &hKey );
if( lRet == ERROR_SUCCESS )
{
printf( "Service Pack 6a (Build %d)\n", osvi.dwBuildNumber & 0xFFFF );
// fprintf( fp,"Service Pack 6a (Build %d)\n", osvi.dwBuildNumber & 0xFFFF );
// fprintf( fp,"Service Pack 6a\n" );
fprintf( fp,"6a\n" );
}
else // Windows NT 4.0 prior to SP6a
{
printf( "%s (Build %d)",osvi.szCSDVersion,osvi.dwBuildNumber & 0xFFFF);
/* printf( "%s (Build %d)\n",
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
*/
// fprintf( fp,"%s (Build %d)\n",
// osvi.szCSDVersion,
// osvi.dwBuildNumber & 0xFFFF);
// fprintf( fp,"%s\n",osvi.szCSDVersion);
fprintf( fp,"%s\n",osvi.szCSDVersion+13); //去掉"Service Pack "共13个字节,只保留最后的号
}
RegCloseKey( hKey );
}
else // Windows NT 3.51 and earlier or Windows 2000 and later
{
printf( "%s (Build %d)",osvi.szCSDVersion,osvi.dwBuildNumber & 0xFFFF);
/*printf( "%s (Build %d)\n",
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
*/
// fprintf( fp,"%s (Build %d)\n",
// osvi.szCSDVersion,
// osvi.dwBuildNumber & 0xFFFF);
// fprintf( fp,"%s\n",osvi.szCSDVersion);
fprintf( fp,"%s\n",osvi.szCSDVersion+13); //去掉"Service Pack "共13个字节,只保留最后的号
}
break;
case VER_PLATFORM_WIN32_WINDOWS:
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
{
printf("Microsoft Windows 95 ");
fprintf(fp,"Microsoft Windows 95 ");
if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' )
{
printf("OSR2");
fprintf(fp,"OSR2");
// strcat(osinfo[0], "OSR2" );
}
printf("\n");
fprintf(fp,"\n");
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
{
printf("Microsoft Windows 98 ");
fprintf(fp,"Microsoft Windows 98 ");
if ( osvi.szCSDVersion[1] == 'A' )
{
printf("SE");
fprintf(fp,"SE");
}
printf("\n");
fprintf(fp,"\n");
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
{
printf("Microsoft Windows Millennium Edition\n");
fprintf(fp,"Microsoft Windows Millennium Edition\n");
}
break;
case VER_PLATFORM_WIN32s:
printf(" Microsoft Win32s\n");
fprintf(fp," Microsoft Win32s\n");
break;
}
return TRUE;
}
void DisplaySetupedPatch(FILE *fp)
{
HKEY hKey;
// LONG lRet;
// char Buf[80];
// char lpBuf[80];
DWORD i;
DWORD retCode;
// DWORD j;
// DWORD retValue;
DWORD dwxValueName=MAX_VALUE_NAME;
LPCTSTR path="SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix";
LONG return0=RegOpenKeyEx(HKEY_LOCAL_MACHINE,path,0,KEY_READ,&hKey);
if(return0!=ERROR_SUCCESS)
{
printf("错误:无法打开有关的键!");
}
for(i=1,retCode=ERROR_SUCCESS;retCode==ERROR_SUCCESS;i++)
{
retCode=RegEnumKey(hKey,i,patchname,MAX_PATH);
if(retCode==ERROR_SUCCESS)
{
printf("%s\n",patchname);
fprintf(fp,"%s\n",patchname);
}
}

}
void main()
{
//写入文件
FILE *fp;
GetLocalIp();
const char SaveReciveFilePath[]="f:\\";
string abPath(SaveReciveFilePath);
char Filename[512]="";
strcpy(Filename,LocalIP); //拷贝字符串
strcat(Filename,".txt"); //连接字符串
abPath += Filename;
fp=fopen(abPath.c_str(),"w");
while(fp==NULL);
// printf("the LocalIP is:%s\n",LocalIP);//输出IP地址
printf("本机IP地址:%s\n",LocalIP);//输出IP地址
// fprintf(fp,"本机IP:%s\n",LocalIP);
// fprintf(fp,"%s\n",LocalIP);
bool a=FALSE;
printf("本机操作系统版本是:");
// fprintf(fp,"本机操作系统版本是:");
//操作系统版本信息
while (a!=TRUE)
{
a=DisplaySystemVesion(fp);
}
//操作系统语言信息
printf("语言:");
// fprintf(fp,"语言:");
/* WORD wLangPID = PRIMARYLANGID( GetSystemDefaultLangID() );
if( LANG_CHINESE == wLangPID )
{
printf("CHS\n");//中文
fprintf(fp,"CHN\n");
}
else
{
printf("ENG\n"); //英文
fprintf(fp,"ENG\n");
}
if( NULL == wLangPID )
{
printf( "Failure!\n" );
}
*/
int LanguageID=GetSystemDefaultLangID(); //返回操作系统语言的ID号
switch(LanguageID)
{
case 0x0404:
printf("CHT\n");//Chinese Taiwan
fprintf(fp,"CHT\n");
break;
case 0x0804:
printf("CHS\n");//Chinese PRC
fprintf(fp,"CHS\n");
break;
case 0x0c04:
printf("ZHH\n");//Chinese Hong Kong SAR
fprintf(fp,"ZHH\n");
break;
case 0x1004:
printf("ZHH\n");//Chinese Singapore
fprintf(fp,"ZHH\n");
break;
case 0x1404:
printf("ZHM\n");//Chinese Macau SAR
fprintf(fp,"ZHM\n");
break;
case 0x0809:
printf("ENG\n");//English United Kingdom
fprintf(fp,"ENG\n");
break;
case 0x0409:
printf("USA\n");//English United States
fprintf(fp,"USA\n");
break;
default:
printf("Default\n");
fprintf(fp,"Default\n");
break;
}
// printf("%d\n",id);
//操作系统已安装补丁信息
printf("系统已经安装的升级补丁有:\n");
// fprintf(fp,"系统已经安装的升级补丁有:\n");
DisplaySetupedPatch(fp);
fclose(fp);
}

⑶ C语言如何获取系统版本就是说win XP,win7还是其他的什么

调用API 读取注册表就OK了
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
int main()
{
TCHAR buf[255];
int size;
HKEY hKey;
DWORD regType = REG_SZ;
LONG ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
0,
KEY_READ,
&hKey);
if(ret!=ERROR_SUCCESS)
{
_tprintf(TEXT("%s"), TEXT("无法打开\nHKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"));
return 1;
}

size = 254;
ret = RegQueryValueEx(hKey, "ProctName",0,®Type,
buf, &size);
if(ret!=ERROR_SUCCESS)
_tprintf(TEXT("%s"), TEXT("无法读取ProctName"));
else
_tprintf(TEXT("系统:%s\n"), buf);

size = 254;
ret = RegQueryValueEx(hKey, "CSDVersion",0,®Type,
buf, &size);
if(ret!=ERROR_SUCCESS)
_tprintf(TEXT("%s"), TEXT("无法读取CSDVersion"), TEXT("错误"));
else
_tprintf(TEXT("补丁包:%s\n"), buf);

size = 254;
ret = RegQueryValueEx(hKey, "CurrentVersion",0,®Type,
buf, &size);
if(ret!=ERROR_SUCCESS)
_tprintf(TEXT("%s"), TEXT("无法读取CurrentVersion"), TEXT("错误"));
else
_tprintf(TEXT("版本号:%s\n"), buf);

size = 254;
ret = RegQueryValueEx(hKey, "CurrentBuildNumber",0,®Type,
buf, &size);
if(ret!=ERROR_SUCCESS)
_tprintf(TEXT("%s"), TEXT("无法读取CurrentBuildNumber"), TEXT("错误"));
else
_tprintf(TEXT("子版本号:%s\n"), buf);

RegCloseKey(hKey);
return 0;
}

⑷ windows下C/C++如何获得当前主机的信息:操作系统版本、系统类型、CUP、网卡、内存和磁盘的利用率

示例代码如下:

GetsysInfo.h:

#ifndef_H_GETSYSINFO
#define_H_GETSYSINFO


#pragmaonce

#include<afxtempl.h>

classGetSysInfo
{
public:
GetSysInfo(void);
~GetSysInfo(void);

public:
/********获取操作系统版本,Servicepack版本、系统类型************/
voidGetOSVersion(CString&strOSVersion,CString&strServiceVersion);
BOOLIsWow64();//判断是否为64位操作系统

/***********获取网卡数目和名字***********/
intGetInterFaceCount();
voidGetInterFaceName(CString&InterfaceName,intpNum);

/***获取物理内存和虚拟内存大小***/
voidGetMemoryInfo(CString&dwTotalPhys,CString&dwTotalVirtual);

/****获取CPU名称、内核数目、主频*******/
voidGetCpuInfo(CString&chProcessorName,CString&chProcessorType,DWORD&dwNum,DWORD&dwMaxClockSpeed);

/****获取硬盘信息****/
voidGetDiskInfo(DWORD&dwNum,CStringchDriveInfo[]);

/****获取显卡信息*****/
voidGetDisplayCardInfo(DWORD&dwNum,CStringchCardName[]);
private:
CStringListInterfaces; //保存所有网卡的名字
CList<DWORD,DWORD&> Bandwidths; //各网卡的带宽
CList<DWORD,DWORD&> TotalTraffics;//各网卡的总流量
};

#endif



阅读全文

与c获取系统版本号相关的资料

热点内容
ps入门必备文件 浏览:348
以前的相亲网站怎么没有了 浏览:15
苹果6耳机听歌有滋滋声 浏览:768
怎么彻底删除linux文件 浏览:379
编程中字体的颜色是什么意思 浏览:534
网站关键词多少个字符 浏览:917
汇川am系列用什么编程 浏览:41
笔记本win10我的电脑在哪里打开摄像头 浏览:827
医院单位基本工资去哪个app查询 浏览:18
css源码应该用什么文件 浏览:915
编程ts是什么意思呢 浏览:509
c盘cad占用空间的文件 浏览:89
不锈钢大小头模具如何编程 浏览:972
什么格式的配置文件比较主流 浏览:984
增加目录word 浏览:5
提取不相邻两列数据如何做图表 浏览:45
r9s支持的网络制式 浏览:633
什么是提交事务的编程 浏览:237
win10打字卡住 浏览:774
linux普通用户关机 浏览:114

友情链接