Silverfrost Forums

Welcome to our forums

How to find Operating System the program is running on ?

24 Jun 2012 4:55 #10417

There appears to be nothing wrong with the code for get_os_ver@. Just a straight call to GetVersionEx.

The problem is probably revealed by the following comment copied from the MSDN topic on GetVersionEx....

If compatibility mode is in effect, the GetVersionEx function reports the operating system as it identifies itself, which may not be the operating system that is installed. For example, if compatibility mode is in effect, GetVersionEx reports the operating system that is selected for application compatibility.

If all else fails there will be a registry setting somewhere that will identify the root operating system.

25 Jun 2012 9:58 #10426

Yes, thanks that you have confirmed my feelings about the whole situation and for good suggestion. As to WinAPI - do you or anyone here remember any functions which expose the core operating system ? ....i think i have met something like this before

26 Jun 2012 6:50 #10434

You can find the information in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion but there ought to be a simpler way.

If no one can suggest a better way then I will add a new routine to the library that will extract the relevant information (or maybe change get_os_ver@).

27 Jun 2012 11:59 #10437

Still searching for simple way...May be John Horspool will remind us which WinAPI function reported Windows OS ? I think i played with one of his suggestions and have seen along the other things as a side effect some function told about Windows version. Or may be it was even some of Clearwin ?!

28 Jun 2012 8:11 #10443

I don't know if this can help, but according to Paul's remark I wrote a little test:

      WINAPP

      program test

      IMPLICIT NONE

      include 'win32api.ins'

      INTEGER,PARAMETER::HKEY_LOCAL_MACHINE=Z'80000002'
      integer*4      hKey,l,j
      character*256  RegPath,version,string

      RegPath = 'Software\\Microsoft\\Windows NT\\CurrentVersion'

      j = RegOpenKey(HKEY_LOCAL_MACHINE,trim(RegPath),hKey)
      if (j .eq. 0) then
        version = ' '
        l = 256
        j = RegQueryValueEx(hKey,'ProductName',
     *    CCORE1(0),CCORE1(0),version,l)
        string = 'OS '//trim(version)
        print*,string(1:len_trim(string))
        version = ' '
        l = 256
        j = RegQueryValueEx(hKey,'CurrentVersion',
     *    CCORE1(0),CCORE1(0),version,l)
        string = 'Version '//trim(version)//', build'
        version = ' '
        l = 256
        j = RegQueryValueEx(hKey,'CurrentBuildNumber',
     *    CCORE1(0),CCORE1(0),version,l)
        string = trim(string)//' '//trim(version)
        print*,string(1:len_trim(string))
      end if
      j = RegCloseKey(hKey)

      RegPath = 'SYSTEM\\CurrentControlSet\\Control\\ProductOptions'
      j = RegOpenKey(HKEY_LOCAL_MACHINE,trim(RegPath),hKey)
      if (j .eq. 0) then
        version = ' '
        l = 256
        j = RegQueryValueEx(hKey,'ProductSuite',
     *    CCORE1(0),CCORE1(0),version,l)
        print*,version(1:len_trim(version))
      end if
      j = RegCloseKey(hKey)

      end

You may compare the results with those from winver.exe. Surely there are more parameters of interest in the registry database.

Regards - Wilfried

28 Jun 2012 2:28 #10444

Addition to see whether Windows7 is installed as 32- or 64-bit version:

      integer*2   dummy

      j = 64
      call attach@('c:\\windows\\syswow64',dummy)
      if (dummy .ne. 0) j = 32
      print*,j

Regards - Wilfried

29 Jun 2012 3:30 #10448

Thanks Wilfried for the shot. Well, seems this task is harder then it looks. Getting with this method the same story as with previous method. As soon as EXE is in compatibility mode with XP, it detects XP not Win7.

It was interesting though how you have detected 32 vs 64 bit OS....May be meantime i will use it as a temporal indirect solution, because my real XP was 32 bit and Win7 is 64bit 😃. By the way will it work with the 32bit Win7 ?

3 Jul 2012 1:50 #10458

I can confirm that the registry value above also changes with the compatibility mode. So I am stuck for now.

See also http://blogs.msdn.com/b/patricka/archive/2010/01/14/common-problems-with-checking-os-versions-in-code.aspx

