Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29426 Discussions

Calling function in C++ Dll from Compaq Visual Fortran

jk
Beginner
828 Views

I am trying to call a function in C++ Dll module and passing two character variables. It works fine with one variable but when the second is added I get an error "Stack trace terminated abonormally". My fortran code is as follows:

INTERFACE

integer function GetRandomChar(char1, char2)

!DEC$ ATTRIBUTES C,DLLIMPORT

!DEC$ ATTRIBUTES ALIAS:'_GetRandomChar'::GetRandomChar

!DEC$ ATTRIBUTES REFERENCE :: char1, char2

character*(*) :: char1, char2

end function GetRandomChar

end interface

tmpchar1 = 'text1' //char(0)

tmpchar2 = 'text2'//char(0)

tmpCharid = GetRandomChar(tmpchar1, tmpchar2)

The C++ Dll code is as follows:

In header file:

extern "C" __declspec(dllexport) int GetRandomChar(char*, char*);

In CPP file:

extern "C" __declspec(dllexport) int GetRandomChar(char *test5, char *test6)

{

ofstream myfile("AAA.txt");

//myfile.open ("C:\\\\work\\\\AAA.txt");

myfile << "Writing this to a file.\\n";

myfile << test5;

myfile << "\\n";

myfile << test6;

myfile.close();

}

When I try to access the test6 I get the error. If I comment out "myfile << test6" it runs without problems. What could be wrong?

0 Kudos
2 Replies
Lorri_M_Intel
Employee
828 Views

I modified your code enough to make it compile and link, and I did not see the same error.

F:\tests>type aaa.txt

Writing this to a file.

text1

text2

As written in this note, the attributes on the interface declaration to GetRandomChar do not compile. Please make sure that the real code is correct.

0 Kudos
jk
Beginner
828 Views
Thank you for your answer. I will have a look at it.
0 Kudos
Reply