how to detect OS architecture?

Regan Heath regan at netmail.co.nz
Mon Dec 16 09:04:18 PST 2013


On Mon, 16 Dec 2013 10:53:45 -0000, Hugo Florentino <hugo at acdam.cu> wrote:
> I am writing a launcher to make a Windows application portable, but  
> since this application supports both x86 and x86_64, I would like to  
> detect the architecture of the OS my launcher is being run on, in order  
> to launch the proper executable.
>
> How can I do this?

Compile the launcher as 32bit, and use this global boolean "isWow64":

import std.stdio;
import std.internal.windows.advapi32;

void main(string[] args)
{
   writefln(isWow64 ? "yes" : "no");
}

You can thank Kenji for this one :)  Someone document this somewhere  
please :p

The code from advapi32 for those interested..

immutable bool isWow64;

shared static this()
{
     // WOW64 is the x86 emulator that allows 32-bit Windows-based  
applications to run seamlessly on 64-bit Windows
     // IsWow64Process Function - Minimum supported client - Windows Vista,  
Windows XP with SP2
     alias extern(Windows) BOOL function(HANDLE, PBOOL) fptr_t;
     auto hKernel = GetModuleHandleA("kernel32");
     auto IsWow64Process = cast(fptr_t) GetProcAddress(hKernel,  
"IsWow64Process");
     BOOL bIsWow64;
     isWow64 = IsWow64Process && IsWow64Process(GetCurrentProcess(),  
&bIsWow64) && bIsWow64;
}

Basically, your 32 bit launcher process has to, at runtime, ask Kernel32  
for a function "IsWow64Process" which only exists on 64 bit windows.  So,  
if it doesn't find it, it's 32 bit.  If it finds it, it calls it with the  
current PID to find out of the current process is a 32 bit app running  
inside WOW64 (the emulator layer) and that gives you the answer.

R


-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list