PageRenderTime 16ms CodeModel.GetById 6ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

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