Reputation: 133
I have a Fortran application that is required to call two C routines. One to load a file and one to run a calculation on the file about 200 times. I understand that a C DLL cannot 'save' the loaded struct in a static variable to be reused in the calculate function so I am looking to parse back a void* to Fortran and send it to C calculate function.
The C functions:
__declspec(dllexport) void loadfile(void * file); // Empty pointer should be filled with struct of loaded file
__declspec(dllexport) void calculate(void * file, double * result); //filled void ptr is used (casted back to my struct first)
My Fortran code:
module globalFileHolder USE, INTRINSIC::ISO_C_BINDING type(C_PTR), save :: fileModule = C_NULL_PTR end module
Load file routine:
SUBROUTINE loadcfile()
USE, INTRINSIC::ISO_C_BINDING
use globalFileHolder
IMPLICIT NONE
INTERFACE
SUBROUTINE loadfile(fm) BIND(C)
USE, INTRINSIC::ISO_C_BINDING
TYPE(C_PTR) :: fm
END SUBROUTINE loadfile
END INTERFACE
TYPE(C_PTR) :: fms = c_null_ptr
call loadfile(fms)
fileModule = fms
return
end
And finally my routine that is supposed to use the loaded file in a calculation:
SUBROUTINE calculatec() USE, INTRINSIC::ISO_C_BINDING use globalFileHolder IMPLICIT NONE INTERFACE SUBROUTINE calculate(fm,res) BIND(C) USE, INTRINSIC::ISO_C_BINDING TYPE(C_PTR) , VALUE :: fm REAL(C_DOUBLE) , value :: res END SUBROUTINE calculate END INTERFACE TYPE(C_PTR) :: fms REAL(C_DOUBLE) result fms = C_LOC(fileModule) call calculate(fms,result) return end
Now the problem I currently have is that the module variable filemodule seems to be filled but when sending it to the calculate function the variable is null after casting it like:
myStruct * ms = (myStruct*)file;
Where do I go wrong?
Upvotes: 2
Views: 568
Reputation: 21431
The declaration of the fm
dummy argument in the interface for loadfile is missing the VALUE attribute.
Upvotes: 1