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

C Fortran communication

patuco
Beginner
406 Views
Hi everyone,
i have a problem with the c fortran communication: I am trying to send an string to fortran form a C library, but the fortran code is becoming some unreadle characters, I tryed a couple ideas and suggestions but i do not know what i am missing, Anyone could give me a hand?
Thanks in advance.
What i have implemented is:

---------------------------- C call ----------------------------

extern "C" void __stdcall Setmolbyname(char *, int);

---------------------------- C call ----------------------------

---------------------------- Cfunc ----------------------------

JNIEXPORT void JNICALL Java_Main_set_1molecule_1name(JNIEnv *env, jobject jobj, jstring prompt)

{

int size = 20;//TEST ONLY --> DEFINE A CONSTANT WITH THE VALUE OF THE MINIMUM LENGTH OF THE STRING

char * buf ;

printf("Calling the c function ");

buf = (char *)calloc(size,sizeof (char));

buf = (char *)env->GetStringUTFChars(prompt,NULL);

printf("String c1: %s ", buf);

printf(" Calling fortran with the chain %s ", buf);

fflush(stdout);

Setmolbyname(buf, size);

//printf("Release the jstring: %s ", buf);

env->ReleaseStringUTFChars(prompt, buf);

//printf("Ending C ");

}

---------------------------- endCfunc ----------------------------

---------------------------- fortran func ----------------------------

SUBROUTINE SETMOLBYNAME(NEWMOLEC, i_Len)
!MS$ATTRIBUTES DLLEXPORT,STDCALL,ALIAS:'_Setmolbyname@8'::SETMOLBYNAME
INCLUDE 'COM_MASE.LVG'
CHARACTER*20 NEWMOLEC
INTEGER i_Len
WRITE(*,*) "Molec in fortran "
WRITE(*,*) NEWMOLEC
WRITE(*,*) "Molec in fortran "
DCOM ='MOLE'
RDCOM=NEWMOLEC
END SUBROUTINE
---------------------------- end fortran func ----------------------------

---------------------------- output ----------------------------

String c1: SIO

Calling fortran with the chain SIO

Molec in fortran

Molec in fortran

---------------------------- end output ----------------------------

0 Kudos
2 Replies
TimP
Honored Contributor III
406 Views
As the Fortran CHARACTER argument implies a hidden length argument which is seen only on the C side, it looks like you have an extra argument on the Fortran side. AFAIK stdcall is not recommended, as it is not supported on 64-bit Windows, for example. Also, Fortran CHARACTER argument is a compiler-dependent extension, not strictly permitted by stdcall.
You would normally use strlen(), for example, to pass the actual length of the string, and use that length on the Fortran side. It looks here like you are trying to display the trailing null and subsequent uninitialized characters on the Fortran side.
I/O to the same stream by both C and Fortran, without precautions, is problematic. Maybe you could do it with a flush on the Fortran side as well.
Check the examples which come with your choice of Fortran and adhere more closely to them.
0 Kudos
patuco
Beginner
406 Views
Many thanks for the reply Tim,
Ive already tryed with this other config (using the implicit arguments) but getting the same answer, an unreadable chain.
It seems like fortran is not becoming the chars from c !!!!???:
SUBROUTINE SETMOLBYNAME(NEWMOLEC)
!MS$ATTRIBUTES DLLEXPORT,STDCALL,ALIAS:'_Setmolbyname@8'::SETMOLBYNAME
about the length it can be done easily but for the moment i am trying to make this part work, and i do not know how to solve this stage. Thanks

Message Edited by patuco on 03-29-200601:54 AM

0 Kudos
Reply