Silverfrost Forums

Welcome to our forums

Passing strings to non-Fortran DLLs

19 Sep 2006 9:41 #1066

I have a standard windows DLL (written in C/C++) with functions called from Fortran. Everything works nicely however I've noticed that when passing strings as parameters (declared with 'STRING' in the Fortran declaration eg. STDCALL StrSplit 'StrSplit' (STRING, STRING, VAL) : INTEGER*4), if the string is blank (ie. all spaces), the DLL sees it as a C string of length 1 containing a single space, however if the string is not blank, it passes through without any trailing spaces (as expected).

Is this behaviour expected and if so is this the only situation where Fortran trailing spaces are passed through to a DLL ?

Thanks, Alan

19 Sep 2006 12:54 #1068

Alan

I am not sure that there is an expected behaviour here. This area is outside of the Fortran Standard and the designer has to reconcile fixed length Fortran character variables with null terminated C strings.

If it is relevant you could see if using INSTRING and/or OUTSTRING makes any difference.

20 Sep 2006 1:14 #1072

Paul,

INSTRING has the same issue as STRING (outputting strings from C to Fortran with STRING or OUTSTRING works fine - a null-terminated C-string gets converted as expected). It still seems to me that there is either a small bugette here, or more likely, there is a specific reason why 'empty' strings are passed as a single space rather than an empty C string. The ftn95 help is pretty detailed in its description of STRING/INSTRING saying that the compiler will generate the appropriate null-terminated string for passing to the DLL - since a single string of spaces in Fortran is considered an 'empty' string this implies that an empty C-string should be passed (if your fortran string contains non-space text, any trailing spaces disappear in the C - so a Fortran 'fred ' becomes a C 'fred' (so I would expect), but a fortran ' ' becomes a C ' '). Presumably the issue is the same when using the Win32 API - any attempt to pass an empty string in directly would result in a single ' '.

It's not difficult to get around, but it would be useful to have some clarification because if there are other situations where we may get trailing spaces then its probably not worth using STRING/INSTRING - I might as well just pass the Fortran string in as a ref and have a C 'len_trim' function and avoid any extra buffering or conversion. If the only time it happens is with a blank string, then checking for the specific case of a single ' ' isn't a problem.

Any further clarification or info would be greatly appreciated, Alan

Please login to reply.