forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

WRITE called in a DLL results in an exception
Goto page Previous  1, 2
 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
mecej4



Joined: 31 Oct 2006
Posts: 1885

PostPosted: Sat Jul 18, 2020 12:53 pm    Post subject: Re: Reply with quote

BK wrote:
I don't pass the file names because past experience shows that the file name strings get garbled.


You can certainly pass file names (or strings in general) from C to Fortran, but you have to use the appropriate calling conventions.

File cmain.c:

Code:
#include <stdio.h>
#include <string.h>
extern "C"{
   FTN_SUB(char *str1, char *str2, int l1, int l2);
   }

int main(int argc, char *argv[]){
char *s1 = "file_abc", *s2 = "file_pqr";
FTN_SUB(s1,s2,strlen(s1),strlen(s2));
}


File ftnsub.f90:

Code:
subroutine ftn_sub(s1,s2)
implicit none
character*(*) s1,s2
print '(2x,A,2x,A)',s1,s2
return
end subroutine


Compile, link and run:

Code:
scc cmain.c
ftn95 ftnsub.f90
slink cmain.obj ftnsub.obj
cmain


Output:

Code:
  file_abc  file_pqr


Alternatively, put the Fortran code into a DLL, build the DLL, link the C caller with the DLL, and run:

Code:
ftn95 ftnsub.f90
slink /dll ftnsub.obj /exportall
slink cmain.obj ftnsub.dll
cmain


Do note that STDCALL has not been used. If you think that you have a need for it in your code, please check and state why.
Back to top
View user's profile Send private message
BK



Joined: 07 May 2020
Posts: 14

PostPosted: Mon Jul 20, 2020 2:44 pm    Post subject: Reply with quote

PaulLaidler, I took your simple fortran routine and added blocks, one at a time. I used the file on unit 10 to write debug lines. It turns out that Silverfrost is much more strict abt WRITE statements than DIGITAL Visual FORTRAN. Complicating the debugging is that once an exception occurs, nothing is written to unit 10.

BTW, TRIM doesn't appear to eliminate trailing spaces. A CHARACTER*80 line is still LEN=80 after TRIM.
OUTNAM = TRIM(LINE)
ILEN = LEN(OUTNAM)
OUTNAM (file name path) and LINE are both CHARACTER*120.
Back to top
View user's profile Send private message
Kenneth_Smith



Joined: 18 May 2012
Posts: 697
Location: Hamilton, Lanarkshire, Scotland.

PostPosted: Mon Jul 20, 2020 3:14 pm    Post subject: Reply with quote

Code:
PROGRAM test_trim
  CHARACTER(len=20), PARAMETER :: s = "Silverfrost         "
  print*, LEN(s), LEN(TRIM(s))
END PROGRAM


generates
Code:
           20          11
Back to top
View user's profile Send private message Visit poster's website
mecej4



Joined: 31 Oct 2006
Posts: 1885

PostPosted: Mon Jul 20, 2020 3:57 pm    Post subject: Re: Reply with quote

BK wrote:
... TRIM doesn't appear to eliminate trailing spaces. A CHARACTER*80 line is still LEN=80 after TRIM.
OUTNAM = TRIM(LINE)
ILEN = LEN(OUTNAM)
OUTNAM (file name path) and LINE are both CHARACTER*120.


You are not giving a correct interpretation of what happens.

Standard Fortran has no variable length string type. The expression TRIM(LINE) may be a 11-character string, but when it is assigned as the value of OUTNAME, the string value is either padded on the right with spaces or truncated to match the length of OUTNAME.

Whether one likes this behaviour or not, this is what the language rules prescribe.
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Mon Jul 20, 2020 4:51 pm    Post subject: Reply with quote

BK

You have not said whether or not you have made progress. Did you get a modified form of my simple sample to work?

There will be no output after an exception is raised and I don't see how any compiler can do otherwise. Also you don't provide evidence for your statement that FTN95 is stricter.

I guess that everyone blames the tools when things go wrong but up to now you have not demonstrated that FTN95 is at fault.
Back to top
View user's profile Send private message AIM Address
DanRRight



Joined: 10 Mar 2008
Posts: 2813
Location: South Pole, Antarctica

PostPosted: Mon Jul 20, 2020 9:25 pm    Post subject: Reply with quote

Quote:
Sad to say, I think Silverfrost is not ready for the big leagues.

hahaha... funny guy
Back to top
View user's profile Send private message
BK



Joined: 07 May 2020
Posts: 14

PostPosted: Mon Jul 20, 2020 9:48 pm    Post subject: Reply with quote

PaulLaidler, yes, I used your simple fortran program. I took out the F_STDCALL, etc. in all subroutines, added blocks of code one at a time, and got the DLL to work with C++Builder.

It wasn't a straight-fwd process of add and build, add and build. There were numerous exceptions, many of which were my own mistakes. Going back after it worked, what seemed like strict requirements for the WRITES, weren't.

The last several weeks have been extremely frustrating. My apologies for being a royal pain. And thanks for the assistance.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
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