PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CppCheckOSBitness/ReadMe.txt

#
Plain Text | 147 lines | 117 code | 30 blank | 0 comment | 0 complexity | bd2a62e43e89bf44b86d68667d0527c9 MD5 | raw file
  1. ========================================================================
  2. CONSOLE APPLICATION : CppCheckOSBitness Project Overview
  3. ========================================================================
  4. /////////////////////////////////////////////////////////////////////////////
  5. Summary:
  6. The code sample demonstrates how to determine whether the operating system of
  7. the current machine or any remote machine is a 64-bit operating system.
  8. /////////////////////////////////////////////////////////////////////////////
  9. Demo:
  10. The following steps walk through a demonstration of the sample.
  11. Step1. After you successfully build the sample project in Visual Studio 2008,
  12. you will get an application: CppCheckOSBitness.exe.
  13. Step2. Run the application in a command prompt (cmd.exe) on a 32-bit
  14. operating system (e.g. Windows 7 x86). The application prints the following
  15. content in the command prompt:
  16. Current OS is not 64-bit
  17. Current OS is not 64-bit
  18. It dictates that the current operating system is not a 64-bit operating
  19. system.
  20. /////////////////////////////////////////////////////////////////////////////
  21. Implementation:
  22. The sample introduces two solutions of detecting programmatically whether you
  23. are running on 64-bit operating system.
  24. Solution 1. Use the IsWow64Process function
  25. //
  26. // FUNCTION: Is64BitOS()
  27. //
  28. // PURPOSE: The function determines whether the current operating system is
  29. // a 64-bit operating system.
  30. //
  31. // RETURN VALUE: The function returns TRUE if the operating system is
  32. // 64-bit; otherwise, it returns FALSE.
  33. //
  34. BOOL Is64BitOS()
  35. If the running process is a 64-bit process, the operating system must be a
  36. 64-bit operating system.
  37. #if defined(_WIN64)
  38. return TRUE; // 64-bit programs run only on Win64
  39. To detect programmatically whether your 32-bit program is running on 64-bit
  40. operating system, you can use the IsWow64Process function.
  41. #elif defined(_WIN32)
  42. // 32-bit programs run on both 32-bit and 64-bit Windows
  43. BOOL f64bitOS = FALSE;
  44. return (SafeIsWow64Process(GetCurrentProcess(), &f64bitOS) && f64bitOS);
  45. SafeIsWow64Process is a wrapper of the IsWow64Process API. It determines
  46. whether the specified process is running under WOW64. IsWow64Process does not
  47. exist prior to Windows XP with SP2 and Window Server 2003 with SP1. For
  48. compatibility with operating systems that do not support IsWow64Process, call
  49. GetProcAddress to detect whether IsWow64Process is implemented in
  50. Kernel32.dll. If GetProcAddress succeeds, it is safe to call IsWow64Process
  51. dynamically. Otherwise, WOW64 is not present.
  52. Solution 2. Query the Win32_Processor WMI class's AddressWidth property
  53. //
  54. // FUNCTION: Is64BitOS(LPCWSTR, LPCWSTR, LPCWSTR)
  55. //
  56. // PURPOSE: The function determines whether the operating system of the
  57. // current machine of any remote machine is a 64-bit operating system
  58. // through Windows Management Instrumentation (WMI).
  59. //
  60. // PARAMETERS:
  61. // * pszMachineName - the full computer name or IP address of the target
  62. // machine. "." or NULL means the local machine.
  63. // * pszUserName - the user name you need for a connection. A null value
  64. // indicates the current security context. If the user name is from a
  65. // domain other than the current domain, the string should contain the
  66. // domain name and user name, separated by a backslash: string 'username'
  67. // = "DomainName\\UserName".
  68. // * pszPassword - the password for the specified user.
  69. //
  70. // RETURN VALUE: The function returns true if the operating system is
  71. // 64-bit; otherwise, it returns false.
  72. //
  73. // EXCEPTION: If this function fails, it throws a C++ exception which
  74. // contains the HRESULT of the failure. For example,
  75. // WBEM_E_LOCAL_CREDENTIALS (0x80041064) is thrown when user credentials
  76. // (pszUserName, pszPassword) are specified for local connections.
  77. // COR_E_UNAUTHORIZEDACCESS (0x80070005) is thrown because of incorrect
  78. // user name or password.
  79. // RPC_S_SERVER_UNAVAILABLE (0x800706BA) is usually caused by the firewall
  80. // on the target machine that blocks the WMI connection or some network
  81. // problem.
  82. //
  83. // EXAMPLE CALL:
  84. // try
  85. // {
  86. // f64bitOS = Is64BitOS(L".", NULL, NULL);
  87. // wprintf(L"Current OS %s 64-bit.\n",
  88. // f64bitOS ? L"is" : L"is not");
  89. // }
  90. // catch (HRESULT hr)
  91. // {
  92. // wprintf(L"Is64BitOS failed with HRESULT 0x%08lx\n", hr);
  93. // }
  94. //
  95. BOOL Is64BitOS(LPCWSTR pszMachineName, LPCWSTR pszUserName,
  96. LPCWSTR pszPassword);
  97. It queries Win32_Processor.AddressWidth which dicates the current operating
  98. mode of the processor (on a 32-bit OS, it would be 32; on a 64-bit OS, it
  99. would be 64). In contrast, Win32_Processor.DataWidth indicates the capability
  100. of the processor. On a 64-bit processor, it is "64". The OSArchitecture
  101. property of the Win32_OperatingSystem WMI class can also tell the bitness of
  102. OS. On a 32-bit OS, it would be "32-bit". However, the property is only
  103. available on Windows Vista and newer operating systems.
  104. Note: The first solution of using IsWow64Process is the preferred way to
  105. detect OS bitness of the current system because it is much easier and faster.
  106. The WMI solution is useful when you want to find this information on a remote
  107. system. The remote computer must be configured for remote connections of WMI:
  108. http://msdn.microsoft.com/en-us/library/aa389290(VS.85).aspx
  109. /////////////////////////////////////////////////////////////////////////////
  110. References:
  111. How to detect programmatically whether you are running on 64-bit Windows
  112. http://blogs.msdn.com/oldnewthing/archive/2005/02/01/364563.aspx
  113. MSDN: Win32_Processor Class
  114. http://msdn.microsoft.com/en-us/library/aa394373(VS.85).aspx
  115. MSDN: Win32_OperatingSystem Class
  116. http://msdn.microsoft.com/en-us/library/aa394239(VS.85).aspx
  117. /////////////////////////////////////////////////////////////////////////////