3 Jul 2012 2:55 #10459

If I change the first RegPath line in my example above from

      RegPath = 'Software\\Microsoft\\Windows NT\\CurrentVersion'

to

      RegPath = 
     * 'Software\\Wow6432Node\\Microsoft\\Windows NT\\CurrentVersion'

then I get the message 'Windows 7' even if I run the programme in compatibility mode (XP). May be this helps?

If yes, then to prevent problems on PCs with 'real' XP operation system I would replace the first line by

      RegPath = 
     * 'Software\\Wow6432Node\\Microsoft\\Windows NT\\CurrentVersion'
      j = RegOpenKey(HKEY_LOCAL_MACHINE,trim(RegPath),hKey)
      if (j .ne. 0) then
        RegPath = 'Software\\Microsoft\\Windows NT\\CurrentVersion'
        j = RegOpenKey(HKEY_LOCAL_MACHINE,trim(RegPath),hKey)
      end if

Regards - Wilfried

3 Jul 2012 6:15 #10460

Holly...that works...wow people started doing real wizardry here...i even still not get what and how 😃. Nice to see the little miracle done. Kudos and big thanks, Wilfried!!! Thanks all for the efforts.

4 Jul 2012 5:49 #10461

Brilliant! I will see if I can build that into a library function for future use.

4 Jul 2012 6:57 #10462

Here is some modified code (f95) - see the subroutine get_os_info:

WINAPP

program test

IMPLICIT NONE

integer*4       bits,err_code
character*256   name,version,build

call get_os_info(name,version,build,bits,err_code)
print*,'Name     ',name(1:len_trim(name))
print*,'Version  ',version(1:len_trim(version))
print*,'Build    ',build(1:len_trim(build))
print*,'Bits     ',bits
print*,'err_code ',err_code
end

! ---------------------------------------------------------------------

subroutine get_os_info(name,version,build,bits,err_code)

IMPLICIT NONE
include 'win32api.ins'

integer,parameter::HKEY_LOCAL_MACHINE=Z'80000002'
integer*4       hKey,l,j,bits,err_code
character*256   RegPath,name,version,build

err_code = -1
name     = ' '
version  = ' '
build    = ' '
bits     = 32

RegPath  = 'Software\\Wow6432Node\\Microsoft\\Windows NT\\CurrentVersion'
j = RegOpenKey(HKEY_LOCAL_MACHINE,trim(RegPath),hKey)
if (j /= 0) then
  RegPath = 'Software\\Microsoft\\Windows NT\\CurrentVersion'
  j = RegOpenKey(HKEY_LOCAL_MACHINE,trim(RegPath),hKey)
else
  bits = 64
end if

if (j == 0) then
  l = 256
  j = RegQueryValueEx(hKey,'ProductName',CCORE1(0),CCORE1(0),name,l)
  l = 256
  j = RegQueryValueEx(hKey,'CurrentVersion',CCORE1(0),CCORE1(0),version,l)
  l = 256
  j = RegQueryValueEx(hKey,'CurrentBuildNumber',CCORE1(0),CCORE1(0),build,l)
end if
j = RegCloseKey(hKey)

err_code = 0
return
end

Regards - Wilfried

5 Jul 2012 7:40 #10465

Works like charm! Thanks again

6 Jul 2012 10:13 #10469

I have added a new routine called get_wow_ver@ for the next release. This provides the machine operating system whilst get_os_ver@ remains unchanged and provides the compatible operating system when the executable runs in compatibility mode.

7 Jul 2012 5:10 #10473

Thanks, Paul

27 Apr 2014 8:28 #14014

Yes whenever you call GET_OS_VER@ in a large program, the result is not correct for windows 8. However, with a trick you can avoid such problem,

just write the following program:

winapp program getver use mswin implicit none

INTEGER PLATFORMID, MAJOR, MINOR OPEN(UNIT=40,FILE='winver.92',FORM='UNFORMATTED') call GET_OS_VER@( PLATFORMID, MAJOR, MINOR ) write(40)MAJOR, MINOR,PLATFORMID stop end

This writes the result in a file and then in your large program read these values, it would be correct.

Please login to reply.