c语言获得程序位数和操作系统位数和名称

来源:互联网 发布:wuli什么意思网络用语 编辑:程序博客网 时间:2024/05/22 17:11
////////////////////////////////////////////////////////////// vcis64.cpp : VC 64位程序开发心的——c语言获得程序位数和操作系统位数和名称。////////////////////////////////////////////////////////////#include <Windows.h>#include <stdio.h>#include <tchar.h>#include <conio.h>// 获取程序位数(被编译为多少位的代码)int GetProgramBits(){return sizeof(int*) * 8;}// 安全的取得真实系统信息VOID SafeGetNativeSystemInfo(__out LPSYSTEM_INFO lpSystemInfo){if (NULL==lpSystemInfo)return;typedef VOID (WINAPI *LPFN_GetNativeSystemInfo)(LPSYSTEM_INFO lpSystemInfo);LPFN_GetNativeSystemInfo fnGetNativeSystemInfo = (LPFN_GetNativeSystemInfo)GetProcAddress( GetModuleHandle(_T("kernel32")), "GetNativeSystemInfo");;if (NULL != fnGetNativeSystemInfo){fnGetNativeSystemInfo(lpSystemInfo);}else{GetSystemInfo(lpSystemInfo);}}// 获取操作系统位数int GetSystemBits(){SYSTEM_INFO si;SafeGetNativeSystemInfo(&si);if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 ){return 64;}return 32;}//获取操作系统名字,如windows 7//成功返回系统信息,失败返回NULLint GetVersionInfo(char* systeminfo){OSVERSIONINFO osvi;ZeroMemory(&osvi, sizeof(OSVERSIONINFO));osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);if (!GetVersionEx(&osvi)) {return NULL;}//判断版本if (osvi.dwMajorVersion == 5) {switch (osvi.dwMinorVersion) {case 0://wcscpy_s(systeminfo,_T("Windows 2000"));strcpy(systeminfo,"Windows 2000");break;case 1:strcpy(systeminfo,"Windows XP");break;case 2:strcpy(systeminfo,"Windows Server 2003");break;default:strcpy(systeminfo,"Unknown");break;}}else if (osvi.dwMajorVersion == 6) {switch (osvi.dwMinorVersion) {case 0:strcpy(systeminfo,"Windows Vista");break;case 1:strcpy(systeminfo,"Windows 7");break;case 2:strcpy(systeminfo,"Windows 8");break;default:strcpy(systeminfo,"Unknown");break;}}else {strcpy(systeminfo,"Unknown");}return 0;}int _tmain(int argc, _TCHAR* argv[]){const int nBitCode = GetProgramBits();const int nBitSys = GetSystemBits();char systeminfo[1024];memset(systeminfo,0,1024);GetVersionInfo(systeminfo);//_tprintfprintf("I am a %dbit Program, run on %d bit %s System.", nBitCode, nBitSys,systeminfo);//_getch();return 0;}

0 0
原创粉丝点击