how to detect OS architecture?

Gary Willoughby dev at nomad.so
Mon Dec 16 07:18:28 PST 2013


On Monday, 16 December 2013 at 13:19:52 UTC, Hugo Florentino 
wrote:
> On Mon, 16 Dec 2013 12:59:52 +0100, John Colvin wrote:
>> On Monday, 16 December 2013 at 11:56:07 UTC, Hugo Florentino 
>> wrote:
>>> On Mon, 16 Dec 2013 12:40:17 +0100, MrSmith wrote:
>>>> I think this is what he want
>>>> http://dlang.org/phobos/core_cpuid.html#.isX86_64
>>>
>>> Thanks, that's precisely what I needed :)
>>
>> Are you sure?
>>
>> This will tell you about the processor, but not necessarily 
>> about
>> what the OS supports. I don't know, but you may find that when 
>> using
>> windows 32bit on an x64 machine, cpuid will tell you the cpu 
>> is 64bit,
>> but the OS won't let you run any 64bit code.
>
> You are right. I realized that this function was not quite what 
> I needed when running this code on a 32 bit system:
>
> import std.stdio, core.cpuid;
>
> int main() {
>   immutable auto appname = "myapp";
>   auto appversion = !isX86_64() ? appname ~ "32" : appname ~ 
> "64") ~ ".exe";
>   scope(failure) return -1;
>   writeln(appversion);
>   return 0;
> }
>
> I was expecting "myapp32.exe" but got "myapp64.exe". Apparently 
> what isX86_64() detects is the capability of the processor, not 
> the arquitecture of the OS.
>
> So currently D has no specific function for detecting the OS 
> architecture at runtime? I had not expected this.
>
> I will try using the other options though. Thanks

Try building the launcher as a 32bit executable then use the 
following code:

import std.stdio;
import core.sys.windows.windows;

enum AMD64   = 9;
enum IA64    = 6;
enum X86     = 0;
enum UNKNOWN = 0xFFFF;

void main()
{
	SYSTEM_INFO executableEnvironment;
	SYSTEM_INFO outerEnvironment;

	GetSystemInfo(&executableEnvironment);
	GetNativeSystemInfo(&outerEnvironment);

	// See the above enums for values.
	writefln("Executable: %s", 
executableEnvironment.wProcessorArchitecture);
	writefln("Outer: %s", outerEnvironment.wProcessorArchitecture);
}

If the launcher is running under Wow64 (the 32bit emulation layer 
on a 64bit processor) the results will be different for 
executableEnvironment and outerEnvironment. GetSystemInfo gets 
the environment the executable is running under, 
GetNativeSystemInfo gets the 'real' environment. I guess it's a 
place to start?


More information about the Digitalmars-d-learn mailing list