GetVersionEx 在win8 win8.1 win10 之后已经无法使用,如果非要使用的话需要让exe嵌入manifest,mainfest如下。这个文件需要已utf-8存储。
1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 2 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 3 <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> 4 <application> 5 <!-- Windows 10 --> 6 <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/> 7 <!-- Windows 8.1 --> 8 <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/> 9 <!-- Windows Vista --> 10 <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/> 11 <!-- Windows 7 --> 12 <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/> 13 <!-- Windows 8 --> 14 <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/> 15 </application> 16 </compatibility> 17 </assembly>
或者使用高版本windows库函数
这里我使用了其他的函数来获取
1 typedef LONG (WINAPI *pfnRtlGetVersion)(RTL_OSVERSIONINFOEXW*); 2 bool MyGetVersionEx(RTL_OSVERSIONINFOEXW &osverinfo) 3 { 4 osverinfo.dwOSVersionInfoSize = sizeof(osverinfo); 5 HMODULE hmodule=LoadLibrary(L"ntdll.dll"); 6 pfnRtlGetVersion Rtl = (pfnRtlGetVersion)GetProcAddress(hmodule,"RtlGetVersion"); 7 if (Rtl) 8 { 9 if(!Rtl(&osverinfo)) 10 { 11 FreeLibrary(hmodule); 12 return true; 13 } 14 else 15 { 16 FreeLibrary(hmodule); 17 return false; 18 } 19 } 20 else 21 { 22 FreeLibrary(hmodule); 23 return false; 24 } 25 } 26 bool GetOSName(string &strOsName,bool &bX86) 27 { 28 SYSTEM_INFO info; //用SYSTEM_INFO结构判断64位AMD处理器 29 GetSystemInfo(&info); //调用GetSystemInfo函数填充结构 30 31 RTL_OSVERSIONINFOEXW osInfo= {0}; 32 if(MyGetVersionEx(osInfo)) 33 { 34 char osName[30] = "UNKNOWN"; 35 36 if (GetSystemBits()==64) 37 { 38 bX86=false; 39 } 40 else 41 { 42 bX86=true; 43 } 44 switch(osInfo.dwPlatformId) 45 { 46 case VER_PLATFORM_WIN32_WINDOWS: 47 { 48 switch(osInfo.dwMinorVersion) 49 { 50 case 0: 51 { 52 strcpy(osName,"Windows 95"); 53 break; 54 } 55 56 case 10: 57 { 58 strcpy(osName,"Windows 98"); 59 break; 60 } 61 62 case 90: 63 { 64 strcpy(osName,"Windows Me"); 65 break; 66 } 67 } 68 break; 69 } 70 71 case VER_PLATFORM_WIN32_NT: 72 { 73 switch(osInfo.dwMajorVersion) 74 { 75 case 3: 76 { 77 strcpy(osName,"Windows NT 3.51"); 78 break; 79 } 80 81 case 4: 82 { 83 strcpy(osName,"Windows NT 4.0"); 84 break; 85 } 86 87 case 5: 88 { 89 if(osInfo.dwMinorVersion == 0) 90 { 91 strcpy(osName,"Windows 2000"); 92 } 93 else if(osInfo.dwMinorVersion == 1) 94 { 95 if(osInfo.wSuiteMask & VER_SUITE_EMBEDDEDNT) 96 { 97 strcpy(osName,"Windows XP Embedded"); 98 } 99 else 100 { 101 strcpy(osName,"Windows XP"); 102 } 103 } 104 else if(osInfo.dwMinorVersion == 2) 105 { 106 if(osInfo.wProductType==VER_NT_WORKSTATION && info.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64) 107 { 108 strcpy(osName,"Windows XP Pro x64 Edition"); 109 } 110 else if( GetSystemMetrics(SM_SERVERR2) == 0 ) 111 { 112 strcpy(osName,"Windows Server 2003"); 113 } 114 else if( GetSystemMetrics(SM_SERVERR2) != 0 ) 115 { 116 strcpy(osName,"Windows Server 2003 R2"); 117 } 118 119 } 120 break; 121 } 122 123 case 6: 124 { 125 //printf("%d %d ",6,osInfo.dwMinorVersion); 126 if(osInfo.dwMinorVersion == 0 ) 127 { 128 if( osInfo.wProductType == VER_NT_WORKSTATION ) 129 { 130 strcpy(osName,"Windows Vista"); 131 } 132 else 133 { 134 strcpy(osName,"Windows Server 2008"); 135 } 136 } 137 else if(osInfo.dwMinorVersion == 1 ) 138 { 139 if( osInfo.wProductType == VER_NT_WORKSTATION ) 140 { 141 strcpy(osName,"Windows 7"); 142 } 143 else 144 { 145 strcpy(osName,"Windows Server 2008 R2"); 146 } 147 } 148 else if (osInfo.dwMinorVersion == 2) 149 { 150 if( osInfo.wProductType == VER_NT_WORKSTATION ) 151 { 152 strcpy(osName,"Windows 8"); 153 } 154 else 155 { 156 strcpy(osName,"Windows Server 2012 "); 157 } 158 } 159 else if (osInfo.dwMinorVersion == 3) 160 { 161 if( osInfo.wProductType == VER_NT_WORKSTATION ) 162 { 163 strcpy(osName,"Windows 8.1"); 164 } 165 else 166 { 167 strcpy(osName,"Windows Server 2012 r2"); 168 } 169 } 170 break; 171 } 172 173 case 10: 174 { 175 if (osInfo.dwMinorVersion ==0) 176 { 177 if( osInfo.wProductType == VER_NT_WORKSTATION ) 178 { 179 strcpy(osName,"Windows 10"); 180 } 181 } 182 break; 183 } 184 } 185 break; 186 } 187 } 188 string os(osName); 189 strOsName=os; 190 return true; 191 } 192 else 193 return false; 194 }