OpenFileMapping between C++ and Fortran

OpenFileMapping between C++ and Fortran

Hi, Im trying to get a C++ and Fortran program to send each other data. I have used the http://msdn.microsoft.com/en-us/library/windows/desktop/aa366551(v=vs.85).aspx example successfully. Right now Im trying to port the "second process" to fortran code.

I'm not very expirienced in win32 programming let alone fortran so pardon my ignorance if any.

Here's the fortran side of things:

PROGRAM secondprocess

USE KERNEL32, only: OpenFileMapping, MapViewOfFile, FILE_MAP_ALL_ACCESS

INTEGER*4 hMapFile
INTEGER*4 pBuf
INTEGER*4 temp
CHARACTER(256) szName

szName = "Global\MyFileMappingObject"

hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, .FALSE., szName)
if(hMapFile == 0) then
write(*,*) "OpenFileMapping failed"
write(*,*) GetLastError();
else
pBuf = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 256)
end if

write(*,*) pBuf
write(*,*) szName

CALL sleep(2)

END PROGRAM secondprocess

When I run the program hMapfile fails, and i get an NaN error ("Not a Number" from what i've read)

I get the feeling that pointers should be involved, eg pBuf is declared as an integer yet i think it stores the memory location of the message.

Any ideas ?

5 Beiträge / 0 neu
Letzter Beitrag
Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.

szName = "Global\MyFileMappingObject"//achar(0)
Also you might consider
USE IFWINTY, ONLY: FALSE, LPVOID, HANDLE
and then pass FALSE instead of .FALSE. for the bInheritHandle actual argument and declare
INTEGER(HANDLE) hMapFile
INTEGER(LPVOID) pBuf

Zitat:

Repeat Offender schrieb:

szName = "Global\MyFileMappingObject"//achar(0)
Also you might consider
USE IFWINTY, ONLY: FALSE, LPVOID, HANDLE
and then pass FALSE instead of .FALSE. for the bInheritHandle actual argument and declare
INTEGER(HANDLE) hMapFile
INTEGER(LPVOID) pBuf

Hey, thanks for the quick response. I tried the above but to no avail, however the achar addition fixed the error returned by the OpenFileMapping function.
Does achar have something to do with a NULL terminated string ?
Also the "HANDLE" and "LPVOID" indicate that I can put any of the the other windows variable types in too, eg "LPSTR", LPTSTR, "WPARAM", etc?
Like "INTEGER(LPSTR) pBuf"??

Zitat:

Gabriel I. schrieb:

Hey, thanks for the quick response. I tried the above but to no avail, however the achar addition fixed the error returned by the OpenFileMapping function.
Does achar have something to do with a NULL terminated string ?
Also the "HANDLE" and "LPVOID" indicate that I can put any of the the other windows variable types in too, eg "LPSTR", LPTSTR, "WPARAM", etc?
Like "INTEGER(LPSTR) pBuf"??


You didn't say what is going wrong at this point. When you printed out the value of pBuf, did you get something that was nonzero? If so, that's the pointer to the data that MapViewOfFile is trying to return to you. You can use TRANSFER to convert it into TYPE(C_PTR) and from there my post at http://software.intel.com/en-us/forums/topic/347322#comment-1718792 may help you to convert this into a deferred-length character variable which Fortran can print for you.
Concatenating a Fortran string with ACHAR(0) in fact appends an ASCII NUL character to it so that you have a (Fortran) NUL-terminated string which is what these C functions want.
For Windows variable types, use google or bing to search for "Windows data types MSDN', also look in ifwinty.f90 in your %INCLUDE% path for comparison. The important thing about HANDLE and LPVOID is that they take the value 4 for 32-bit programs and 8 for 64-bit programs so that when you use these integer KIND numbers instead of blindly using INTEGER*4 for everything your code has a fighting chance to work in both 32-bit and 64-bit modes.
For Windows functions, search the web for "MapViewOfFile MSDN" for example, once you find there that it's in Kernel32.lib, you can look up MapViewOfFile in kernel32.f90 in your %INCLUDE% path. There you can see the data types to use in your ifort invocation of the function.

BTW, I have composed a working example in gofrtran. Should work in 64-bit ifort. Needs a little modification to work in 32-bit ifort.

Anlagen: 

AnhangGröße
Herunterladen proc1.f908.51 KB
Herunterladen proc2.f908.31 KB

Kommentar hinterlassen

Bitte anmelden, um einen Kommentar hinzuzufügen. Sie sind noch nicht Mitglied? Jetzt teilnehmen