- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This may turn out to be a bit of a C++ question but...
A while ago I established a method (with Jugoslav's help) of creating a SafeArray in C++, passing it to Fortran where it was populated with strings, and then returning to the C++ layer to extract/reference the strings.
I was wondering if there are any other more flexible string-array constructs available in C++ with which I could do the same trick? SafeArrayGetElement is a bit clumsy - "strings" would be more useful. Something like an STL ? (I don't know enough about STL to know if it is 'compatible' with CVF (6.6))
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The problem is that STL strings were not meant to be interoperable with other languages, while SafeArrays are. You could overcome the problem by writing a typedef/struct/class in C++, containing the stl string, and exposing its interfaces to the Fortran program, so that its contents are in effect opaque (but you still have to use those routines to manipulate the string in Fortran).
A rough sketch:
A rough sketch:
//C++=========
typedef std::array<:STRING> CStrArray;
extern "C" void StrAdd(CStrArray& ar, char* str)
{ ar.push_back(string(str));}
extern "C" void StrSet(CStrArray& ar, char* str, int index)
{ ar[index] = string(str);}
extern "C" void StrGet(CStrArray& ar, int index, char* str,
int strlen)
{ strncpy(ar[index].c_str(), str, strlen);}
...
CStrArray ar;
F90_Populate(&ar);
//Fortran====
subroutine F90_Populate(ar)
integer, parameter:: PSTRARR = INT_PTR_KIND()
integer(PSTRARR):: ar
interface
subroutine StrAdd(ar, str)
!DEC$ATTRIBUTES C, REFERENCE, ...
integer(PSTRARR):: ar
!DEC$ATTRIBUTES REFERENCE:: str
character(*):: str
end subroutine
end interface
call StrAdd(ar, "Foo"C)
call StrAdd(ar, "Bar"C)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Jugoslav...seems like safearrays might be easier after all!

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