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

Getting the Windows Handle (Fortran POSIX File Descriptor)

Nick2
New Contributor I
587 Views

The user manual for ifort 2015 Update 4 subroutine PXFFILENO suggests that in the Windows OS, I can get the file descriptor (handle returned from CreateFile) associated with a Fortran File Unit number.  I tried the following code accordingly:

(Fortran:)

      EXTERNAL  UOPEN

      INTEGER(INT_PTR_KIND()) UOPEN

     

      CALL PXFPOSIXIO (1,iold,ierror)

      OPEN(UNIT=10,FILE='UOPEN.DAT',STATUS='NEW',USEROPEN=UOpen)

      write(10,*)"hi"

      flush(10)

      CALL PXFFILENO (10,fd,ierror)

(C++:)

extern "C" __declspec(dllexport) void* UOPEN(

                _In_     char*                 lpFileName,

                _In_     DWORD                 dwDesiredAccess,

                _In_     DWORD                 dwShareMode,

                _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,

                _In_     DWORD                 dwCreationDisposition,

                _In_     DWORD                 dwFlagsAndAttributes,

                _In_opt_ HANDLE                hTemplateFile,

                _In_     int&                  UNIT)//,

                //            _In_     int&                  FLEN)

{

                BOOL bResult1 = CreatePipe(&hReadPipe, &hWritePipe, 0, 100000000);//100e6

                DWORD err1 = GetLastError();

                //HANDLE hMapping = CreateMailslot(L"Some super cool mailslot for MAAP", 0, 1000000, 0);

                // PAGE_READWRITE | SEC_COMMIT

                char*Bytes = new char[100000000];//100e6

                DWORD bytesWritten;

                for (int i = 0; i < 99000000; ++i)

                                Bytes = 'n';

 

                BOOL bResult = WriteFile(hWritePipe, Bytes, 99000000, &bytesWritten, 0);

                DWORD err = GetLastError();

 

                //HANDLE hProcess = GetCurrentProcess();

                //DuplicateHandle(hProcess, hResult, hProcess, &hFile, 0, FALSE, DUPLICATE_SAME_ACCESS);

                //hFile = hResult;

                delete[]Bytes;

                return hWritePipe;

}

 

I know that hWritePipe is 0x88, so I would expect fd to be 0x88.  However, fd comes back as 3.  That is not a valid handle.  So, what am I doing wrong?  Is the description in the user manual wrong?  I very desparately want that handle in the Fortran code to avoid writing spaghetti code.

0 Kudos
4 Replies
Steven_L_Intel1
Employee
587 Views

Your header for UOPN doesn't match the documentation for arguments that are passed by reference, but you don't use these so it probably doesn't matter. I'll admit that I have never played with POSIX I/O so there may be an issue with that. I am playing with some examples and will let you know what I find.

0 Kudos
Scott_L_
New Contributor I
587 Views

 

In the intel fortran documentation (Fortran Compiler 15.0), there is mention of pipe I/O in a few places, but there doesn't seem to be a clear definition of whether fortran statements like open, close, read, write can be applied to a pipe.  Maybe I haven't found the right section.

 

Assigning Files to Logical Units

Most I/O operations involve a disk file, keyboard, or screen display. Other devices can also be used:

  • Sockets can be read from or written to if a USEROPEN routine (usually written in C) is used to open the socket.

  • Pipes opened for read and write access block (wait until data is available) if you issue a READ to an empty pipe.

  • Pipes opened for read-only access return EOF if you issue a READ to an empty pipe.

 

 thanks

scott

 

0 Kudos
Nick_K_6
Beginner
587 Views

Scott - the key "missing" piece is that Fortran talks to the underlying operating system.  In Windows, it creates, and talks to the Windows handles.  So, in my example, I was able to connect Fortran Unit 10 to a Windows handle of my own choosing that represents a pipe.  That handle could have been anything, I just chose a pipe.  The reason it works is because it's the kind of handle with which you (or Fortran runtime) can call WriteFile (just like you can call WriteFile with the handle you get if you open a file on a disk), and I'm using a WRITE statement.  This is probably easier to follow if you're familiar with the relevant parts of the Windows API.

0 Kudos
Steven_L_Intel1
Employee
587 Views

I have part of the answer to this. The POSIX File Descriptor is NOT a Windows file handle and the documentation doesn't say it is. Unfortunately, there doesn't seem to be a way to convert a POSIX File Descriptor to a Windows handle.

0 Kudos
Reply