how to detect OS architecture?

Gary Willoughby dev at nomad.so
Tue Dec 17 06:13:18 PST 2013


On Monday, 16 December 2013 at 21:23:11 UTC, Hugo Florentino
wrote:
> GetNativeSystemInfo worked. Thanks!
>
> The code ended being like this (it seems to be working both in 
> x86 and x86_64):
>
> import std.file: exists, getcwd;
> import std.path: buildPath, dirName;
> import std.string: format, toStringz;
> import core.sys.windows.windows;
>
> enum X86   = 0;
> enum AMD64 = 9;
> immutable static auto appname = "myapp";
>
> extern(Windows) HANDLE ShellExecuteA(HWND, LPCSTR, LPCSTR, 
> LPCSTR, LPCSTR, int);
> extern(Windows) int MessageBoxA(HWND, LPCSTR, LPCSTR, UINT);
>
> int main() {
>   auto appath = getcwd();
>   string appexe;
>   SYSTEM_INFO env;
>   GetNativeSystemInfo(&env);
>   switch(env.wProcessorArchitecture) {
>     case X86: appexe = buildPath(appath, appname ~ "32.exe"); 
> break;
>     case AMD64: appexe = buildPath(appath, appname ~ "64.exe"); 
> break;
>     default:
>       MessageBoxA(null, "System architecture is not 
> supported.", "Error", MB_ICONHAND + MB_OK);
>       return -1;
>   }
>   if (exists(appexe)) {
>     auto param = format(`/ini="%s"`, buildPath(appath, appname 
> ~ ".ini"));
>     ShellExecuteA(null, "", toStringz(appexe), 
> toStringz(param), "", SW_SHOWMAXIMIZED);
>     scope(failure) return -2;
>   }
>   return 0;
> }

Make sure you handle if users have a 32bit OS installed on a
64bit PC.


More information about the Digitalmars-d-learn mailing list