跟Google学写代码--Chromium/base--windows_version源码学习及应用

Chromium是一个伟大的、庞大的开源工程,很多值得我们学习的地方。 前面写道: 《跟Google学写代码–Chromium/base–stl_util源码学习及应用》

今天与大家分享的就是Chromium下base中的windows_version,对于你的win32应用程序一定会有很多的帮助。

1 定义version枚举

enum Version {
  VERSION_PRE_XP = 0,  // Not supported.
  VERSION_XP,
  VERSION_SERVER_2003, // Also includes XP Pro x64 and Server 2003 R2.
  VERSION_VISTA,       // Also includes Windows Server 2008.
  VERSION_WIN7,        // Also includes Windows Server 2008 R2.
  VERSION_WIN8,        // Also includes Windows Server 2012.
  VERSION_WIN8_1,      // Code named Windows Blue
  VERSION_WIN_LAST,    // Indicates error condition.};
enum VersionType {
  SUITE_HOME,
  SUITE_PROFESSIONAL,
  SUITE_SERVER,
  SUITE_LAST,
};

2 定义 OSInfo类 a. 通过BASE_EXPORT定义为导出类 b. 将OSInfo()和 ~OSInfo()声明为私有,即我们要的单例模式

class BASE_EXPORT OSInfo { public:  struct VersionNumber {    int major;    int minor;    int build;
  };  struct ServicePack {    int major;    int minor;
  };  enum WindowsArchitecture {
    X86_ARCHITECTURE,
    X64_ARCHITECTURE,
    IA64_ARCHITECTURE,
    OTHER_ARCHITECTURE,
  };  enum WOW64Status {
    WOW64_DISABLED,
    WOW64_ENABLED,
    WOW64_UNKNOWN,
  };  static OSInfo* GetInstance();

  Version version() const { return version_; }
  VersionNumber version_number() const { return version_number_; }
  VersionType version_type() const { return version_type_; }
  ServicePack service_pack() const { return service_pack_; }
  WindowsArchitecture architecture() const { return architecture_; }  int processors() const { return processors_; }
  size_t allocation_granularity() const { return allocation_granularity_; }
  WOW64Status wow64_status() const { return wow64_status_; }
  std::string processor_model_name();  static WOW64Status GetWOW64StatusForProcess(HANDLE process_handle); private:  OSInfo();
  ~OSInfo();

  Version version_;
  VersionNumber version_number_;
  VersionType version_type_;
  ServicePack service_pack_;
  WindowsArchitecture architecture_;  int processors_;
  size_t allocation_granularity_;
  WOW64Status wow64_status_;
  std::string processor_model_name_;

  DISALLOW_COPY_AND_ASSIGN(OSInfo);
};// Because this is by far the most commonly-requested value from the above// singleton, we add a global-scope accessor here as syntactic sugar.BASE_EXPORT Version GetVersion();

}  // namespace win}  // namespace base

3 GetInstance()的实现 这里用到了InterlockedCompareExchangePointer: Provides compiler intrinsic support for the Win32 Platform SDK InterlockedCompareExchangePointer function.

OSInfo* OSInfo::GetInstance() {  static OSInfo* info;  if (!info) {
    OSInfo* new_info = new OSInfo();    if (InterlockedCompareExchangePointer(        reinterpret_cast<PVOID*>(&info), new_info, NULL)) {      delete new_info;
    }
  }  return info;
}

这里补充一下,delete new_info后,最好把new_info置为nullptr。

4 获取WOW64状态的实现 最主要的是这行: GetProcAddress(GetModuleHandle(L”kernel32.dll”), “IsWow64Process”));

可以通过加载Kernel32来动态判断 当前操作系统32bit还是64bit。

OSInfo::WOW64Status OSInfo::GetWOW64StatusForProcess(HANDLE process_handle) {  typedef BOOL (WINAPI* IsWow64ProcessFunc)(HANDLE, PBOOL);
  IsWow64ProcessFunc is_wow64_process = reinterpret_cast<IsWow64ProcessFunc>(
      GetProcAddress(GetModuleHandle(L"kernel32.dll"), "IsWow64Process"));  if (!is_wow64_process)    return WOW64_DISABLED;
  BOOL is_wow64 = FALSE;  if (!(*is_wow64_process)(process_handle, &is_wow64))    return WOW64_UNKNOWN;  return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED;
}

应用: 简单的使用:

