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

Callback functions with c#

Harry1
Beginner
622 Views

Hi,

My collegue posted a topic a few days ago regarding returning strings from c#, however the discussion didn't really go in the direction we thought so I though i'd post a more specific question.

First for some background - We have had developed for quite some time a large production application that uses a Fortran backend (DLL) and a .NET 3.5 WPF front end. We currently pass a very large amount of binary input and output data back and forth across the DLL boundary via structures which contain all sorts of sub-structures, string buffers, multi-dimensional arrays, etc. We even managed so write a "custom marshaler" in c# to automatically marshal all this binary data - including dynamically allocated fortran arrays. All of this works flawlessly.

As the project developed further we required a mechanism to strings stored in a XAML "Resource Dictionary" on the WPF c# side into the Fortran DLL. We didn't want to pass across all possible strings (there are hundreds) so instead we thought we could utilise a send a c# delegate as a function pointer to the Fortran DLL and use it as a callback to dynamically request the string as required. A further complication to this is that we also support non-english character sets so we also decided to convert the string into a UTF-8 encoded byte stream and pass it back to Fortran as a character(len=1024) buffer.

Attached is a reduced down VS2008 solution that illustrates what we are trying to do.

If you look at the code in "test.F90" you will see calls to GetStringResourceCallbackExternal, this routine passes the string resource key it is requested, a 1024 character buffer where the result is stored and two integers that return the length of the string in both ANSI and UTF-8 encoding.

What we have noticed is that the first call to GetStringResourceCallbackExternal always works perfectly, within the debugger everything looks as it should. However, on subsequent calls things start to go astray and we really are out of ideas as to why.

After the second call to GetStringResourceCallbackExternal when we try to print the value of sStringResource execution hangs and I find I have to stop the process manually.

If we remove the "print" statements, all three calls to GetStringResourceCallbackExternal appear to work properly in the debugger but a Attempted to read or write protected memory exception is raised when "testfunction" returns.

Does anyone here have any idea what is going on? Have we just missed something simple or are we doing something fundamentally wrong?

Any help would be greatly appreciated.

Yes, the attachment process seemed to catch me out here.

Darren Thompson

0 Kudos
3 Replies
anthonyrichards
New Contributor III
622 Views
..and I guess you are having trouble posting attachments, just like the rest of us?
0 Kudos
Steven_L_Intel1
Employee
622 Views
Quoting - anthonyrichards
..and I guess you are having trouble posting attachments, just like the rest of us?

See this announcement for attachment instructions.

0 Kudos
onkelhotte
New Contributor II
622 Views
Ive had to create a new project, because I dont have IVF11 yet... Still using 10.1.025.
But I get the same exception. But when I use a write command to put sStringResource into a text file, the output of string1 and string2 is correct (although the euroletter or the yenletter is not correct, but thats a ASCII/UTF8 thing).

After the write / print statement, externalgetstringresourcecallback is an undefined pointer/array and RCODE is 0.

Maybe you can only call a loced external procedure once and not more often.
Markus
PS: It would be nice to know how you pass arrays between Fortran and C#. Because C# uses an array[x,y] and Fortran an array[y,x] allocation, I copy every single element, so I have the same structure in both worlds. Is there a simpler way? Feel free to contact me ;-)
0 Kudos
Reply