Software Archive
Read-only legacy content
17061 Discussions

passing strings to Visual C++ .dll

Intel_C_Intel
Employee
755 Views
I'm trying to pass a string to a routine in a C++ .dll, have the .dll change the string and then pass it back. I can pass integers by reference fine, but I can't get the strings to work. I have looked at the examples in the Compaq Visual Fortran Programmers Guide. Here is my Fortran code:

interface
subroutine c_routine(int_arg,
& strErrorMessage,
& strInputFile,
& dEndMomentArray) !

!dec$ attributes c::c_routine
!dec$ attributes dllimport::c_routine

integer*4 int_arg
real*8 dEndMomentArray(13,2)
character*(*) strInputFile
character*(*) strErrorMessage

!dec$ attributes reference::int_arg
!dec$ attributes reference::strInputFile
!dec$ attributes reference::strErrorMessage
!dec$ attributes reference::dEndMomentArray

end subroutine c_routine
end interface

character*256 strInputFile
character(256) strErrorMessage
data strErrorMessage /'�'C/
integer*4 int_arg

real*8 dEndMomentArray(13,2)

do i = 1,13
do j = 1,2
dEndMomentArray(i,j) = 0.0
end do
end do

int_arg = 0

!call c_routine.
strInputFile = XMLInputFile
call c_routine(int_arg,
& strErrorMessage,
& strInputFile,
& dEndMomentArray)

This is the simple C++ .dll that it calls:

void c_routine(int& int_arg,
char* cErrorMessage,
char* cInputFile,
double dResults[13][2])
{
string strInputFile(cInputFile);
cout << cInputFile;

cErrorMessage = "No Error!";

for(long i = 0;i<13;i++)
{
for(long j = 0;j<2;j++)
{
dResults = i+j;
}
}

int_arg = 50;
}

The C++ routine can see and change the contents of the strings passed in OK, but it doesn't pass the changed values back to Fortran. Do you have any ideas what I am missing? Thanks.
0 Kudos
3 Replies
Intel_C_Intel
Employee
755 Views
I figured out my error. I was filling the string incorrectly in the C++ code. I needed to fill the character string in a loop or using the strcpy function.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
755 Views
Apart from that -- I'm quite tired so I may be wrong -- C declaration
dResults[13][2] looks suspicious to me. C and fortran use different conventions for storing multi-dimensional arrays in memory (or for matrix notation, depending on the viewpoint -- row-major
vs. column-major order). I think that it should be [2][13] and
that it should be instead of . No access violation occurs in your code because everything happens in 26-word space and the i+j operation
is commutative, but for a different expression you'll get wrong results.

Jugoslav
0 Kudos
Intel_C_Intel
Employee
755 Views
You are right about the way Fortran and C++ index multi-dimensioned arrays. I didn't do a very thorough check of the results being passed back in the array, so I missed that problem. Thanks, that would have given me a big headache down the road.

Mark
0 Kudos
Reply