From: cg on
Hello,

I developp a win32 application and i want this application runs under
Windows Server 2008 x64 / x32 - Vista x64 / x32 - Seven x64 / x32, and i
need to make some modification for my application to be x64 perfectly
compliant.
My problem is : how to know if my application runs under an Os 64bits or
32bits.

I try the GetSystemInfo with the wProcessorArchitecture entry which
curiously return 0.

Curiously GetVersionEx can't be used.

Help would be grateful.

Chris.


From: Leo Davidson on
On Apr 8, 11:09 am, "cg" <c...(a)nospam.fr> wrote:
> My problem is : how to know if my application runs under an Os 64bits or

Here's one way:

// From http://blogs.msdn.com/oldnewthing/archive/2005/02/01/364563.aspx
bool Is64BitWindows()
{
#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 so must
sniff
BOOL f64 = FALSE;
return (::IsWow64Process(::GetCurrentProcess(), &f64) && f64);
#else
return false; // Win64 does not support Win16
#endif
}
From: Stefan Kuhr on
Hello Chris,

On 4/8/2010 12:09 PM, cg wrote:
> Hello,
>
> I developp a win32 application and i want this application runs under
> Windows Server 2008 x64 / x32 - Vista x64 / x32 - Seven x64 / x32, and i
> need to make some modification for my application to be x64 perfectly
> compliant.
> My problem is : how to know if my application runs under an Os 64bits or
> 32bits.
>
> I try the GetSystemInfo with the wProcessorArchitecture entry which
> curiously return 0.
>
> Curiously GetVersionEx can't be used.
>
> Help would be grateful.
>
> Chris.
>
>

You might find the following snippet useful:

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


BOOL IsRunningUnderWow64(void)
{
BOOL bIsWow64 = FALSE;

#ifndef _WIN64
HMODULE hModKrn32 = GetModuleHandleW(L"kernel32");
LPFN_ISWOW64PROCESS fnIsWow64Process = NULL;
__analysis_assert(hModKrn32);
fnIsWow64Process =
(LPFN_ISWOW64PROCESS)GetProcAddress(hModKrn32,"IsWow64Process");

if (NULL != fnIsWow64Process)
{
if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
bIsWow64 = FALSE;
}
#endif // _WIN64

return bIsWow64;
}

From: Brett on

I do this a lot for detecting which driver (kernel mode app) to load. I've
posted my stripped-down code below for both Win32 and .NET.

*** For my Win32 apps ***
DWORD CDriverMgr::GetID()
{
// We need to detect the correct OS version so we can load the right driver
OSVERSIONINFO osv;
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

if(!GetVersionEx(&osv))
return 0;

// Win95 = 4.0(Same as WinNT)
// Win98 = 4.10
// WinME = 4.90
// WinNT = 4.0(Same as Win95)
// Win2k = 5.0
// WinXP = 5.1
// WinXP64 = 5.2(Same as Win2003)
// Win2003 = 5.2(Same as WinXP64)
// WinVista = 6.0
// WinLH = 6.0

if(osv.dwPlatformId == 1) // WinME, Win98, Win95
return ID_WXP32;

if(osv.dwMajorVersion == 4)// All the 32-bit non-Vista versions except XP
return ID_WXP32;

if(osv.dwMajorVersion == 5 && osv.dwMinorVersion != 2)// WinXP32 and Win2000
return ID_WXP32;

// Anything else could be 64-bit, figure out if it is...
if(!m_bIsWow64) // If we've already been through this and decided it's
Wow64, then don't bother
{
if(!Is64Bit(&m_bIsWow64)) // If it errors out, it's most likely that it
can't find that particular function (Win2k, 98, 95, ME, NT, etc...)
m_bIsWow64 = FALSE;
}

if(osv.dwMajorVersion == 6)
{
if(m_bIsWow64)
return ID_WV64;
else
return ID_WV32;
}

if(m_bIsWow64)
return ID_WXP64;
else
return ID_WXP32;
}

inline BOOL CDriverMgr::Is64Bit(PBOOL pb64Bit)
{
*pb64Bit = FALSE;
LPFN_ISWOW64PROCESS fnIsWow64Process =
(LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"),"IsWow64Process");
if(fnIsWow64Process != NULL)
{
if(!fnIsWow64Process(GetCurrentProcess(), pb64Bit))
return FALSE;
}
else
{
*pb64Bit = FALSE; // If the function doesn't exist, then it's not 64-bit
return TRUE;
}

return TRUE;
}


*** For .NET ***
#region OS Detection and Driver Selection
private enum DriverVersion { None, WindowsXP_32, WindowsXP_64,
WindowsVista_32, WindowsVista_64 };

[StructLayout(LayoutKind.Sequential)]
internal struct SYSTEM_INFO
{
public ushort wProcessorArchitecture;
public ushort wReserved;
public uint dwPageSize;
public IntPtr lpMinimumApplicationAddress;
public IntPtr lpMaximumApplicationAddress;
public UIntPtr dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public ushort wProcessorLevel;
public ushort wProcessorRevision;
};

[DllImport("kernel32.dll")]
internal static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);

[DllImport("kernel32.dll")]
internal static extern void GetSystemInfo(ref SYSTEM_INFO lpSystemInfo);

private DriverVersion DetermineDriverVersion()
{
OperatingSystem osInfo = System.Environment.OSVersion;
SYSTEM_INFO sysInfo = new SYSTEM_INFO();

switch (osInfo.Version.Major)
{
case 6: // Vista and Win7
GetNativeSystemInfo(ref sysInfo);
if (sysInfo.wProcessorArchitecture == 9) // x64
return DriverVersion.WindowsVista_64;
else // x86 or something else
return DriverVersion.WindowsVista_32;
case 5: // WinXP and Win2k
if (osInfo.Version.Minor >= 1) // WinXP or later
GetNativeSystemInfo(ref sysInfo);
else
GetSystemInfo(ref sysInfo);
if (sysInfo.wProcessorArchitecture == 9) // x64
return DriverVersion.WindowsXP_64;
else // x86 or something else
return DriverVersion.WindowsXP_32;
case 4: // WinNT4 Win98 WinME
return DriverVersion.WindowsXP_32;
default:
break;
}
return DriverVersion.None;
}
#endregion


"cg" wrote:

> Hello,
>
> I developp a win32 application and i want this application runs under
> Windows Server 2008 x64 / x32 - Vista x64 / x32 - Seven x64 / x32, and i
> need to make some modification for my application to be x64 perfectly
> compliant.
> My problem is : how to know if my application runs under an Os 64bits or
> 32bits.
>
> I try the GetSystemInfo with the wProcessorArchitecture entry which
> curiously return 0.
>
> Curiously GetVersionEx can't be used.
>
> Help would be grateful.
>
> Chris.
>
>
> .
>
From: Dee Earley on
On 08/04/2010 11:09, cg wrote:
> Hello,
>
> I developp a win32 application and i want this application runs under
> Windows Server 2008 x64 / x32 - Vista x64 / x32 - Seven x64 / x32, and i
> need to make some modification for my application to be x64 perfectly
> compliant.

If your app is a 32bit app then it should work exactly as it did before.

> My problem is : how to know if my application runs under an Os 64bits or
> 32bits.
>
> I try the GetSystemInfo with the wProcessorArchitecture entry which
> curiously return 0.
>
> Curiously GetVersionEx can't be used.

I think it lies to your app if it's 32-bit. (It doesn't say in MSDN so I
can't say this definitively.)
If you compile a 64bit version you will get the correct answer.

--
Dee Earley (dee.earley(a)icode.co.uk)
i-Catcher Development Team

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)