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

Calling a Win32 API using KERNEL32 module

Jon_D
New Contributor I
376 Views
Hi,

I am trying to convert the C part of a C/Fortran mixed language program to pure Fortran. The C part essentially invokes several file I/O related win32 APIs. One such API is the CreateFile function. The snippet from the origional C code is as follows:

hFile = CreateFile (cname,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);

From reading the definition for CreateFile, I understand that one can "combine" GENERIC_READ and GENERIC_WRITE flags, and I assume that "|" represents the combine operation. When I use the equivalent Fortran function defined in KERNEL32 module, how do I represent the combine operation? Is it an addition or something else?

Thanks for any help,
Jon

0 Kudos
2 Replies
Steven_L_Intel1
Employee
376 Views
The Fortran equivalent of the "|" operator is the IOR intrinsic function. Many people will use .OR. instead, and that will work, but is not standard-conforming as .OR. is defined for use on LOGICAL values only, and you're working with integers. (C does not make the distinction.)

That said, I will sometimes use .OR. when I am feeling lazy. The disadvantage of IOR is that it takes only two arguments so you'd have to write:

IOR(IOR(GENERIC_READ,GENERIC_WRITE),IOR(FILE_SHARE_READ,FILE_SHARE_WRITE))

You could instead use:

GENERIC_READ .OR. GENERIC_WRITE .OR. FILE_SHARE_READ .OR. FILE_SHARE_WRITE

Do not use addition for this, even though it may work in some cases.
0 Kudos
Paul_Curtis
Valued Contributor I
376 Views
INTEGER FUNCTION MOR (f1, f2, f3, f4, f5, f6, f7, f8)
IMPLICIT NONE
INTEGER,INTENT(IN) :: f1, f2
INTEGER,INTENT(IN),OPTIONAL :: f3, f4, f5, f6, f7, f8

! IOR of multiple arguments, as supplied
MOR = IOR(f1, f2)
IF (.NOT.PRESENT(f3)) RETURN
MOR = IOR(MOR,f3)
IF (.NOT.PRESENT(f4)) RETURN
MOR = IOR(MOR,f4)
IF (.NOT.PRESENT(f5)) RETURN
MOR = IOR(MOR,f5)
IF (.NOT.PRESENT(f6)) RETURN
MOR = IOR(MOR,f6)
IF (.NOT.PRESENT(f7)) RETURN
MOR = IOR(MOR,f7)
IF (.NOT.PRESENT(f8)) RETURN
MOR = IOR(MOR,f8)
END FUNCTION MOR
0 Kudos
Reply