Recent

Author Topic: How to detect which OS and bitness my app is running on?  (Read 14341 times)

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: How to detect which OS and bitness my app is running on?
« Reply #15 on: February 05, 2019, 08:15:56 pm »
You should not rely on the Registry to get OS details.  You should ask the OS itself instead.

Except that the OS lies in some of its APIs...  ::)

But that's good advice, given that many of the registry processes can be written by other users/processes.  And that those results are not really documented.

It's kind of a shame that this data is not presented as easily by the OS itself, and it would be nice to have a platform independent way to get this from the OSes.

Since my development is 99.999% Windows, I was able to successfully use the following:  wiki.lazarus.freepascal.org/Detect_Windows_x32-x64_example
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: How to detect which OS and bitness my app is running on?
« Reply #16 on: February 05, 2019, 09:32:51 pm »
You should not rely on the Registry to get OS details.  You should ask the OS itself instead.

Except that the OS lies in some of its APIs...  ::)

Only some do. I mentioned earlier which ones do and which ones do not.

Since my development is 99.999% Windows, I was able to successfully use the following:  wiki.lazarus.freepascal.org/Detect_Windows_x32-x64_example

Using GetNativeSystemInfo() instead of IsWow64Process() (which, BTW, has been deprecated in favor of IsWow64Process2() in Windows 10) would have made more sense in that example.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: How to detect which OS and bitness my app is running on?
« Reply #17 on: February 05, 2019, 09:42:34 pm »
Only some do. I mentioned earlier which ones do and which ones do not.

Indeed, you did.   I was just being cheeky.  :P

...(which, BTW, has been deprecated in favor of IsWow64Process2() in Windows 10)...


Yep, I saw that when I was researching this yesterday.
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

440bx

  • Hero Member
  • *****
  • Posts: 3945
Re: How to detect which OS and bitness my app is running on?
« Reply #18 on: February 05, 2019, 10:22:14 pm »
Using GetNativeSystemInfo() instead of IsWow64Process() (which, BTW, has been deprecated in favor of IsWow64Process2() in Windows 10) would have made more sense in that example.
I haven't tried this but, it seems that a 32bit program can simply make a call to GetModuleHandle('wow64.dll') to determine the O/S bitness.

Wow64.dll is one of the dlls that implements the thunking from 32 to 64bit.  It is not present in a 32bit O/S and must be present in all 32bit processes running under a 64bit O/S.   Its presence or absence would seem to be a reliable indicator of O/S bitness.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: How to detect which OS and bitness my app is running on?
« Reply #19 on: February 06, 2019, 02:03:24 am »
I haven't tried this but, it seems that a 32bit program can simply make a call to GetModuleHandle('wow64.dll') to determine the O/S bitness.

Wow64.dll is one of the dlls that implements the thunking from 32 to 64bit.  It is not present in a 32bit O/S and must be present in all 32bit processes running under a 64bit O/S.   Its presence or absence would seem to be a reliable indicator of O/S bitness.

That is relying on a private implementation detail that could change in future versions.  And besides, you would still have to differentiate between 32bit native and 64bit native when the DLL is not present. What is wrong with just ASKING THE OS using established APIs that exist for this very purpose?
« Last Edit: February 06, 2019, 02:05:27 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

440bx

  • Hero Member
  • *****
  • Posts: 3945
Re: How to detect which OS and bitness my app is running on?
« Reply #20 on: February 06, 2019, 04:21:43 am »
That is relying on a private implementation detail that could change in future versions.
That is true but, quite unlikely.  MS goes out of its way to preserve dll names and their associated functionality.

And besides, you would still have to differentiate between 32bit native and 64bit native when the DLL is not present.
I am unclear as to what you mean there.  On a pure 32bit OS, the dll is not present/loaded (quite sensible since there is no 64bit layer to thunk to.)  Did I miss your point ?  Also, when using GetNativeSystemInfo one has to rely on the value returned in ProcessorArchitecture to determine the bitness, it would be much simpler for MS to "fudge" the value returned in that field if it served its purposes than to change the name and/or functionality of a key system dll.

What is wrong with just ASKING THE OS using established APIs that exist for this very purpose?
Usually little to nothing but, unfortunately, MS has indulged in returning values _they_ want the programmers to use instead of the real/actual values.  This seems to be particularly true when it comes to information about the OS (e.g, system directories and their actual contents - ironically, that "white redirection" (they don't lie, they redirect ... chuckle) could be used to determine OS bitness too.)

Even with IsWow64Process, https://msdn.microsoft.com/en-us/windows/desktop/ms684139 MS states
Quote
Note that this technique [ed: using IsWow64Process] is not a reliable way to detect whether the operating system is a 64-bit version of Windows because the Kernel32.dll in current versions of 32-bit Windows also contains this function.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: How to detect which OS and bitness my app is running on?
« Reply #21 on: February 06, 2019, 10:03:57 am »
Even with IsWow64Process, https://msdn.microsoft.com/en-us/windows/desktop/ms684139 MS states
Quote
Note that this technique [ed: using IsWow64Process] is not a reliable way to detect whether the operating system is a 64-bit version of Windows because the Kernel32.dll in current versions of 32-bit Windows also contains this function.
You should quote this correctly:
Quote
For compatibility with operating systems that do not support this function, call GetProcAddress to detect whether IsWow64Process is implemented in Kernel32.dll. If GetProcAddress succeeds, it is safe to call this function. Otherwise, WOW64 is not present. Note that this technique is not a reliable way to detect whether the operating system is a 64-bit version of Windows because the Kernel32.dll in current versions of 32-bit Windows also contains this function.
This means that it is not sufficient to detect the bitness of the OS by only checking that the function exists. You need to call it.
And if available you should use IsWow64Process2 instead as that also works correctly for i386 binaries running on a AArch64 Windows (Windows 10 on ARM).

440bx

  • Hero Member
  • *****
  • Posts: 3945
Re: How to detect which OS and bitness my app is running on?
« Reply #22 on: February 06, 2019, 02:13:21 pm »
You should quote this correctly:
This means that it is not sufficient to detect the bitness of the OS by only checking that the function exists. You need to call it.
Just for the record, it wasn't my intention to mislead.  When I read that passage, I didn't get what they were saying.  I believe your interpretation of that passage is correct. 

Thank you for the clarification.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: How to detect which OS and bitness my app is running on?
« Reply #23 on: September 21, 2019, 04:03:01 pm »
Yes, you are right.

If possible, I do not want to have Java as a prerequisite for my Lazarus Free Pascal project.
That's why I wrote it for fun ;) I don't want Java all over the place either ;)
But I am working on it along the lines me and Fungus explained.
(That will work for windows, linux (incl arm, and Mac OSX).

Five routines:
- OS
- BItness
- Version
- Endianness.
- CPU architecture  (restricted)

All of this is alreadyy available in the standard FPC libraries, but not unified.
Is your unified solution already on forthcoming FPC 3.2.0?
Thanks.

 

TinyPortal © 2005-2018