加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

用C++代码判断操作系统类型

(2012-08-02 16:20:02)
标签:

杂谈

  
  1. #include <windows.h>   
  2. #include <Lmcons.h>   
  3. #include <stdio.h>   
  4.   
  5. void ShowVersionInfo();  
  6. void ShowSystemInfo();  
  7. void GetFolders();  
  8. void GetNames();  
  9. void MouseSpeed();  
  10.   
  11. int main()  
  12.  
  13.     ShowVersionInfo();  
  14.     ShowSystemInfo();  
  15.     GetFolders();  
  16.     GetNames();  
  17.     MouseSpeed();  
  18.  
  19.     
  20. void ShowVersionInfo()  
  21.  
  22.     OSVERSIONINFOEX ovex;  
  23.     CHAR szVersionInfo[1024];  
  24.     *szVersionInfo '\x00' 
  25.     // 设置参数大小,调用并判断是否成功   
  26.     ovex.dwOSVersionInfoSize sizeof(OSVERSIONINFOEX);  
  27.     if(!GetVersionEx(&ovex))  
  28.      
  29.         printf("error %d\n",GetLastError());  
  30.         return 
  31.      
  32.     // 判断版本   
  33.     if(ovex.dwMajorVersion==5)  
  34.      
  35.         if(ovex.dwMinorVersion==0)  
  36.             lstrcat(szVersionInfo,"Windows 2000 ");  
  37.         else if(ovex.dwMinorVersion==1)  
  38.             lstrcat(szVersionInfo,"Windows XP ");  
  39.         else if(ovex.dwMinorVersion==2)  
  40.             lstrcat(szVersionInfo,"Windows Server 2003 ");  
  41.         else if(ovex.dwMinorVersion==7)  
  42.             lstrcat(szVersionInfo,"Windows ");  
  43.      
  44.     else if(ovex.dwMajorVersion == 6)  
  45.         lstrcat(szVersionInfo,"Windows Vista ");  
  46.     else  
  47.         lstrcat(szVersionInfo,"Windows NT 4.0 或者其他 ");  
  48.     // 安装的SP,字符串   
  49.     lstrcat(szVersionInfo,ovex.szCSDVersion);  
  50.     // 判断wProductType,产品类型   
  51.     switch(ovex.wProductType)  
  52.      
  53.     case VER_NT_DOMAIN_CONTROLLER:  
  54.         lstrcat(szVersionInfo,"\n域控制器");  
  55.         break 
  56.     case VER_NT_SERVER:  
  57.         lstrcat(szVersionInfo,"\n服务器");  
  58.         break 
  59.     case VER_NT_WORKSTATION  
  60.         lstrcat(szVersionInfo,"\n独立工作站");  
  61.         break 
  62.      
  63.     // 判断wSuiteMask   
  64.     if(ovex.wSuiteMask VER_SUITE_PERSONAL)  
  65.      
  66.         lstrcat(szVersionInfo,"\nWindows XP Home Edition");  
  67.      
  68.     if(ovex.wSuiteMask VER_SUITE_SINGLEUSERTS)  
  69.      
  70.         lstrcat(szVersionInfo,"\n安装了终端服务,但只支持一个会话");  
  71.      
  72.     // wSuiteMask成员还可能是以下值的组合   
  73.     //VER_SUITE_BLADE   
  74.     //VER_SUITE_COMPUTE_SERVER   
  75.     //VER_SUITE_DATACENTER   
  76.     //VER_SUITE_ENTERPRISE   
  77.     //VER_SUITE_EMBEDDEDNT   
  78.     //VER_SUITE_PERSONAL   
  79.     //VER_SUITE_SINGLEUSERTS   
  80.     //VER_SUITE_SMALLBUSINESS   
  81.     //VER_SUITE_SMALLBUSINESS_RESTRICTED   
  82.     //VER_SUITE_STORAGE_SERVER   
  83.     //VER_SUITE_TERMINAL   
  84.     printf("%s\n",szVersionInfo);  
  85.  
  86.   
  87. void ShowSystemInfo()  
  88.  
  89.     SYSTEM_INFO si;  
  90.     GetSystemInfo(&si);  
  91.     printf("内存分页大小:0x%.8X,可用内存起始:0x%.8X,可用内存结束:0x%.8X,\n"  
  92.         "处理器个数:%d,处理器类型:" 
  93.         si.dwPageSize,  
  94.         si.lpMinimumApplicationAddress,  
  95.         si.lpMaximumApplicationAddress,  
  96.         si.dwNumberOfProcessors);  
  97.   
  98.     switch (si.dwProcessorType)  
  99.      
  100.     case PROCESSOR_INTEL_386:  
  101.         printf("386");  
  102.         break 
  103.     case PROCESSOR_INTEL_486:  
  104.         printf("486");  
  105.         break 
  106.     case PROCESSOR_INTEL_PENTIUM:  
  107.         printf("pentium");  
  108.         printf(", Cpu Model 0x%.2X, Stepping 0x%.2X" 
  109.             (BYTE)(si.wProcessorRevision>>8),  
  110.             (BYTE)si.wProcessorRevision);  
  111.         break 
  112.      
  113.     printf("\n处理器架构:");  
  114.     switch (si.wProcessorArchitecture)  
  115.      
  116.     case PROCESSOR_ARCHITECTURE_INTEL:  
  117.         printf("intel");  
  118.         printf(CPU vendor is %d",si.wProcessorLevel);  
  119.         break 
  120.     case PROCESSOR_ARCHITECTURE_IA64:  
  121.         printf("64 bits intel");  
  122.         break 
  123.     case PROCESSOR_ARCHITECTURE_AMD64:  
  124.         printf("64 bits AMD");  
  125.         break 
  126.     case PROCESSOR_ARCHITECTURE_UNKNOWN:  
  127.         printf("UNKNOWN");  
  128.         break 
  129.      
  130.     printf("\n");  
  131.  
  132.   
  133. void GetFolders()  
  134.  
  135.     TCHAR szSystemDirectory[MAX_PATH];  
  136.     TCHAR szWindowsDirectory[MAX_PATH];  
  137.   
  138.     GetSystemDirectory(szSystemDirectory,MAX_PATH);  
  139.     GetWindowsDirectory(szWindowsDirectory,MAX_PATH);  
  140.     printf("系统目录:\t%s\nWindows目录:\t%s\n" 
  141.         szSystemDirectory,  
  142.         szWindowsDirectory);  
  143.  
  144.   
  145. void GetNames()  
  146.  
  147.     DWORD dwComputerNameLen MAX_COMPUTERNAME_LENGTH+1;  
  148.     DWORD dwUserNameLen UNLEN+1;  
  149.     TCHAR szComputerName[MAX_COMPUTERNAME_LENGTH+1];  
  150.     TCHAR szUserName[UNLEN 1];  
  151.     if(!SetComputerName("My_Computer"))  
  152.      
  153.         printf("Set Error %d",GetLastError());  
  154.      
  155.     GetComputerName(szComputerName,&dwComputerNameLen);  
  156.   
  157.     printf("计算机名:%s\n",szComputerName);  
  158.   
  159.     //ComputerNameNetBIOS    
  160.     //ComputerNameDnsHostname    
  161.     //ComputerNameDnsFullyQualified    
  162.     //ComputerNamePhysicalNetBIOS    
  163.     //ComputerNamePhysicalDnsHostname    
  164.     //ComputerNamePhysicalDnsDomain    
  165.     //ComputerNamePhysicalDnsFullyQualified    
  166.     dwComputerNameLen MAX_COMPUTERNAME_LENGTH+1;  
  167.     GetComputerNameEx(ComputerNameDnsHostname,szComputerName,&dwComputerNameLen);  
  168.     printf("ComputerNameDnsHostname: %s\n",szComputerName);  
  169.   
  170.     dwComputerNameLen MAX_COMPUTERNAME_LENGTH+1;  
  171.     GetComputerNameEx(ComputerNamePhysicalNetBIOS,szComputerName,&dwComputerNameLen);  
  172.     printf("ComputerNamePhysicalNetBIOS: %s\n",szComputerName);   
  173.     GetUserName(szUserName,&dwUserNameLen);  
  174.     printf("用户名:%s\n",szUserName);  
  175.  
  176.   
  177. void MouseSpeed()  
  178.  
  179.   
  180.     BOOL fResult;  
  181.     int aMouseInfo[3];       // 保存数据信息的数组   
  182.   
  183.     // 调用 SystemParametersInfo   
  184.     fResult SystemParametersInfo(  
  185.         SPI_GETMOUSE,   // 获取鼠标信息   
  186.         0,              // 未使用   
  187.         &aMouseInfo,    // 用于保存鼠标信息   
  188.         0);             // 未使用   
  189.   
  190.     // 把鼠标速度加倍    
  191.     iffResult  
  192.      
  193.         aMouseInfo[2] aMouseInfo[2];   
  194.   
  195.         SystemParametersInfo(  
  196.             SPI_SETMOUSE,       // 设置鼠标信息   
  197.             0,                 // 未使用   
  198.             aMouseInfo,         // 鼠标信息   
  199.             SPIF_SENDCHANGE);  // 更新 win.ini    
  200.      
  201.  

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有