Click here to Skip to main content
14,932,691 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I try to pass an 1D-array from a C#-DLL to Fortran. For that I am using R. Giesecke DllExport. That makes my managed DLL to a unmanaged DLL with a .lib file. To pass a normal parameter works well. Please see here: https://software.intel.com/en-us/forums/topic/517631
The 1D-Array has always the size of 6! The size of the array will never change only the assigned value.

Here my Code in C#:
C#
using System;
using System.Text;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;

namespace Testme
{
    class Test
    {
        [DllExport("Get1DArray", CallingConvention = CallingConvention.Cdecl)]
        public static int Get1DArray([MarshalAs(UnmanagedType.LPArray, SizeConst = 5)] int[] Stress)
            
        {
           
            Stress [0] = 0;
            Stress [1] = 1;
            Stress [2] = 2;
            Stress [3] = 3;
            Stress [4] = 4;
            Stress [5] = 5; 

            return Stress[5];
        }

    }
}

And my Fortran Code:
Python
MODULE MYEXTERNALS
USE iso_c_binding
INTERFACE
FUNCTION Get1DArray(Stress) RESULT(ret) bind(c, name="Get1DArray")
!DEC$ATTRIBUTES DLLIMPORT :: Get1DArray
USE, intrinsic :: iso_c_binding
INTEGER(c_int), intent(in) :: Stress(6)
INTEGER(c_int) :: ret
END FUNCTION
END INTERFACE
END MODULE MYEXTERNALS


PROGRAM CallArray
USE MYEXTERNALS
IMPLICIT NONE
INTEGER(c_int) :: Stress(6)
PRINT*, '1DArray =', Get1DArray(Stress)
PAUSE
END PROGRAM CallArray

After debugging I get following output:

'CallArrayDll.exe': Loaded '\\cifs\cluster\Home\ca416444\WinDocuments\Masterarbeit\C# Dllexport\to Fortran\1DArray2\CallArrayDll\Debug\CallArrayDll.exe', Symbols loaded.

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded '\\cifs\cluster\Home\ca416444\WinDocuments\Masterarbeit\C# Dllexport\to Fortran\1DArray2\CallArrayDll\Debug\ArrayDll.dll', No native symbols in symbol file.

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\mscoree.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\imagehlp.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoreei.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\shlwapi.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\msvcr110_clr0400.dll', Cannot find or open the PDB file

First-chance exception at 0x75c8c42d in CallArrayDll.exe: 0x04242420: 0x4242420.

'CallArrayDll.exe': Loaded 'C:\Windows\assembly\NativeImages_v4.0.30319_32\mscorlib\ce5f61c5754789df97be8dc991c47d07\mscorlib.ni.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded '\ArrayDll.dll', No symbols loaded.

'CallArrayDll.exe': Unloaded '\ArrayDll.dll'

'CallArrayDll.exe': Loaded '\ArrayDll.dll', No symbols loaded.

'CallArrayDll.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\clrjit.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\diasymreader.dll', Cannot find or open the PDB file

First-chance exception at 0x75c8c42d in CallArrayDll.exe: Microsoft C++ exception: EEException at memory location 0x0020f4fc..

First-chance exception at 0x75c8c42d in CallArrayDll.exe: 0xE0434352: 0xe0434352.

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\dbghelp.dll', Cannot find or open the PDB file

'CallArrayDll.exe': Loaded 'C:\Windows\SysWOW64\clbcatq.dll', Cannot find or open the PDB file

The thread 'Win32 Thread' (0x5d4c) has exited with code 3 (0x3).

The thread 'Win32 Thread' (0x3944) has exited with code 3 (0x3).

The thread 'Win32 Thread' (0x59fc) has exited with code 3 (0x3).

The program '[24492] CallArrayDll.exe: Native' has exited with code 3 (0x3).



To Create a C Dll with this Code:
C++
int Get1DArray(int Stress[]){
   Stress[0]=0; Stress[1]=1; Stress[2]=2;
   Stress[3]=3; Stress[4]=4; Stress[5]=5;
   return Stress[5];
   }

and call it with the Fortran Code above works fine!! So i guess the problem is on the C# side.

Please help me to fix the problem.
Posted
Comments
Sergey Alexandrovich Kryukov 11-Jul-14 22:51pm
   
R. Giesecke DllExport? Looks interesting, but what is that? Do you have a reference?
Yes, it's quite possible to export from .NET assembly to unmanaged, without using COM. There are some solutions on CodeProject.
—SA
Member 10854414 13-Jul-14 6:01am
   
Hey Sergey,
here the link: https://www.nuget.org/packages/UnmanagedExports
Sergey Alexandrovich Kryukov 13-Jul-14 22:20pm
   
Thank you. Another question: do you have the source code of this library?
I know how to export to unmanaged from a .NET assembly, never new the work you are using, would be curious to know how well is it implemented.
—SA

1 solution

Shouldn't it be SizeConst = 6?

C#
public static int Get1DArray([MarshalAs(UnmanagedType.LPArray, SizeConst = 6)] int[] Stress)


If that is not the problem, you can do an ugly workaround if you have to move forward in your project.
It will work if you don't have a lot of similar methods that you need to change.

C#
using System;
using System.Text;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;

namespace Testme
{
    class Test
    {
        [DllExport("Get1DArray", CallingConvention = CallingConvention.Cdecl)]
        public static int Get1DArray(int stress1, int stress2, int stress3, int stress4, int stress5, int stress6)

        {

            Stress [0] = stress1;
            Stress [1] = stress2;
            Stress [2] = stress3;
            Stress [3] = stress4;
            Stress [4] = stress5;
            Stress [5] = stress6;

            return Stress[5];
        }

    }
}


It is ugly, but it will most likely work.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900