導航:首頁 > 版本升級 > 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

友情鏈接