Fortran Dll in C# passing strings problem

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi I have to link C# with a fortran Dll. I have a fortran definition of a subrutine:

<div style="color:Black;background-color:White; <pre>
subroutine SETUP (nc,hfiles,hfmix,hrf,ierr,herr)

c
c define models <span style="color:Blue; and initialize arrays
c
c A call <span style="color:Blue; to this routine is required.
c
c inputs:
c nc--number of components (1 <span style="color:Blue; for pure fluid) [integer]
c hfiles--array of file names specifying fluid/mixture components
c [character*255 variable] <span style="color:Blue; for each of the nc components;
c e.g., :fluids:r134a.fld (Mac) <span style="color:Blue; or fluidsr134a.fld (DOS) <span style="color:Blue; or
c [full_path]/fluids/r134a.fld (UNIX)
c hfmix--mixture coefficients [character*255]
c file name containing coefficients <span style="color:Blue; for mixture model,
c <span style="color:Blue; if applicable
c e.g., fluidshmx.bnc
c hrf--reference state <span style="color:Blue; for thermodynamic calculations [character*3]
c <span style="color:#A31515; DEF: <span style="color:Blue; default reference state <span style="color:Blue; as specified <span style="color:Blue; in fluid file
c is applied <span style="color:Blue; to each pure component
c <span style="color:#A31515; NBP: h,s = 0 at pure component normal boiling point(s)
c <span style="color:#A31515; ASH: h,s = 0 <span style="color:Blue; for sat liquid at -40 C (ASHRAE convention)
c <span style="color:#A31515; IIR: h = 200, s = 1.0 <span style="color:Blue; for sat liq at 0 C (IIR convention)
c other choices are possible, but these require a separate
c call <span style="color:Blue; to SETREF
c outputs:
c ierr--error flag: 0 = successful
c 101 = error <span style="color:Blue; in opening file
c 102 = error <span style="color:Blue; in file <span style="color:Blue; or premature <span style="color:Blue; end of file
c -103 = unknown model encountered <span style="color:Blue; in file
c 104 = error <span style="color:Blue; in setup of model
c 105 = specified model <span style="color:Blue; not found
c 111 = error <span style="color:Blue; in opening mixture file
c 112 = mixture file of wrong <span style="color:Blue; type
c 114 = nc<>nc from setmod
c herr--error string (character*255 variable <span style="color:Blue; if ierr<>0)
c [fluid parameters, etc. returned via various common blocks]

character*3 hrf
character*255 hfiles(ncmax),hfmix
[/code]

<br/>
and I have made in C# the following prototype:


<div style="color:Black;background-color:White; <pre>
<span style=<span style="color:#A31515; "white-space:pre"> [DllImport(<span style="color:#A31515; @"C:Program FilesREFPROPrefprop.dll",
<span style="color:Green; //CallingConvention=CallingConvention.StdCall,
<span style="color:Green; //CharSet = CharSet.Ansi,
EntryPoint = <span style="color:#A31515; "SETUPdll")
]
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; extern <span style="color:Blue; void Setup(<span style="color:Blue; ref <span style="color:Blue; long nc,
<span style="color:Blue; string hfiles,<span style="color:Blue; long hfiles_len,
<span style="color:Blue; string hfmix,<span style="color:Blue; long hfmx_len,
<span style="color:Blue; string hrf,<span style="color:Blue; long hrf_len,
<span style="color:Blue; ref <span style="color:Blue; long ierr, <span style="color:Blue; ref <span style="color:Blue; string herr,<span style="color:Blue; ref <span style="color:Blue; int herr_len);
[/code]

<br/>
The problem is that when I access the function I receive the following error:


Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

What it is wrong with my code? Can anyone help me?

View the full article
 
Back
Top