int main(int argc, char* argv[]) {  int version = base::win::OSInfo::GetInstance()->version();  std::cout << "VERSION_PRE_XP = 0," << std::endl;  std::cout << "VERSION_SERVER_2003 = 1," << std::endl;  std::cout << "VERSION_VISTA = 2," << std::endl;  std::cout << "VERSION_WIN7 = 3," << std::endl;  std::cout << "VERSION_WIN8 = 4," << std::endl;  std::cout << "VERSION_WIN8_1 = 5," << std::endl;  std::cout << "VERSION_WIN_LAST = 6," << std::endl;  std::cout << "My Windows Version is: " << version << std::endl << std::endl;  std::string processor_model_name = base::win::OSInfo::GetInstance()->processor_model_name();  std::cout << "My Processor Model Name is: " << processor_model_name << std::endl << std::endl;  int wow64_status = base::win::OSInfo::GetInstance()->wow64_status();  std::cout << "WOW64_DISABLED = 0," << std::endl;  std::cout << "WOW64_ENABLED = 1," << std::endl;  std::cout << "WOW64_UNKNOWN = 2," << std::endl;  std::cout << "My wow64_status is: " << wow64_status << std::endl << std::endl;




  base::win::OSInfo::VersionNumber version_number = base::win::OSInfo::GetInstance()->version_number();  std::cout << "My Version Number: " << std::endl;  std::cout << " major: " << version_number.major << std::endl;  std::cout << " minor: " << version_number.minor << std::endl;  std::cout << " build: " << version_number.build << std::endl;

  system("pause");  return 0;
}

输出结果:

VERSION_PRE_XP = 0,VERSION_SERVER_2003 = 1,VERSION_VISTA = 2,VERSION_WIN7 = 3,VERSION_WIN8 = 4,VERSION_WIN8_1 = 5,VERSION_WIN_LAST = 6,
My Windows Version is: 5

My Processor Model Name is: Intel(R) Core(TM) i7-5500U CPU @ 2.40GHzWOW64_DISABLED = 0,WOW64_ENABLED = 1,WOW64_UNKNOWN = 2,
My wow64_status is: 1

My Version Number:
 major: 6
 minor: 2
 build: 9200

插曲 一个坑儿:

一个VS中有好几个工程,在运行我们想要运行的项目的时候,出现上图中的错误。

如果你不够细心,你就以为是库的版本不对,比如库是64位,而你运行的是32位程序。。。。。。 但是你看编译库的工程也是32位的,这时候就有点发蒙。。。。。 蓦然回首,你没有把想要运行的程序设置为启动项,而且把那个库的工程设置为了启动项。

原文发布于微信公众号 - 程序员的酒和故事(cppdabaojian)

原文发表时间:2017-01-22

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏伦少的博客

Spark通过修改DataFrame的schema给表字段添加注释

通过Spark将关系型数据库(以Oracle为例)的表同步的Hive表,要求用Spark建表,有字段注释的也要加上注释。Spark建表,有两种方法:

843
来自专栏沃趣科技

数据库对象事件与属性统计 | performance_schema全方位介绍

上一篇《事件统计 | performance_schema全方位介绍》详细介绍了performance_schema的事件统计表,但这些统计数据粒度太粗,仅仅按...

1394
来自专栏BinarySec

mmap及linux地址空间随机化失效漏洞

Linux下动态库是通过mmap建立起内存和文件的映射关系。其定义如下void* mmap(void* start,size_t length,int prot...

141
来自专栏F-Stack的专栏

F-Stack 之 kqueue 封装为 epoll 介绍

F-Stack是一个全用户态的高性能的网络接入开发包,基于DPDK、FreeBSD协议栈、微线程接口等,适用于各种需要网络接入的业务

4990
来自专栏一枝花算不算浪漫

Java应用集群下的定时任务处理方案(mysql)

4158
来自专栏JackeyGao的博客

五个很实用的Django 项目推荐

很多Django的包都能很大的增加我们的开发效率或者增加我们项目的功能, 比如: django-rest-framework 、wagtail, 它们带来了很棒...

693
来自专栏张善友的专栏

10招步骤保护IIS服务器安全

问题 IIS(Internet Information Server)是黑客特别喜欢的目标。因此,对于管理IIS网页服务器的管理员来说,确保服务器安全是一件至...

1745
来自专栏数据库新发现

关于shared pool的深入探讨(五)

http://www.eygle.com/internal/shared_pool-5.htm

542
来自专栏乐沙弥的世界

ERROR 1160 (08S01) : Got an error writing communication packets

    应用程序端碰到错误提示,MySQLNonTransientConnectionException:Got an error writing commun...

700
来自专栏智能大石头

2,ORM组件XCode(速览)

    啥也不说,上图: image.png     这是最基本的增删改查代码!     符合X系列组件的一贯作风,不求万能,只求简单实用!    ...

1729

扫描关注云+社区