// vcis64.cpp : VC 64位程序开发心的——c语言获得程序位数和操作系统位数和名称。
#include
#include
#include
#include
// 获取程序位数(被编译为多少位的代码)
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
//成功返回系统信息,失败返回NULL
int 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);
//_tprintf
printf("I am a %dbit Program, run on %d bit %s System.", nBitCode, nBitSys,systeminfo);
//
_getch();
return 0;
}