- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
See this announcement for attachment instructions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page