- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Steve Lionel has modified the C_CALL_Fortran mixed language samplefrom CVF into an IVF sample.
The sample uses a FDLL project which builds the Fortran source of subroutine FSUBas a DLL. FSUB has an integer and a character string input argument and an character string output argument. FSUB concatenates the input arguments tooneoutput string.
A c/c++ main programUSEDLL calls the dll version of FSUB.
As an extension to this sample I am interested in ac/c++DLL project (say CDLL) in whichthe cdllsource codecontains (an entry of)a character function that calls the FSUB dll and returns thec string constructed by FSUB.
Is that possible?
Giel
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Most anything is possible, but I don't fully understand your specification. How would you declare this C function in C? (Pretend Fortran isn't part of this.) What does it mean to be a "character function" in C?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I admit that the use of "function" and "subroutine" is indeed confusing.
Iwould suggest anything like:
LPTSTR CFUN
LPSTR CFUN
char *CFUN
And for the definition of the function CFUN:
LPTSTR CFUN(int *int_arg,LPTSTR *str_in)
LPSTR CFUN(int *int_arg,LPSTR *str_in)
char *CFUN(int *int_arg, char *str_in)
The body of the latter CFUN wouldbe something like
{
char *str_out;
int STR_OUT_LEN;
STR_OUT_LEN= 512;
FSUB(&int_arg,str_in, str_out, strlen(str_in),STR_OUT_LEN);
return str_out;
}
Giel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Uh-oh. You see, the problem with Fortran strings is that they-re two-fold: the passed information must always contain both the address and the length. Now, since both cannot be returned through registers, Fortran compiler "internally" turns the character-valued function into:
FUNCTION StrRet()Well, maybe, I didn't care to do complete reverse-engineering. There are similar issues with functions returning TYPEs, arrays etc. So, it's better to avoid functions returning anything other than plain vanilla ints or floats in mixed-language programming.
CHARACTER(10):: StrRet
StrRet="1234567890"
END FUNCTION StrRet
extern "C" char* strret(char*, int)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
OK. Exactly, therefore I would like to usethe CFUN.
Giel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In C, your CFUN returns a pointer to a string, one that is presumably allocated within the function. You can do the same in Fortran. The equivalent Fortran function would be something like this:
It's then the caller's responsibility to deallocate the result.
function CFUN()
integer(int_ptr_kind()) :: CFUN
character(10) :: retstr = 'abcdefghij'
integer(1), dimension(*) :: temp
pointer (p, temp)
p = malloc (len(retstr)+1)
do i = 1,len(retstr)
temp(i) = ichar(retstr(i:i)
end do
temp(ubound(temp)) = 0
CFUN = P
return
end
It's then the caller's responsibility to deallocate the result.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page