Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

32 or 64 Bits?

reneeculver
Beginner
1,838 Views
Is there is way for Intel fortran to tell if it's running on 32 or 64bits ? The length of an intptr will tell you in the VS CRF but it don't know how to do it in fortran.

Renee
0 Kudos
1 Solution
JVanB
Valued Contributor II
1,827 Views
[bash]program iswow64
   use iso_c_binding
   use ifwin
   implicit none
   type(c_funptr) cfp
   procedure(IsWow64Process),pointer :: fp
   integer(4) result
   cfp = GetProcAddress(GetModuleHandle("kernel32"//achar(0)),"IsWow64Process"//achar(0))
   if(c_associated(cfp)) then
      call c_f_procpointer(cfp,fp)
      if(fp(GetCurrentProcess(),result) /= 0) then
         if(result /= 0) then
            write(*,'(a)') 'It''s a SysWOW64 Process'
         else
            write(*,'(a)') 'It''s a normal process'
         end if
      else
         write(*,'(a,i0)') 'Strange error ',GetLastError()
      end if
   else
      write(*,'(a)') 'It''s a 32-bit OS'
   end if
end program iswow64
[/bash]

View solution in original post

0 Kudos
23 Replies
Steven_L_Intel1
Employee
1,653 Views
If you want to test if the program was compiled for 64-bit, you can do this:

LOGICAL, PARAMETER :: I_AM_64_BIT = (INT_PTR_KIND() == 8)

If you want to ask if the program is running on a 64-bit OS, no matter how the program was built,. that's a bit harder but still doable.
0 Kudos
reneeculver
Beginner
1,653 Views
Hi Steve,

Yes, I do want to tell two things:

1.) Whether the image was built for 32 or 64 bits.

and

2.) What the system is currently running on. Obviously 64 bit software cannot run on a 32 bit system.

Renee
0 Kudos
Steven_L_Intel1
Employee
1,653 Views
Windows makes it fairly hard to detect that a 32-bit program is running on 64-bit Windows. One way might be to call GET_ENVIRONMENT_VARIABLE for the variable name "ProgramFiles(x86)" and see if it returns anything. If it does, then it is almost certainly a 64-bit system. This variable will not be defined on a 32-bit system.

