|
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
acw
Joined: 04 Nov 2005 Posts: 165 Location: Darkest Devon
|
Posted: Tue Sep 19, 2006 10:41 am Post subject: Passing strings to non-Fortran DLLs |
|
|
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 |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7928 Location: Salford, UK
|
Posted: Tue Sep 19, 2006 1:54 pm Post subject: Passing strings to non-Fortran DLLs |
|
|
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. |
|
Back to top |
|
|
acw
Joined: 04 Nov 2005 Posts: 165 Location: Darkest Devon
|
Posted: Wed Sep 20, 2006 2:14 am Post subject: Passing strings to non-Fortran DLLs |
|
|
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 |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|