- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello all
I have been struggling to pass an array of structs from C# to a Fortran DLL, alter its contents in Fortran and pass it back again. I managed to get it more or less working, but I'm apparently using an onconventional way of calling. And though it seems to work fine, I do get an 'imbalanced stack' error in C# debug mode. In Fortran debug mode it seems to work fine however.
Here is my Fortran header:
SUBROUTINE PassArrayOfStructs(pArray, Length)
cDEC$ ATTRIBUTES DLLEXPORT :: PassArrayOfStructs
cDEC$ ATTRIBUTES DECORATE, ALIAS: "PassArrayOfStructs"
& :: PassArrayOfStructs
USE ISO_C_BINDING, ONLY: C_INT, C_PTR
TYPE(C_PTR), INTENT(IN):: pArray [VALUE] !the external pointer to the external array - be sure to pass it by value!
INTEGER(C_INT), INTENT(IN):: Length [VALUE] !the length of the external array
And my C# header:
[DllImport(Constants.DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void PassArrayOfStructs(IntPtr pArray, int length);
and its call:
I do have several questions:
1. Why do I have to add the [VALUE] attribute when passing the pointer to get it working, though this is against the standard calling conventions?
2. If I try to pass the 'Length' (or 'pArray' for that matter) argument as 'INTEGER(C_INT), INTENT(IN), VALUE:: Length' I get a 'trying to write protected memory' error and the program crashes, though this is the standard syntax I should be using (as I have been told).
3. What could cause the 'imbalanced stack' warning? Could it be that this is something that is caused by the Debug environment?
Further information:
- I compile in 32-bit mode.
- I'm using the Intel Parallel Studio XE 2029 compiler, integrated in Visual Studio 2022.
Your help is much appreciated!
Regards, Jan
Link Copied

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