Wednesday, July 23, 2008

Callback to C# from Unmanaged Fortran

Using unmanaged code eg Fortran to callback to C# can be a nightmare to get right. But once you know it, it is just following a recipe. Hence without further explanation, an example and recipe is given below. It is quite self-expalnatory I hope.

Just note that, the example is more than a simple call back. The C# actually calls a Fortran function in a dll. Within the Fortran function calls the callback in C#.

5 comments:

This seems to work perfectly for callbacks that take simple numbers as arguments (the progress, in your example). However, how would you modify your code to send both the array and progress value back to C#?

HiI have been trying to pass an integer array through the callback and I also got the first element only. Could not figure out how to get the full array. After some googling, I believe the solution to this lies in using IntPtr. I tried a bit using IntPtr but still can get it to work (given that I have very little knowledge in using IntPtr). I have attached some links below which perhaps could give you some ideas of what to try. If you are successful, please post your solution in this blog as I'm sure many others would like to know too. Please feel free to investigate more with IntPtr.