Passing C++ character array to Fortran -


i have been trying pass character array c++ fortran subroutine seems fortran not receive characters properly. have been searching web find solution proposed ideas seem complicated. interesting (and concise) solution proposed steve @ intel fortran forum cannot work in code. so, appreciate suggestion me out resolve issue.

the following function c++ routine makes call fortran subroutines:

extern "c" void __stdcall f_validate_xml(const char* buffer, int len); void cudmeditordoc::onvalidatexml() {     cstring filename = "test.udm";     const char* buffer = filename.getbuffer();     int len = filename.getlength();     f_validate_xml(buffer, len); } 

and here fortran subroutine supposed receive character array:

subroutine validate_xml(file_name, len) !dec$ attributes decorate, stdcall, alias:"f_validate_xml" :: validate_xml  use xml_reader_structure use, intrinsic :: iso_c_binding  integer(c_int), value, intent(in) :: len character(len, kind=c_char), intent(in) :: file_name  integer  :: i = 1 end subroutine validate_xml 

in debug mode , when program has stopped @ line (i = 1), hover mouse on file_name see contents watch window says cannot find file_name symbol (although len correctly passed). also, if watch file_name(1:8) in watch window, still don't original character arrays. believe there wrong way pass parameters fortran chances watch window not correct.

i appreciate shed lights here. thanks

you fell trap caused saying (even on stack overflow in heavily upvoted answers) should use iso c binding construct interoperable subroutines.

that wrong, using iso_c_binding module not make procedure interoperable , not change calling conventions.

the other, separate , equally important, feature in fortran 2003 c interoperability bind(c) attribute.

notice, steve lionel has in linked example. should have

   subroutine validate_xml(file_name, len) bind(c, name="f_validate_xml") 

the bind(c) changes calling conventions in several subtle ways , largest difference character strings, pass length in hidden argument, not in interoperable procedures.


Comments

Popular posts from this blog

What Are The Best Universities In The World?

Hard vs. Soft Water: What's The Difference?

The Ten Most Livable Cities In The World