C and C++ gets the 32 bit or 64 bit implementation code for the Windows system

  • 2020-05-30 20:52:54
  • OfStack

C/C++ gets the 32-bit or 64-bit implementation code for the Windows system

scenario

1. In the development of Windows 64bit system, there are situations where it is necessary to judge the Program Files path, but the 64bit system has two Program Files or Program Files(x86), at which point it is necessary to obtain the path according to the number of bits in the current system.

instructions

1. It is useless to judge whether the program is 32bit or 64bit, because the 64bit system can run 32bit and 64bit programs.

2.64bit system kernel32.dll has a function interface IsWow64Process, just need to determine whether the dll has this derived function.

example


// test-OSBit.cpp :  Define the entry point for the console application. 
//

#include "stdafx.h"
#include <Windows.h>
#include "Shlobj.h"
#include <iostream>


typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);

static LPFN_ISWOW64PROCESS fnIsWow64Process = NULL;

//
//  FUNCTION: SafeIsWow64Process(HANDLE, PBOOL)
//
//  PURPOSE: This is a wrapper of the IsWow64Process API. It determines 
//  whether the specified process is running under WOW64. IsWow64Process 
//  does not exist prior to Windows XP with SP2 and Window Server 2003 with 
//  SP1. For compatibility with operating systems that do not support 
//  IsWow64Process, call GetProcAddress to detect whether IsWow64Process is 
/// implemented in Kernel32.dll. If GetProcAddress succeeds, it is safe to 
//  call IsWow64Process dynamically. Otherwise, WOW64 is not present.
//
//  PARAMETERS:
//  * hProcess - A handle to the process. 
//  * Wow64Process - A pointer to a value that is set to TRUE if the process 
//   is running under WOW64. If the process is running under 32-bit Windows, 
//   the value is set to FALSE. If the process is a 64-bit application 
//   running under 64-bit Windows, the value is also set to FALSE.
//
//  RETURN VALUE: If the function succeeds, the return value is TRUE.If 
//  IsWow64Process does not exist in kernel32.dll, or the function fails, 
//  the return value is FALSE. 
//
static BOOL WINAPI SafeIsWow64Process(HANDLE hProcess, PBOOL Wow64Process)
{
  if (fnIsWow64Process == NULL)
  {
    // IsWow64Process is not available on all supported versions of 
    // Windows. Use GetModuleHandle to get a handle to the DLL that 
    // contains the function, and GetProcAddress to get a pointer to the 
    // function if available.
    HMODULE hModule = GetModuleHandle(L"kernel32.dll");
    if (hModule == NULL)
    {
      return FALSE;
    }

    fnIsWow64Process = reinterpret_cast<LPFN_ISWOW64PROCESS>(
      GetProcAddress(hModule, "IsWow64Process"));
    if (fnIsWow64Process == NULL)
    {
      return FALSE;
    }
  }
  return fnIsWow64Process(hProcess, Wow64Process);
}

//
//  FUNCTION: Is64BitOS()
//
//  PURPOSE: The function determines whether the current operating system is 
//  a 64-bit operating system.
//
//  RETURN VALUE: The function returns TRUE if the operating system is 
//  64-bit; otherwise, it returns FALSE.
//
static BOOL Is64BitOS()
{
#if defined(_WIN64)
  return TRUE;  // 64-bit programs run only on Win64
#elif defined(_WIN32)
  // 32-bit programs run on both 32-bit and 64-bit Windows
  BOOL f64bitOS = FALSE;
  return (SafeIsWow64Process(GetCurrentProcess(), &f64bitOS) && f64bitOS);
#else
  return FALSE; // 64-bit Windows does not support Win16
#endif
}

int _tmain(int argc, _TCHAR* argv[])
{
  TCHAR folder[MAX_PATH] = {0};
  if(Is64BitOS())
    ::SHGetSpecialFolderPath(NULL,folder,CSIDL_PROGRAM_FILESX86,FALSE);
  else
    ::SHGetSpecialFolderPath(NULL,folder,CSIDL_PROGRAM_FILES,FALSE);
  std::wcout << "32bit Program Files: " << folder << std::endl;
  system("pause");
  return 0;
}

Output:


32bit Program Files: C:\Program Files (x86)

reference

SHGetSpecialFolderPath function

If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!


Related articles: