My Windows application is written primarily in Fortran but calls functions from in C library (which I have access to code). I need to be able to call a Fortran subroutine from within the C library to display error and warning messages the GUI.
The simplest solution seems to be to create a C function, within the C library, that calls a Fortran subroutine which accepts the message as a character array. I found the following text on a web site called XGear but I've not had any joy using this with the Intel compiler.
Fortran functions that receive string variables from C
This is slightly more complicated. The Fortran function must be declared at the beginning of the C calling function (e.g. main) like this:
extern void load_(char *string, size_t *len_string);
Note that we have to pass all variables to Fortran as pointers. Our string variable is already a pointer, but we have to pass the string's length as a pointer too. Although the function name is not case sensitive in Fortran, it gains an underscore in the C declaration and when it is called:
len_string = strlen(string); load_(string, &len_string);
Note that in passing the string from C to Fortran we also need to pass the length of the string, hence the use of strlen beforehand (not forgetting to declare the string length variable as a size_t rather than an int).
The Fortran function has to be modified to receive a string variable as a byte array (instead of a character string), as well as the string's length:
SUBROUTINE LOAD(string,len_string) BYTE string(51) INTEGER len_string
Note that the original Fortran function would not have needed an integer variable to store the length of the received string. We also have to add the following to our Fortran function so that it copies the byte array to a string:
CHARACTER*50 charstr WRITE (charstr,'(50a)') (string(i),i=1,len_string)
Has anyone any tips on how can this be done using the ISO_C_BINDING which would be the cleanest route but a have not found a good example for a subroutine passing a string from Fortran to C. I have tried several things without success? The main problem is an undefine external symbol. The code would need to apply toboth 32-bit and 64-bit configurations.
Thanks in advance.