码迷,mamicode.com
首页 > 其他好文 > 详细

获取系统信息

时间:2016-03-20 21:20:49      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:


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




获取系统信息

标签:

原文地址:http://www.cnblogs.com/hungryvampire/p/5299285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!