I have to ask, though - what are you going to do with this information?
0 Kudos
JVanB
Valued Contributor II
1,828 Views
[bash]program iswow64
   use iso_c_binding
   use ifwin
   implicit none
   type(c_funptr) cfp
   procedure(IsWow64Process),pointer :: fp
   integer(4) result
   cfp = GetProcAddress(GetModuleHandle("kernel32"//achar(0)),"IsWow64Process"//achar(0))
   if(c_associated(cfp)) then
      call c_f_procpointer(cfp,fp)
      if(fp(GetCurrentProcess(),result) /= 0) then
         if(result /= 0) then
            write(*,'(a)') 'It''s a SysWOW64 Process'
         else
            write(*,'(a)') 'It''s a normal process'
         end if
      else
         write(*,'(a,i0)') 'Strange error ',GetLastError()
      end if
   else
      write(*,'(a)') 'It''s a 32-bit OS'
   end if
end program iswow64
[/bash]
0 Kudos
reneeculver
Beginner
1,653 Views
Thank you!!!!!
Renee
0 Kudos
reneeculver
Beginner
1,653 Views
UH-oh....

I'm working an taking a historical program called Adventure and Ifwin has conflicting names init. It gives about six errors with IFWIN.

Renee
0 Kudos
reneeculver
Beginner
1,653 Views
I've put it in it's own test program and I am down to some undefines....

ISWOW64PROCESS is a undefined interface.
error #6303: The assignment operation or the binary expression operation is invalid for the data types of the two operands. [GETPROCADDRESS_G1]
error #6404: This name does not have a type, and must have an explicit type. [FP] C:\Users\renee\AppData\Local\Temporary Projects\Test\Test.f90 11
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,653 Views
Then use MSDN to look up the interface and declare it yourself
MSDN shows:

BOOL IsWow64Process(
  HANDLE hProcess,
  PBOOL Wow64Process
);
-------
You transcribe to something like
------





INTERFACE
function IsWow64Process(hProcess, Wow64Process)
use IFWINTY
INTEGER(BOOL) :: IsWow64Process
!DEC$ ATTRIBUTES DEFAULT, STDCALL, DECORATE, ALIAS:'IsWow64Process' :: IsWow64ProcessINTEGER(HANDLE) :: hProcess INTEGER(PBOOL) :: Wow64Process
end function IsWow64Process
end INTERFACE
------



Jim Dempsey
0 Kudos
Steven_L_Intel1
Employee
1,653 Views
Renee, what will you do with this information in the program? I know what you're working on. I just don't see a practical use for knowing if a 32-bit program is running under a 64-bit OS.
0 Kudos
JVanB
Valued Contributor II
1,653 Views
PBOOL means pointer to BOOL so you need something more like:

!DEC$ ATTRIBUTES REFERENCE :: Wow64Process
INTEGER(BOOL) :: Wow64Process

to describe that argument.
0 Kudos
reneeculver
Beginner
1,653 Views

Steve,

They are for identification at the begining of the program. Primary they are for an idividual who attempts to run the 64 Bit program on a 32 bit system and wonders why it wont work. Of course...you would need a 64 bit system to run it For the ID.

Renee

0 Kudos
reneeculver
Beginner
1,653 Views
RO.

Thank you. I'll work on it.

Renee
0 Kudos
Steven_L_Intel1
Employee
1,653 Views
Renee, a user attempting to run a 64-bit executable on a 32-bit system will get an error about an invalid executable. Your code will not run and no amount of detection you add will help. And if the 32-bit code is run on a 64-bit OS - why is that important?

There is no reason for you to build this as a 64-bit program. Just build it as 32-bit and it will run everywhere. I don't understand why you want to add so many levels of complexity to a simple console application.
0 Kudos
reneeculver
Beginner
1,653 Views
Thank you so much steve. I will look into that.

I had a tenacious computer virus today. The way they are selling these diagnostic packages is disgraceful. The thing is just one step ahead of a virus and it makes quite a mess of the registry also. But I'm alright now and I have a very recent backup but I didn't need it.

Renee
0 Kudos
reneeculver
Beginner
1,653 Views
Let's try Again.......

First of all the book I got does not work for me. I use the index to find something and THAT book does not have the word datatype(s) in it???? How then, am I supposed to use such a book? Now if someone could recommend a technical book, I would appreciate it. I am working with a console task, so an emphasis on console tasks isnot out of the question or should I just go to AMAZON?

OK, this question....

I want to know whether my task was compiled and linked with 32 or 64 Bits.

This does not work:

LOGICAL Parameter I_AM_64BIT = (INT_PTR_KIND() == 8)

.
.
.
if ( I_AM_64Bit) then

print 3

3 format ( "yes" )

end if

On a 64 bit machine I_AM_64 is 0 and the contents of the if loop are not processed.

Renee

0 Kudos
TimP
Honored Contributor III
1,653 Views
There are several good downloadable on-line references (European course material) easily found by browser search.
I'm about to order Metcalf, Reid, and Cohen's latest edition
http://www.oup.com/us/catalog/general/subject/ComputerScience/ProgrammingLanguageTheory/?view=usa&ci=9780199601424
as I have a credit at Amazon, and they seem to have stopped expiring my balances.
It's not exactly an easy jump from Fortran 77, but it's one of the more accessible all-round references.
0 Kudos
Steven_L_Intel1
Employee
1,653 Views
Renee,

You did not copy what I wrote exactly - you left out characters. This works:

[fortran]LOGICAL, PARAMETER :: I_AM_64BIT = (INT_PTR_KIND() == 8)

if ( I_AM_64Bit) then

print 3

3 format ( "yes" )

end if
end[/fortran]
[plain]E:Projects>ifort r.f90
Intel Visual Fortran Intel 64 Compiler XE for applications running on Intel 64, Version 12.0.1.127 Build 20101116
Copyright (C) 1985-2010 Intel Corporation.  All rights reserved.

Microsoft  Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:r.exe
-subsystem:console
r.obj

E:Projects>r.exe
yes

E:Projects>[/plain]

[plain]E:Projects>ifort r.f90
Intel Visual Fortran Compiler XE for applications running on IA-32, Version 12.0.1.127 Build 20101116
Copyright (C) 1985-2010 Intel Corporation.  All rights reserved.

Microsoft  Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:r.exe
-subsystem:console
r.obj

E:Projects>r.exe

E:Projects>[/plain]
0 Kudos
reneeculver
Beginner
1,653 Views
Ok Steve, I am trying it again because the following did not discrimate between 64 bits and 32 bits:

call setupexit ! Call the routine which exits rel cleanly from the

c window which contains it.

call GET_ENVIRONMENT_VARIABLE('ProgramFiles(x86)', value1, len,

1 status)

If ( value1 .eq. 'C:\Program File') then

i_am_64bit = true

end if

True and false have been defined correctly....by .true. and .false.(I don't believeI needed to do this when I was at DEC.)

Your running AI32?

Renee

0 Kudos
Steven_L_Intel1
Employee
1,653 Views
Renee,

I am running Windows 7 x64. The test you are using is not the one I have suggested and you have again not written it correctly, and seem to be confused regarding TRUE and FALSE.

The named constants TRUE and FALSE are to be used when calling Windows API routines, as they have different values, by default, than the Fortran LOGICAL constants .TRUE. and .FALSE.. You are calling a Fortran intrinsic procedure for which the STATUS argument is an integer. It is not a TRUE/FALSE value.

The following does what you want:

[fortran]character(80) env
integer status
integer len

call get_environment_variable("ProgramFiles(x86)",env,len,status)

if (status == 0) then
  print *, "System is 64-bit"
else
  print *, "System is 32-bit"
end if

end[/fortran]
You don't want to test the value as it is language-dependent. But I will also say that this test adds no value to the user. You have not explained what you will do with this information.
0 Kudos
Robert_van_Amerongen
New Contributor III
1,477 Views
Renee,

did you ever consider the API routine GetSystemInfo? The SYSTEM_INFO structure that you can obtain with it has the member %wProcessorArchitecture and that shows wether you are running on a x64, an IA64, an x86(Intel) or an IA32_on_win64 o.s.

Robert


0 Kudos
Reply