String returned from C# to Fortran - problem

Mark Besley
Total Points:
30
Registered User
November 26, 2008 3:12 AM PST
Rate
 
#4
Quoting - Mark Besley

I have an Intel Fortran program that calls a C# function. The C# function header is a follows, it takes "stringresourcekey" and returns "stringresource" (1024 byte string) and two integers.

private void GetStringResource(
string stringresourcekey,
[In, Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 1024)] byte[] stringresource,
ref int stringresourcelength_ansi,
ref int stringresourcelength_utf8)

In Fortran, I call it like this:

call GetStringResourceCallbackExternal(stringresourcekey,sStringResource,length_ansi,length_utf8)

Declarations are:

character

 

 

(len=*), intent(in) :: stringresourcekey
character (len=1024) :: sStringResource,out_string
integer :: &
length_ansi, &
! ANSI String length
length_utf8 ! Effective String length when displayed with UTF-8 encoding

This all works fine, the C# function is called, executes properly, and appears to return the intended values.  Running in the debugger, I can see sStringResource,length_ansi,length_utf8 all returned with correct values and the intended string in sStringResource.

However when I try to use sStringResource, I have problems - if I write it to a file, the first part of it is garbage.  If I try a simple assignment:

out_string = sStringResource

then the program crashes with an access violation.  Any ideas what I'm doing wrong (or not doing)?

Mark Besley

 

 

A further development - I changed nothing in the way that the calls were done and changed nothing at the C# end, but instead declared sStringResource as an array of 256 4-byte integers:

use the same call:
call GetStringResourceCallbackExternalstringresourcekey,sStringResource,length_ansi,length_utf8)

integer, dimension(256) :: sStringResource,out_string
character (len=1024) :: mold,out_string1  

out_string = sStringResource

I can assign this to another integer array without getting an access violation, and better still I can transfer the data into a character variable and write it out and get the intended output as follows: 

Any idea why this fails if I declare the sStringResource as character but works fine if I declare it as integer?  Is there some subtle difference in the way the data is handled between the two types?

out_string1 =

 

transfer(out_string,mold)
write(unlis1,"(a)")out_string1

 Mark Besley

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



Intel Software Network Forums Statistics

8473 users have contributed to 31605 threads and 100654 posts to date.
In the past 24 hours, we have 30 new thread(s) 110 new posts(s), and 160 new user(s).

In the past 3 days, the most popular thread for everyone has been gemm(A,A,A) like possible? The most posts were made to gemm(A,A,A) like possible? The post with the most views is Dear Steve, excuse me for a d

Please welcome our newest member Kevin Johnson