none
Expression: [mscorlib recursive resource lookup bug] RRS feed

  • Question

  • Expression:[mscorlib recursive resource lookup buy]

    Description: infinite recursion during resource lookup with <g class="gr_ gr_54 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling" data-gr-id="54" id="54">mscorlib</g>, this may be <g class="gr_ gr_139 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="139" id="139">bug</g> in <g class="gr_ gr_105 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="105" id="105">mscorlib</g>, or potentially in certain extensibility points such as assembly resolve events or cultureInfo names. Resource name: Arg_AccessViolationException

    Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'C:\Users\Desktop\RefpropCSharp\RefpropCSharp\bin\Debug\RefpropCSharp.vshost.exe'.

    Additional information: The runtime has encountered a fatal error. The address of the error was at 0x6587ee78, on thread 0x1a08. The error code is 0x80131623. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user <g class="gr_ gr_302 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="302" id="302">marshaling</g> errors for COM-interop or PInvoke, which may corrupt the stack.

    Friday, March 9, 2018 1:19 PM

All replies

  • Simply posting an error message doesn't really help us. Please provide the code that is causing the issue, the exception details including stack trace and any relevant information about the data being used at the time.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, March 9, 2018 2:44 PM
    Moderator
  • when I calling the following function. the problem happens.

    mf_net_wa__refprop.TRNPRPdll(ref TK, ref D_mol_L, ref x, ref ETA_uPaS, ref TCX_W_mK, ref ierr, HERR, errormessagelength);

    the mf_net_wa__refprop is showed as follow:

            [DllImport(refpropDLL_path, EntryPoint = "TRNPRPdll", SetLastError = true)]
            public static extern void TRNPRPdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double ETA, ref double TCX, ref Int32 ierr, char[] herr, Int32 LengthHERR);

    Thank you very much

    Friday, March 9, 2018 4:23 PM
  • You're making a P/Invoke call to a native DLL. The DLL name is defined in some (constant) value called refpropDLL_path. Can you post the code for this definition along with the actual value. Note that if it has a hard coded path then it is going to have issues running on random machines.

    At this point it looks like your import statement is likely wrong. Because you're calling native code, and as mentioned in the error, it is possible the stack is getting corrupted and therefore you get weird errors. When stack corruption occurs it is generally caused by either using the wrong calling convention (you didn't specify it in your import statement) or incorrect parameters being passed.

    One thing that is standing out to me is the fact that you're passing a char[] to a native call which isn't going to work without using the MarshalAs attribute. I suspect that the next parameter is the length of the array. Thus you'll need to use the MarshalAs to indicate as such. If this is an actual string then you should be using string (or StringBuilder) instead.

    Additionally you aren't specifying the charset for the call and .NET is Unicode based. Most native code tends to be ANSI so you're corrupting the stack as well. You should specify the charset in the import statement.

    Please post the native function you're trying to call so we can confirm that the import statement is correct. 


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, March 9, 2018 4:38 PM
    Moderator
  • The class mf_net_wa__refprop is as follows

        public class mf_net_wa__refprop
        {
            // Some constants...
            //public const Int32 refpropfluidpathlength=10000;
            public const Int32 refpropfluidpathlength = (refpropcharlength + 1) * compsize; //+1 for '|' between file names

            public const Int32 refpropcharlength = 255;
            public const Int32 filepathlength = 255;
            public const Int32 lengthofreference = 3;
            public const Int32 errormessagelength = 255;
            public const Int32 compsize = 20;

            //const string FluidsDirectory = "c:/Program Files/NIST/RefProp7/fluids/";
            //const string MixturesDirectory = "c:/Program Files/NIST/RefProp7/mixtures/";
            //const string refpropDLL_path = "c:/Program Files/NIST/RefProp7/Refprop.dll";
            const string locationS = "C:/Users/Desktop/RefpropCSharp/RefpropCSharp/bin/Debug";
            const string FluidsDirectory = locationS + "/fluids/";
            const string MixturesDirectory = locationS + "/mixtures/";
            const string refpropDLL_path = locationS + "/Refprop.dll";

            #region Initialization Subroutines
            [DllImport(refpropDLL_path, EntryPoint = "SETUPdll", SetLastError = true)]
    #if (setup_use_string)
            public static extern void SETUPdll(ref Int32 NumberOfComponents, string HFILES, string HFMIX, string HRF, ref Int32 ierr, char[] HERR, Int32 l1, Int32 l2, Int32 l3, Int32 l4);
            //public static extern void SETUPdll(ref Int32 NumberOfComponents, string HFILES, string HFMIX, string HRF, [In, Out] ref Int32 ierr, [In, Out] char[] HERR, Int32 l1, Int32 l2, Int32 l3, Int32 l4);
    #else
                public static extern void SETUPdll(ref Int32 NumberOfComponents, char[] HFILES, char[] HFMIX, char[] HRF, [In, Out] ref Int32 ierr,  [In, Out] char[] HERR, Int32 l1, Int32 l2, Int32 l3, Int32 l4);
    #endif

            [DllImport(refpropDLL_path, EntryPoint = "SETKTVdll", SetLastError = true)]
            public static extern void SETKTVdll(Int32 icomp, Int32 jcomp, char hmodij, double fij, char hfmix, ref Int32 ierr, ref char[] herr, ref Int32 Lengthhmodij, ref Int32 Lengthhfmix, Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "GETKTVdll", SetLastError = true)]
            public static extern void GETKTVdll(Int32 icomp, Int32 jcomp, ref char hmodij, ref double fij, ref char hfmix, ref char hfij, ref char hbinp, ref char hmxrul, ref Int32 ln1,
                ref Int32 ln2, ref Int32 ln3, ref Int32 ln4, ref Int32 ln5);

            [DllImport(refpropDLL_path, EntryPoint = "GETFIJdll", SetLastError = true)]
            public static extern void GETFIJdll(char hmodij, ref char fij, ref char hfij, ref char hmxrul, ref Int32 ln1, ref Int32 ln2, ref Int32 ln3);

            #endregion

            #region Saturation-State Subroutines

            //iterate for saturated pressure, liquid density and vapor density by given temperature and the composition of one phase
            [DllImport(refpropDLL_path, EntryPoint = "SATTdll", SetLastError = true)]
            public static extern void SATTdll(ref double TK, double[] X, ref Int32 KPH, ref double PkPa, ref double RHOF, ref double RHOG, double[] XLIQ,
               double[] XVAP, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            //iterate for saturated temperature, liquid density and vapor density by given pressure and the composition of one phase
            [DllImport(refpropDLL_path, EntryPoint = "SATPdll", SetLastError = true)]
            public static extern void SATPdll(ref double PkPa, double[] X, ref Int32 KPH, ref double TK, ref double RHOF, ref double RHOG, double[] XLIQ,
                double[] XVAP, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            //iterate for temperature and pressure given a density along the saturation boundary and the composition
            [DllImport(refpropDLL_path, EntryPoint = "SATDdll", SetLastError = true)]
            public static extern void SATDdll(ref double RHOMOLL, ref double[] X, ref Int32 KPH, ref Int32 kr, ref double TK, ref double PkPa, ref double RHOF, ref double RHOG, ref double XLIQ,
                ref double XVAP, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            //iterate for temperature, pressure, and density given enthalpy along the saturation boundary and the composition
            [DllImport(refpropDLL_path, EntryPoint = "SATHdll", SetLastError = true)]
            public static extern void SATHdll(ref double h, double[] X, ref Int32 KPH, ref Int32 nroot, ref Int32 k1, ref double t1, ref double p1, ref double d1, ref Int32 k2, ref double t2,
                ref double p2, ref double d2, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            //iterate for temperature, pressure, and density given an entropy along the saturation boundary and the composition
            [DllImport(refpropDLL_path, EntryPoint = "SATSdll", SetLastError = true)]
            public static extern void SATSdll(ref double s,double [] X, ref Int32 KPH, ref Int32 nroot, ref Int32 k1, ref double t1, ref double p1, ref double d1, ref Int32 k2, ref double t2,
                ref double p2, ref double d2, ref Int32 k3, ref double t3, ref double p3, ref double d3, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            //compute the heat capacity along the saturation line as a function of temperature for a given component
            [DllImport(refpropDLL_path, EntryPoint = "CSATKdll", SetLastError = true)]
            public static extern void CSATKdll(ref Int32 icomp, ref double TK, ref Int32 KPH, ref double PkPa, ref double RHO, ref double CSAT, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            //compute the heat capacity and dP/dT along the saturation line as a function of temperature for a given component. See also subroutine CSATK.
            [DllImport(refpropDLL_path, EntryPoint = "DPTSATKdll", SetLastError = true)]
            public static extern void DPTSATKdll(ref Int32 icomp, ref double TK, ref Int32 KPH, ref double PkPa, ref double RHO, ref double CSAT, ref double DPT,
                ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            //compute the isochoric heat capacity in the two phase (liquid+vapor) region
            [DllImport(refpropDLL_path, EntryPoint = "CV2PKdll", SetLastError = true)]
            public static extern void CV2PKdll(ref Int32 icomp, ref double TK, ref double RHO, ref double cv2p, ref double CSAT, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            //compute surface tension
            [DllImport(refpropDLL_path, EntryPoint = "SURFTdll", SetLastError = true)]
            public static extern void SURFTdll(ref double TK,ref double RHOF,double [] Xl, ref double sigma, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            //compute surface tension
            [DllImport(refpropDLL_path, EntryPoint = "SURTENdll", SetLastError = true)]
            public static extern void SURTENdll(ref double TK, ref double RHOF, ref double RHOG, ref double[] Xl, ref double xv, ref double sigma, 
                ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            #endregion

            #region General Flash Subroutines

            [DllImport(refpropDLL_path, EntryPoint = "TPFLSHdll", SetLastError = true)]
            public static extern void TPFLSHdll(ref double TK, ref double PkPa, ref double [] z, ref double D, ref double Dl, ref double Dv, ref double x, ref double y, ref double q, ref double e,
                ref double h, ref double s, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "TDFLSHdll", SetLastError = true)]
            public static extern void TDFLSHdll(ref double TK, ref double D, ref double z, ref double PkPa, ref double Dl, ref double Dv, ref double x, ref double y, ref double q, ref double e,
                ref double h, ref double s, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "THFLSHdll", SetLastError = true)]
            public static extern void THFLSHdll(ref double TK, ref double h, ref double z, ref Int32 kr, ref double p, ref double D, ref double Dl, ref double Dv, ref double x, ref double y,
                ref double q, ref double e, ref double s, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "TSFLSHdll", SetLastError = true)]
            public static extern void TSFLSHdll(ref double TK, ref double s, ref double z, ref Int32 kr, ref double p, ref double D, ref double Dl, ref double Dv, ref double x, ref double y,
                ref double q, ref double e, ref double h, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "TEFLSHdll", SetLastError = true)]
            public static extern void TEFLSHdll(ref double TK, ref double e, ref double z, ref Int32 kr, ref double p, ref double D, ref double Dl, ref double Dv, ref double x, ref double y,
                ref double q, ref double h, ref double s, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "PDFLSHdll", SetLastError = true)]
            public static extern void PDFLSHdll(ref double PkPa, ref double D, ref double z, ref double TK, ref double Dl, ref double Dv, ref double x, ref double y, ref double q, ref double e,
                ref double h, ref double s, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "PHFLSHdll", SetLastError = true)]
            public static extern void PHFLSHdll(double PkPa, double HJMOL, ref double Z, ref double TK, ref double RHOMOL, ref double RHOFMOL, ref double RHOGMOL, ref double X, ref double Y,
                ref double QUAL, ref double UJMOL, ref double SJMOLK, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "PSFLSHdll", SetLastError = true)]
            public static extern void PSFLSHdll(double PkPa, double SJMOLK, ref double Z, ref double TK, ref double RHOMOL, ref double RHOFMOL, ref double RHOGMOL, ref double X, ref double Y,
                ref double QUAL, ref double UJMOL, ref double HJMOL, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "PEFLSHdll", SetLastError = true)]
            public static extern void PEFLSHdll(double PkPa, double UJMOL, ref double Z, ref double TK, ref double RHOMOL, ref double RHOFMOL, ref double RHOGMOL, ref double X, ref double Y,
                ref double QUAL, ref double HJMOL, ref double SJMOLK, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "HSFLSHdll", SetLastError = true)]
            public static extern void HSFLSHdll(ref double h, ref double s, ref double z, ref double TK, ref double PkPa, ref double D, ref double Dl, ref double Dv, ref double x, ref double y,
                ref double q, ref double e, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "ESFLSHdll", SetLastError = true)]
            public static extern void ESFLSHdll(ref double e, ref double s, ref double z, ref double TK, ref double PkPa, ref double D, ref double Dl, ref double Dv, ref double x, ref double y,
                ref double q, ref double h, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "DHFLSHdll", SetLastError = true)]
            public static extern void DHFLSHdll(double RHOMOLL, double HJMOL, ref double Z, ref double TK, ref double PKPA, ref double RHOFMOL, ref double RHOGMOL, ref double X, ref double Y,
                ref double QUAL, ref double UJMOL, ref double SJMOLK, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "DSFLSHdll", SetLastError = true)]
            public static extern void DSFLSHdll(ref double RHOMOLL, ref double SJMOLK, ref double Z, ref double TK, ref double PkPa, ref double RHOFMOL, ref double RHOGMOL, ref double X, 
                ref double Y, ref double QUAL, ref double UJMOL, ref double HJMOL, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "DEFLSHdll", SetLastError = true)]
            public static extern void DEFLSHdll(double RHOMOLL, double UJMOL, ref double Z, ref double TK, ref double PkPa, ref double RHOFMOL, ref double RHOGMOL, ref double X, ref double Y,
                ref double QUAL, ref double HJMOL, ref double SJMOL, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "TQFLSHdll", SetLastError = true)]
            public static extern void TQFLSHdll(ref double TK, ref double q, ref double[] z, ref Int32 kq, ref double PkPa, ref double D, ref double Dl, ref double Dv, ref double x, ref double y,
                ref double e, ref double h, ref double s, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "PQFLSHdll", SetLastError = true)]
            public static extern void PQFLSHdll(double PkPa, double QUAL, ref double Z, ref Int32 kq, ref double TK, ref double RHOMOL, ref double RHOFMOL, ref double RHOGMOL, ref double X,
                ref double Y, ref double UJMOL, ref double HJMOL, ref double SJMOLK, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);


            #endregion

            #region Single-Phase Flash Subroutines

            [DllImport(refpropDLL_path, EntryPoint = "PSFL1dll", SetLastError = true)]
            public static extern void PSFL1dll(double PKPA, double SJMOLK, ref double[] Z, ref Int32 KPH, ref double TK, ref double RHOMOL, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "TPRHOdll", SetLastError = true)]
            public static extern void TPRHOdll(ref double TK, ref double PkPa, ref double[] X, ref Int32 KPH, ref Int32 kguess, ref double RHO, ref Int32 ierr, char[] HERR, Int32 LengthHERR);


            #endregion

            #region <g class="gr_ gr_182 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="182" id="182">Therodynamic</g> Property Subroutines

            [DllImport(refpropDLL_path, EntryPoint = "CRITPdll", SetLastError = true)]
            public static extern void CRITPdll(double[] X, ref double TCRIT, ref double PCRIT, ref double RHOCRIT, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);


            [DllImport(refpropDLL_path, EntryPoint = "THERMdll", SetLastError = true)]
            public static extern void THERMdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double PkPa, ref double UJMOL, ref double HJMOL, ref double SJMOLK, ref double CVJMOLK,
                ref double CPJMOLK, ref double GAMMAMS, ref double HJTKKPA);

            [DllImport(refpropDLL_path, EntryPoint = "THERM2dll", SetLastError = true)]
            public static extern void THERM2dll(ref double TK, ref double RHOMOLL, ref double[] X, ref double PkPa, ref double UJMOL, ref double HJMOL, ref double SJMOLK, ref double CVJMOLK,
                ref double CPJMOLK, ref double OMEGA, ref double z, ref double HJTKKPA, ref double aH, ref double G, ref double xkappa, ref double beta, ref double dpdrho, ref double d2PdD2, 
                ref double dPT, ref double drhodT, ref double drhodP, ref double d2PT2, ref double d2PdTD, ref double spare3, ref double spare4);

            [DllImport(refpropDLL_path, EntryPoint = "THERM0dll", SetLastError = true)]
            public static extern void THERM0dll(ref double TK, ref double RHO, ref double[] X, ref double p0, ref double e0, ref double h0, ref double s0, ref double cv0, ref double cp00,
                ref double w0, ref double A0, ref double G0);

            [DllImport(refpropDLL_path, EntryPoint = "ENTROdll", SetLastError = true)]
            public static extern void ENTROdll(double TK, double RHOMOLL, double X, ref double SJMOLK);

            [DllImport(refpropDLL_path, EntryPoint = "ENTHALdll", SetLastError = true)]
            public static extern void ENTHALdll(ref double TK, ref double RHOMOLL, ref double X, ref double HJMOL);

            [DllImport(refpropDLL_path, EntryPoint = "CVCPdll", SetLastError = true)]
            public static extern void CVCPdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double CVJMOLK, ref double CPJMOLK);

            [DllImport(refpropDLL_path, EntryPoint = "CVCPKdll", SetLastError = true)]
            public static extern void CVCPKdll(ref Int32 icomp, ref double t, ref double RHO, ref double cv, ref double cp);

            [DllImport(refpropDLL_path, EntryPoint = "GIBBSdll", SetLastError = true)]
            public static extern void GIBBSdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double Ar, ref double Gr);

            [DllImport(refpropDLL_path, EntryPoint = "AGdll", SetLastError = true)]
            public static extern void AGdll(ref double TK, ref double RHO, ref double[] X, ref double a, ref double g);

            [DllImport(refpropDLL_path, EntryPoint = "PRESSdll", SetLastError = true)]
            public static extern void PRESSdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double PkPa);

            [DllImport(refpropDLL_path, EntryPoint = "DPDDdll", SetLastError = true)]
            public static extern void DPDDdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double dpdrho);

            [DllImport(refpropDLL_path, EntryPoint = "DPDDKdll", SetLastError = true)]
            public static extern void DPDDKdll(ref Int32 icomp, ref double TK, ref double RHO, ref double dpdrho);

            [DllImport(refpropDLL_path, EntryPoint = "DPDD2dll", SetLastError = true)]
            public static extern void DPDD2dll(ref double TK, ref double RHOMOLL, ref double[] X, ref double d2PdD2);

            [DllImport(refpropDLL_path, EntryPoint = "DPDTdll", SetLastError = true)]
            public static extern void DPDTdll(double TK, double RHOMOLL, double[] X, ref double DPT);

            [DllImport(refpropDLL_path, EntryPoint = "DPDTKdll", SetLastError = true)]
            public static extern void DPDTKdll(ref Int32 icomp, ref double TK, ref double RHO, ref double dpt);

            [DllImport(refpropDLL_path, EntryPoint = "DDDPdll", SetLastError = true)]
            public static extern void DDDPdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double DRHODP);

            [DllImport(refpropDLL_path, EntryPoint = "DDDTdll", SetLastError = true)]
            public static extern void DDDTdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double DRHODT);

            [DllImport(refpropDLL_path, EntryPoint = "DHD1dll", SetLastError = true)]
            public static extern void DHD1dll(ref double TK, ref double RHO, ref double[] X, ref double dhdt_d, ref double dhdt_p, ref double dhdd_t, ref double dhdd_p, ref double dhdp_t,
                ref double dhdp_d);

            [DllImport(refpropDLL_path, EntryPoint = "FGCTYdll", SetLastError = true)]
            public static extern void FGCTYdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double[] FUGACITY);

            [DllImport(refpropDLL_path, EntryPoint = "CHEMPOTdll", SetLastError = true)]
            public static extern void CHEMPOTdll(ref double TK, ref double RHO, ref double[] X, ref double[] f);

            [DllImport(refpropDLL_path, EntryPoint = "VIRBdll", SetLastError = true)]
            public static extern void VIRBdll(ref double TK, ref double[] X, ref double b);

            [DllImport(refpropDLL_path, EntryPoint = "DBDTdll", SetLastError = true)]
            public static extern void VIRBdll(ref double TK,double [] X,double dbt);

            [DllImport(refpropDLL_path, EntryPoint = "VIRCdll", SetLastError = true)]
            public static extern void VIRCdll(ref double TK,ref double [] X, ref double b);

            [DllImport(refpropDLL_path, EntryPoint = "FPVdll", SetLastError = true)]
            public static extern void FPVdll(ref double TK, ref double RHO, ref double PkPa, ref double[] X, ref double f);

            [DllImport(refpropDLL_path, EntryPoint = "THERM3dll", SetLastError = true)]
            public static extern void THERM3dll(ref double TK, ref double RHO, ref double[] X, ref double xkappa, ref double beta, ref double xisenk, ref double xkt, ref double betas,
                ref double bs, ref double xkkt, ref double thrott, ref double pi, ref double spht);

            [DllImport(refpropDLL_path, EntryPoint = "HEATdll", SetLastError = true)]
            public static extern void HEATdll(ref double TK, ref double RHO, ref double[] X, ref double xkappa, ref double beta, ref double xisenk, ref double xkt, ref double betas,
                ref double bs, ref double xkkt, ref double thrott, ref double pi, ref double spht);

            #endregion

            #region Transport Property Subroutine

            [DllImport(refpropDLL_path, EntryPoint = "TRNPRPdll", SetLastError = true)]
            //public static extern void TRNPRPdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double ETA, ref double TCX, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);
            public static extern void TRNPRPdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double ETA, ref double TCX, ref Int32 ierr, char[] herr, Int32 LengthHERR);


            [DllImport(refpropDLL_path, EntryPoint = "DIELECdll", SetLastError = true)]
            public static extern void DIELECdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double de);

            [DllImport(refpropDLL_path, EntryPoint = "MELTTdll", SetLastError = true)]
            public static extern void MELTTdll(ref double TK,ref double [] X,ref double PkPa, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            [DllImport(refpropDLL_path, EntryPoint = "MELTPdll", SetLastError = true)]
            public static extern void MELTPdll(ref double PkPa, ref double[] X, ref double TK, ref Int32 ierr, char[] HERR, Int32 LengthHERR);

            #endregion

            #region Utility Subroutines

            [DllImport(refpropDLL_path, EntryPoint = "INFOdll", SetLastError = true)]
            public static extern void INFOdll(Int32 icomp, ref double wmm, ref double ttrp, ref double tnbpt, ref double tc, ref double pc, ref double dc, ref double zc,
                ref double acf, ref double dip, ref double rgas);

            [DllImport(refpropDLL_path, EntryPoint = "XMASSdll", SetLastError = true)]
            public static extern void XMASSdll(double X, ref double XKG, ref double WMOL);

            [DllImport(refpropDLL_path, EntryPoint = "XMOLEdll", SetLastError = true)]
            public static extern void XMOLEdll(double X, ref double XKG, ref double WMOL);

            [DllImport(refpropDLL_path, EntryPoint = "LIMITXdll", SetLastError = true)]
            public static extern void LIMITXdll(char[] htyp, double TinK, double rhoin_Mol, double PinkPa, double[] X, ref double TminK, ref double TmaxK, ref double rhomaxMol,
                ref double PmaxkPa, ref Int32 ierr, ref char[] HERR, ref Int32 lnhtyp, ref Int32 lnherr);


            #endregion






            double[] x; //[compsize]={0};
            Int32 nc, ierr;
            char[] HFILES_ch, HRF_ch, HERR_ch, HFMIX_ch;
            string HFILES_st, HRF_st, HFMIX_st;  //HERR_st unable to return HERR as string

            public mf_net_wa__refprop()
            {
                // Now use the functions.

                // Refprop variables that need to be defined
                //
                // nc = Number of components in the mixture
                // x[NumberOfComponentsInMixtures] = Mole fraction of each component
                // ierr =  An integer flag defining an error
                // HFILES[] = a character array defining the fluids in a mixture
                // HRF[] = a character array denoting the reference state
                // HERR[] = a character array for storing a string - Error message
                // HFMIX[] a character array defining the path to the mixture file

                x = new double[compsize];
                HFILES_ch = new char[refpropfluidpathlength + 1];
                HRF_ch = new char[lengthofreference + 1];
                HERR_ch = new char[errormessagelength + 1];
                HFMIX_ch = new char[refpropcharlength + 1];

                nc = 1;
                x[0] = 1.0;

                //    Set <g class="gr_ gr_180 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="180" id="180">HFILES</g> to the path of the R134a.fld file.  Terminate with '\0'
                //    Set HFMIX to the path of the HMX.BNC file.  Terminate with '\0'
                //    Set HRF to a reference...DEF in this case.  Terminate with '\0'
                //    Set HERR to a default error..."ok" for now.  Terminate with '\0'

                // Note, the directory will likely be different on other machines

                HFILES_st="C:\\Program Files\\Refprop7\\Fluids\\R134a.fld"; // trigger error

                HRF_st = "DEF";
                //overwritten by dll HERR_st="Ok";

                // Call SETUPdll to initialize global variables in Refprop
    #if (setup_use_string)
                SETUPdll(ref nc, HFILES_st, HFMIX_st, HRF_st, ref ierr, HERR_ch,
                    refpropfluidpathlength, refpropcharlength,
                    lengthofreference, errormessagelength);
    #else
                convert_string_to_charArray(HFILES_st, HFILES_ch);
                convert_string_to_charArray(HFMIX_st, HFMIX_ch);
                convert_string_to_charArray(HRF_st, HRF_ch);
                //convert_string_to_charArray(HERR_st, HERR_ch);

    // public static extern void SETUPdll(ref Int32 NumberOfComponents, char[] HFILES, char[] HFMIX, char[] HRF, [In, Out] ref Int32 ierr, [MarshalAs(UnmanagedType.LPArray, SizeConst=errormessagelength)] [In, Out] char[] HERR, Int32 l1, Int32 l2, Int32 l3, Int32 l4);
                SETUPdll(ref nc, HFILES_ch, HFMIX_ch, HRF_ch, ref ierr, HERR_ch,
                    refpropfluidpathlength,refpropcharlength,
                    lengthofreference,errormessagelength);
    #endif

                if (ierr != 0)
                {
                    CheckStatus(ierr, HERR_ch, "SETUPdll");
                }

                // Create variables that are to be
                // (a) Used to call the SATPdll function
                // (b) Returned by the SATPdll function

                double[] XLIQ = new double[compsize]; //={0.0};
                double[] XVAP = new double[compsize]; //={0.0};
                // Some values...
                double PkPa, TK, RHOF, RHOG;
                Int32 KPH;

                KPH = 2;    // Vapor
                PkPa = 101.325;

                TK = 0.0; // else unassigned, declared as ref in function
                RHOF = 0.0;
                RHOG = 0.0;

                // public static extern void SATPdll(ref double PkPa, double[] X, ref Int32 KPH, [In, Out] ref double TK, [In, Out] ref double RHOF, [In, Out] ref double RHOG, [Out] double[] XLIQ, [Out] double[] XVAP, [In, Out] ref Int32 ierr, [MarshalAs(UnmanagedType.LPArray, SizeConst=errormessagelength)] [In, Out] char[] HERR, Int32 LengthHERR);
                SATPdll(ref PkPa, x, ref KPH, ref TK, ref RHOF, ref RHOG, XLIQ, XVAP, ref ierr, HERR_ch, errormessagelength);
                // Answers from the Refprop interface program, to check results
                if (ierr != 0)
                {
                    CheckStatus(ierr, HERR_ch, "SATPdll");
                }
                MessageBox.Show(String.Format("sat p {0} t {1} rhoL {2} rhoV {3}", PkPa, TK, RHOF, RHOG));

                // Tsat(101.325) = 247.08 K
                // rhof = 13.493. mol/m^3
                // rhog = 0.051533 mol/m^3

                // Now try a mixture...say R410a

                nc = 2;
                x[0] = 0.69762;
                x[1] = 0.30238;
                //    Set HFILES to the mixture of R32 and R125.  Terminate with '\0'

                HFILES_st = "C:/Program Files/NIST/Refprop7/Fluids/R32.fld|C:/Program Files/NIST/Refprop7/Fluids/R125.fld|";
                //HFILES_st="C:/Program Files/NIST/Refprop7/Fluids/R32.fld|C:/Program Files/NIST/Refprop7/Fluids/R125.fld"; 
                //  above missing '|' (can trigger error) when using char[] HFILES_ch, console: please enter file name   unit 12
                HFMIX_st = "C:/Program Files/NIST/Refprop7/Fluids/HMX.BNC";
                HRF_st = "DEF";
                //overwritten by dll HERR_st="Ok";

    #if (setup_use_string)
                SETUPdll(ref nc, HFILES_st, HFMIX_st, HRF_st, ref ierr, HERR_ch,
                    refpropfluidpathlength, refpropcharlength,
                    lengthofreference, errormessagelength);
    #else
                convert_string_to_charArray(HFILES_st, HFILES_ch);
                convert_string_to_charArray(HFMIX_st, HFMIX_ch);
                convert_string_to_charArray(HRF_st, HRF_ch);
                //convert_string_to_charArray(HERR_st, HERR_ch);

                SETUPdll(ref nc, HFILES_ch, HFMIX_ch, HRF_ch, ref ierr, HERR_ch,
                    refpropfluidpathlength,refpropcharlength,
                    lengthofreference,errormessagelength);
    #endif

                if (ierr != 0)
                {
                    CheckStatus(ierr, HERR_ch, "SETUPdll");
                }

                SATPdll(ref PkPa, x, ref KPH, ref TK, ref RHOF, ref RHOG, XLIQ, XVAP, ref ierr, HERR_ch, errormessagelength);
                if (ierr != 0)
                {
                    CheckStatus(ierr, HERR_ch, "SATPdll");
                }
                MessageBox.Show(String.Format("sat p {0} t {1} rhoL {2} rhoV {3}", PkPa, TK, RHOF, RHOG));
                // For vapor (KPH = 2)
                // Tsat(101.325) =  221.74 K
                // rhof =  18.594 mol/m^3  actually 18.213
                // rhog =  0.057501 mol/m^3
            }
            ~mf_net_wa__refprop()
            {
            }

            void convert_string_to_charArray(string s, char[] c)
            {
                int i;
                if (s.Length + 1 > c.Length)
                {
                    MessageBox.Show(String.Format("string Length {0} +1 > char Length {1} ", s.Length, c.Length));
                    Application.Exit();
                }
                for (i = 0; i < s.Length; i++)
                {
                    c[i] = s[i];
                }
                c[i] = '\0';
            }

            public bool CheckStatus(Int32 ierr, char[] err_msg, String RoutineName)
            {
                string text;
                bool fatal;

                text = new string(err_msg);

                //Int32 sugCap=12*80;  
                //StringBuilder msg=new StringBuilder("Status "+status+" returned by "+RoutineName+'\n', sugCap);
                //msg.Append("error   "+text+'\n');

                fatal = true;
                //if ierr!=0 should throw an exception, particularly if ierr>0
                MessageBox.Show(text);
                return fatal;
            }
        }

    *************end of mf_net_wa__refprop

    the Program class is <g class="gr_ gr_382 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace gr-progress" data-gr-id="382" id="382">showed</g> as <g class="gr_ gr_380 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="380" id="380">folloes</g>:

           

       class Program
        {

            //static Int32 NumberOfComponents = 1;
            static string HFILES1, HFILES2, HFILES, HFMIX, HRF;
            static Int32 ierr, l1, l2, l3, l4;
            static char[] HERR = new char[4];
            const long numparams = 72;
            const long maxcoefs = 50;
            public const Int32 refpropfluidpathlength = (refpropcharlength + 1) * compsize; //+1 for '|' between file names
            public const Int32 refpropcharlength = 255;
            public const Int32 filepathlength = 255;
            public const Int32 lengthofreference = 3;
            public const Int32 errormessagelength = 255;
            public const Int32 compsize = 20;
            static void Main(string[] args)
            {
                double Water_MW = 18.01528;// g/mol; // Molecular Weight
                double[] x; //[compsize]={0};
                x = new double[compsize];
                x[0] = 1.0;
                string locationS = Application.StartupPath;
                Int32 NumberOfComponents = 1;// 1;//1 for pure fluid
                HFILES1 = locationS + "/fluids/" + "WATER.FLD";
                HFILES2 = locationS + "/fluids/" + "WATER.FLD"; //+"R32.FLD";

                HFILES = HFILES1;

                HFMIX = locationS + "/fluids/" + "HMX.BNC";
                HRF = "DEF";//default reference state as specified in fluid file, is applied to each pure component
                l1 = refpropfluidpathlength;
                l2 = refpropcharlength;
                l3 = lengthofreference;
                l4 = errormessagelength;
                //string HFILES, HFMIX, HRF;
                //Int32 ierr, l1, l2, l3, l4;
                //char[] HERR = new char[4];
                //mf_net_wa__refprop fsds = new mf_net_wa__refprop();
                mf_net_wa__refprop.SETUPdll(ref NumberOfComponents, HFILES, HFMIX, HRF, ref ierr, HERR, l1, l2, l3, l4);

                double[] XLIQ = new double[compsize]; //={0.0};
                double[] XVAP = new double[compsize]; //={0.0};
                // Some values...
                double PkPa, TK, RHOF, RHOG;
                Int32 KPH;

                KPH = 2;    // Vapor;
                PkPa = 6.0;// 101.325;

                TK = 0.0; // else unassigned, declared as ref in function
                RHOF = 0.0;
                RHOG = 0.0;
                mf_net_wa__refprop.SATPdll(ref PkPa, x, ref KPH, ref TK, ref RHOF, ref RHOG, XLIQ, XVAP, ref ierr, HERR, errormessagelength);
                //MessageBox.Show(String.Format("sat p {0} t {1} rhoL {2} rhoV {3}", PkPa, TK, RHOF, RHOG));
                // Tsat(101.325) = 247.08 K
                // rhof = 13.493. mol/m^3
                // rhog = 0.051533 mol/m^3

                PkPa = 5681.0;
                TK = 757.15;

                double D_mol_L = 0, Dl_mol_L = 0, Dv_mol_L = 0, q = 0, e = 0, h_J_mol = 0, s_J_molK = 0, cv_J_molK = 0, cp_J_molK = 0, w = 0, X_L = 0, Y_V = 0, cv_KJ_kgK = 0, cp_KJ_kgK = 0,
                    D_kg_m3 = 0, Dl_kg_m3 = 0, Dv_kg_m3 = 0, h_KJ_KG, s_KJ_kgK;

                mf_net_wa__refprop.TPFLSHdll(ref  TK, ref PkPa, ref x, ref D_mol_L, ref Dl_mol_L, ref Dv_mol_L, ref X_L, ref Y_V, ref q, ref e,
                ref  h_J_mol, ref  s_J_molK, ref  cv_J_molK, ref  cp_J_molK, ref  w, ref ierr, HERR, errormessagelength);

                D_kg_m3 = D_mol_L * Water_MW;
                Dl_kg_m3 = Dl_mol_L * Water_MW;
                Dv_kg_m3 = Dv_mol_L * Water_MW;
                h_KJ_KG = h_J_mol / Water_MW;
                s_KJ_kgK = s_J_molK / Water_MW;
                cv_KJ_kgK = cv_J_molK / Water_MW;
                cp_KJ_kgK = cp_J_molK / Water_MW;

                double ETA_uPaS = 0, TCX_W_mK = 0, PrandtlNum = 0;
                mf_net_wa__refprop.TRNPRPdll(ref TK, ref D_mol_L, ref x, ref ETA_uPaS, ref TCX_W_mK, ref ierr, HERR, errormessagelength);
                PrandtlNum = cp_KJ_kgK * ETA_uPaS / (TCX_W_mK * 1000.0);

                NumberOfComponents = 2;


            }
        }

    ************** end of Program.

    Thanks a lot for your help.

    Friday, March 9, 2018 5:37 PM
  • Please use the code block element in the editor when pasting in code. It is too hard to read otherwise.

    You should never use a DllImport statement with a hardcoded path. This will fail on any machine without the exact same path. Just put the DLL name in the import. If the DLL is not in the search path for Windows you can either add it, ensure the DLL is in the same location as the .NET app (preferred) or use some code to find the actual binary. But this isn't the cause of your issues (locally).

    In the code you posted for mf_net_wa_refprop I don't see the import you originally said you were having an issue with (TRNPRPdll). Was this an oversight or did you reference the wrong code?

    Assuming TRNPRPdll is the issue we need to find the function in the header file.

    typedef void (__stdcall *fp_TRNPRPdllTYPE)(double &,double &,double *,double &,double &,long &,char*,long );

    It is using the STDCALL convention. It is using ref parameters which isn't actually valid in C so this automatically causes some issues. You'll have to treat them as pointers but this may or may not work correctly.

    The second to last parameter is an ANSI string so it'll have to be marshalled as such.

    But, to save a little time, let's skip your code for a second and see if you can use this wrapper that was written by NIST to wrap the refprop library. They include a sample app. If that works then just use their library code. As a reference here is their DllImport.

    [DllImport(@"C:\Program Files (x86)\REFPROP\REFPRP64.dll", CharSet = CharSet.Ansi)]
    public static extern void TRNPRPdll
            (
                ref double t,
                ref double d,
                [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] x,
                ref double eta,
                ref double tcx,
                ref long ierr,
                [MarshalAs(UnmanagedType.VBByRefStr)] ref string herr,
                ref long ln
            );


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, March 9, 2018 5:47 PM
    Moderator
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace RefpropCSharp
    {
        class Program
        {
    
            //static Int32 NumberOfComponents = 1;
            static string HFILES1, HFILES2, HFILES, HFMIX, HRF;
            static Int32 ierr, l1, l2, l3, l4;
            static char[] HERR = new char[4];
            const long numparams = 72;
            const long maxcoefs = 50;
            public const Int32 refpropfluidpathlength = (refpropcharlength + 1) * compsize; //+1 for '|' between file names
            public const Int32 refpropcharlength = 255;
            public const Int32 filepathlength = 255;
            public const Int32 lengthofreference = 3;
            public const Int32 errormessagelength = 255;
            public const Int32 compsize = 20;
            static void Main(string[] args)
            {
                double Water_MW = 18.01528;// g/mol; // Molecular Weight
                double[] x; //[compsize]={0};
                x = new double[compsize];
                x[0] = 1.0;
                string locationS = Application.StartupPath;
                Int32 NumberOfComponents = 1;// 1;//1 for pure fluid
                HFILES1 = locationS + "/fluids/" + "WATER.FLD";
                HFILES2 = locationS + "/fluids/" + "WATER.FLD"; //+"R32.FLD";
    
                HFILES = HFILES1;
    
                HFMIX = locationS + "/fluids/" + "HMX.BNC";
                HRF = "DEF";//default reference state as specified in fluid file, is applied to each pure component
                l1 = refpropfluidpathlength;
                l2 = refpropcharlength;
                l3 = lengthofreference;
                l4 = errormessagelength;
                //string HFILES, HFMIX, HRF;
                //Int32 ierr, l1, l2, l3, l4;
                //char[] HERR = new char[4];
                //mf_net_wa__refprop fsds = new mf_net_wa__refprop();
                mf_net_wa__refprop.SETUPdll(ref NumberOfComponents, HFILES, HFMIX, HRF, ref ierr, HERR, l1, l2, l3, l4);
    
                double[] XLIQ = new double[compsize]; //={0.0};
                double[] XVAP = new double[compsize]; //={0.0};
                // Some values...
                double PkPa, TK, RHOF, RHOG;
                Int32 KPH;
    
                KPH = 2;    // Vapor;
                PkPa = 6.0;// 101.325;
    
                TK = 0.0; // else unassigned, declared as ref in function
                RHOF = 0.0;
                RHOG = 0.0;
                mf_net_wa__refprop.SATPdll(ref PkPa, x, ref KPH, ref TK, ref RHOF, ref RHOG, XLIQ, XVAP, ref ierr, HERR, errormessagelength);
                //MessageBox.Show(String.Format("sat p {0} t {1} rhoL {2} rhoV {3}", PkPa, TK, RHOF, RHOG));
                // Tsat(101.325) = 247.08 K
                // rhof = 13.493. mol/m^3
                // rhog = 0.051533 mol/m^3
    
                PkPa = 5681.0;
                TK = 757.15;
    
                double D_mol_L = 0, Dl_mol_L = 0, Dv_mol_L = 0, q = 0, e = 0, h_J_mol = 0, s_J_molK = 0, cv_J_molK = 0, cp_J_molK = 0, w = 0, X_L = 0, Y_V = 0, cv_KJ_kgK = 0, cp_KJ_kgK = 0,
                    D_kg_m3 = 0, Dl_kg_m3 = 0, Dv_kg_m3 = 0, h_KJ_KG, s_KJ_kgK;
    
                mf_net_wa__refprop.TPFLSHdll(ref  TK, ref PkPa, ref x, ref D_mol_L, ref Dl_mol_L, ref Dv_mol_L, ref X_L, ref Y_V, ref q, ref e,
                ref  h_J_mol, ref  s_J_molK, ref  cv_J_molK, ref  cp_J_molK, ref  w, ref ierr, HERR, errormessagelength);
    
                D_kg_m3 = D_mol_L * Water_MW;
                Dl_kg_m3 = Dl_mol_L * Water_MW;
                Dv_kg_m3 = Dv_mol_L * Water_MW;
                h_KJ_KG = h_J_mol / Water_MW;
                s_KJ_kgK = s_J_molK / Water_MW;
                cv_KJ_kgK = cv_J_molK / Water_MW;
                cp_KJ_kgK = cp_J_molK / Water_MW;
    
                double ETA_uPaS = 0, TCX_W_mK = 0, PrandtlNum = 0;
                mf_net_wa__refprop.TRNPRPdll(ref TK, ref D_mol_L, ref x, ref ETA_uPaS, ref TCX_W_mK, ref ierr, HERR, errormessagelength);
                PrandtlNum = cp_KJ_kgK * ETA_uPaS / (TCX_W_mK * 1000.0);
    
    
            }
        }
    }
    

    #define setup_use_string
    
    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Windows.Forms;
    
    
    /*
     * Units
    temperature                     K
    pressure, fugacity              kPa
    density                         mol/L
    composition                     mole fraction
    quality                         mole basis (moles vapor/total moles)
    enthalpy, internal energy       J/mol
    Gibbs, Helmholtz free energy    J/mol
    entropy, heat capacity          J/(mol.K)
    speed of sound                  m/s
    Joule-Thompson coefficient      K/kPa
    d(p)/d(rho)                     kPa.L/mol
    d2(p)/d(rho)2                   kPa.(L/mol)^2
    viscosity                       microPa.s (10^-6 Pa.s)
    thermal conductivity            W/(m.K)
    dipole moment                   debye
    surface tension                 N/m
    
    Note:  The only exceptions to the above are the conversion utilities
           XMASS and XMOLE.  The graphical interface allows a wide variety
           of units, but not the subroutines.
    */
    
    // all variables in unmanaged DLL function need to be managed types in code  i.e. Int32 instead of long
    
    namespace RefpropCSharp
    {
    
        /*
        for nc=2 
        HFILES_st="C:/Program Files/NIST/Refprop7/Fluids/R32.fld|C:/Program Files/NIST/Refprop7/Fluids/R125.fld|";
        if using HFILES_ch, if terminating '|' is not there, unable to find file for 2nd component
        console:  please enter file name   unit 12
        */
    
    
        // modified Refdemo.cpp
        // consider changing the names of the variables in this sample (and Refdemo.cpp) to match those in manual.txt
        //      subroutine SETUP (nc,hfiles,hfmix,hrf,ierr,herr)
        //      subroutine SATP (p,x,kph,t,rhol,rhov,xliq,xvap,ierr,herr)
    
        public class mf_net_wa__refprop
        {
            // Some constants...
            //public const Int32 refpropfluidpathlength=10000;
            public const Int32 refpropfluidpathlength = (refpropcharlength + 1) * compsize; //+1 for '|' between file names
    
            public const Int32 refpropcharlength = 255;
            public const Int32 filepathlength = 255;
            public const Int32 lengthofreference = 3;
            public const Int32 errormessagelength = 255;
            public const Int32 compsize = 20;
    
            //const string FluidsDirectory = "c:/Program Files/NIST/RefProp7/fluids/";
            //const string MixturesDirectory = "c:/Program Files/NIST/RefProp7/mixtures/";
            //const string refpropDLL_path = "c:/Program Files/NIST/RefProp7/Refprop.dll";
            const string locationS = "C:/Users/Desktop/RefpropCSharp/RefpropCSharp/bin/Debug";
            const string FluidsDirectory = locationS + "/fluids/";
            const string MixturesDirectory = locationS + "/mixtures/";
            const string refpropDLL_path = locationS + "/Refprop.dll";
    
            #region Initialization Subroutines
            [DllImport(refpropDLL_path, EntryPoint = "SETUPdll", SetLastError = true)]
    #if (setup_use_string)
            public static extern void SETUPdll(ref Int32 NumberOfComponents, string HFILES, string HFMIX, string HRF, ref Int32 ierr, char[] HERR, Int32 l1, Int32 l2, Int32 l3, Int32 l4);
            //public static extern void SETUPdll(ref Int32 NumberOfComponents, string HFILES, string HFMIX, string HRF, [In, Out] ref Int32 ierr, [In, Out] char[] HERR, Int32 l1, Int32 l2, Int32 l3, Int32 l4);
    #else
                public static extern void SETUPdll(ref Int32 NumberOfComponents, char[] HFILES, char[] HFMIX, char[] HRF, [In, Out] ref Int32 ierr,  [In, Out] char[] HERR, Int32 l1, Int32 l2, Int32 l3, Int32 l4);
    #endif
    
            [DllImport(refpropDLL_path, EntryPoint = "SETKTVdll", SetLastError = true)]
            public static extern void SETKTVdll(Int32 icomp, Int32 jcomp, char hmodij, double fij, char hfmix, ref Int32 ierr, ref char[] herr, ref Int32 Lengthhmodij, ref Int32 Lengthhfmix, Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "GETKTVdll", SetLastError = true)]
            public static extern void GETKTVdll(Int32 icomp, Int32 jcomp, ref char hmodij, ref double fij, ref char hfmix, ref char hfij, ref char hbinp, ref char hmxrul, ref Int32 ln1,
                ref Int32 ln2, ref Int32 ln3, ref Int32 ln4, ref Int32 ln5);
    
            [DllImport(refpropDLL_path, EntryPoint = "GETFIJdll", SetLastError = true)]
            public static extern void GETFIJdll(char hmodij, ref char fij, ref char hfij, ref char hmxrul, ref Int32 ln1, ref Int32 ln2, ref Int32 ln3);
    
            #endregion
    
            #region Saturation-State Subroutines
    
            //iterate for saturated pressure, liquid density and vapor density by given temperature and the composition of one phase
            [DllImport(refpropDLL_path, EntryPoint = "SATTdll", SetLastError = true)]
            public static extern void SATTdll(ref double TK, double[] X, ref Int32 KPH, ref double PkPa, ref double RHOF, ref double RHOG, double[] XLIQ,
               double[] XVAP, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            //iterate for saturated temperature, liquid density and vapor density by given pressure and the composition of one phase
            [DllImport(refpropDLL_path, EntryPoint = "SATPdll", SetLastError = true)]
            public static extern void SATPdll(ref double PkPa, double[] X, ref Int32 KPH, ref double TK, ref double RHOF, ref double RHOG, double[] XLIQ,
                double[] XVAP, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            //iterate for temperature and pressure given a density along the saturation boundary and the composition
            [DllImport(refpropDLL_path, EntryPoint = "SATDdll", SetLastError = true)]
            public static extern void SATDdll(ref double RHOMOLL, ref double[] X, ref Int32 KPH, ref Int32 kr, ref double TK, ref double PkPa, ref double RHOF, ref double RHOG, ref double XLIQ,
                ref double XVAP, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            //iterate for temperature, pressure, and density given enthalpy along the saturation boundary and the composition
            [DllImport(refpropDLL_path, EntryPoint = "SATHdll", SetLastError = true)]
            public static extern void SATHdll(ref double h, double[] X, ref Int32 KPH, ref Int32 nroot, ref Int32 k1, ref double t1, ref double p1, ref double d1, ref Int32 k2, ref double t2,
                ref double p2, ref double d2, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            //iterate for temperature, pressure, and density given an entropy along the saturation boundary and the composition
            [DllImport(refpropDLL_path, EntryPoint = "SATSdll", SetLastError = true)]
            public static extern void SATSdll(ref double s,double [] X, ref Int32 KPH, ref Int32 nroot, ref Int32 k1, ref double t1, ref double p1, ref double d1, ref Int32 k2, ref double t2,
                ref double p2, ref double d2, ref Int32 k3, ref double t3, ref double p3, ref double d3, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            //compute the heat capacity along the saturation line as a function of temperature for a given component
            [DllImport(refpropDLL_path, EntryPoint = "CSATKdll", SetLastError = true)]
            public static extern void CSATKdll(ref Int32 icomp, ref double TK, ref Int32 KPH, ref double PkPa, ref double RHO, ref double CSAT, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            //compute the heat capacity and dP/dT along the saturation line as a function of temperature for a given component. See also subroutine CSATK.
            [DllImport(refpropDLL_path, EntryPoint = "DPTSATKdll", SetLastError = true)]
            public static extern void DPTSATKdll(ref Int32 icomp, ref double TK, ref Int32 KPH, ref double PkPa, ref double RHO, ref double CSAT, ref double DPT,
                ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            //compute the isochoric heat capacity in the two phase (liquid+vapor) region
            [DllImport(refpropDLL_path, EntryPoint = "CV2PKdll", SetLastError = true)]
            public static extern void CV2PKdll(ref Int32 icomp, ref double TK, ref double RHO, ref double cv2p, ref double CSAT, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            //compute surface tension
            [DllImport(refpropDLL_path, EntryPoint = "SURFTdll", SetLastError = true)]
            public static extern void SURFTdll(ref double TK,ref double RHOF,double [] Xl, ref double sigma, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            //compute surface tension
            [DllImport(refpropDLL_path, EntryPoint = "SURTENdll", SetLastError = true)]
            public static extern void SURTENdll(ref double TK, ref double RHOF, ref double RHOG, ref double[] Xl, ref double xv, ref double sigma, 
                ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            #endregion
    
            #region General Flash Subroutines
    
            [DllImport(refpropDLL_path, EntryPoint = "TPFLSHdll", SetLastError = true)]
            public static extern void TPFLSHdll(ref double TK, ref double PkPa, ref double [] z, ref double D, ref double Dl, ref double Dv, ref double x, ref double y, ref double q, ref double e,
                ref double h, ref double s, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "TDFLSHdll", SetLastError = true)]
            public static extern void TDFLSHdll(ref double TK, ref double D, ref double z, ref double PkPa, ref double Dl, ref double Dv, ref double x, ref double y, ref double q, ref double e,
                ref double h, ref double s, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "THFLSHdll", SetLastError = true)]
            public static extern void THFLSHdll(ref double TK, ref double h, ref double z, ref Int32 kr, ref double p, ref double D, ref double Dl, ref double Dv, ref double x, ref double y,
                ref double q, ref double e, ref double s, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "TSFLSHdll", SetLastError = true)]
            public static extern void TSFLSHdll(ref double TK, ref double s, ref double z, ref Int32 kr, ref double p, ref double D, ref double Dl, ref double Dv, ref double x, ref double y,
                ref double q, ref double e, ref double h, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "TEFLSHdll", SetLastError = true)]
            public static extern void TEFLSHdll(ref double TK, ref double e, ref double z, ref Int32 kr, ref double p, ref double D, ref double Dl, ref double Dv, ref double x, ref double y,
                ref double q, ref double h, ref double s, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "PDFLSHdll", SetLastError = true)]
            public static extern void PDFLSHdll(ref double PkPa, ref double D, ref double z, ref double TK, ref double Dl, ref double Dv, ref double x, ref double y, ref double q, ref double e,
                ref double h, ref double s, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "PHFLSHdll", SetLastError = true)]
            public static extern void PHFLSHdll(double PkPa, double HJMOL, ref double Z, ref double TK, ref double RHOMOL, ref double RHOFMOL, ref double RHOGMOL, ref double X, ref double Y,
                ref double QUAL, ref double UJMOL, ref double SJMOLK, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "PSFLSHdll", SetLastError = true)]
            public static extern void PSFLSHdll(double PkPa, double SJMOLK, ref double Z, ref double TK, ref double RHOMOL, ref double RHOFMOL, ref double RHOGMOL, ref double X, ref double Y,
                ref double QUAL, ref double UJMOL, ref double HJMOL, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "PEFLSHdll", SetLastError = true)]
            public static extern void PEFLSHdll(double PkPa, double UJMOL, ref double Z, ref double TK, ref double RHOMOL, ref double RHOFMOL, ref double RHOGMOL, ref double X, ref double Y,
                ref double QUAL, ref double HJMOL, ref double SJMOLK, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "HSFLSHdll", SetLastError = true)]
            public static extern void HSFLSHdll(ref double h, ref double s, ref double z, ref double TK, ref double PkPa, ref double D, ref double Dl, ref double Dv, ref double x, ref double y,
                ref double q, ref double e, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "ESFLSHdll", SetLastError = true)]
            public static extern void ESFLSHdll(ref double e, ref double s, ref double z, ref double TK, ref double PkPa, ref double D, ref double Dl, ref double Dv, ref double x, ref double y,
                ref double q, ref double h, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "DHFLSHdll", SetLastError = true)]
            public static extern void DHFLSHdll(double RHOMOLL, double HJMOL, ref double Z, ref double TK, ref double PKPA, ref double RHOFMOL, ref double RHOGMOL, ref double X, ref double Y,
                ref double QUAL, ref double UJMOL, ref double SJMOLK, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "DSFLSHdll", SetLastError = true)]
            public static extern void DSFLSHdll(ref double RHOMOLL, ref double SJMOLK, ref double Z, ref double TK, ref double PkPa, ref double RHOFMOL, ref double RHOGMOL, ref double X, 
                ref double Y, ref double QUAL, ref double UJMOL, ref double HJMOL, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "DEFLSHdll", SetLastError = true)]
            public static extern void DEFLSHdll(double RHOMOLL, double UJMOL, ref double Z, ref double TK, ref double PkPa, ref double RHOFMOL, ref double RHOGMOL, ref double X, ref double Y,
                ref double QUAL, ref double HJMOL, ref double SJMOL, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "TQFLSHdll", SetLastError = true)]
            public static extern void TQFLSHdll(ref double TK, ref double q, ref double[] z, ref Int32 kq, ref double PkPa, ref double D, ref double Dl, ref double Dv, ref double x, ref double y,
                ref double e, ref double h, ref double s, ref double cv, ref double cp, ref double w, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "PQFLSHdll", SetLastError = true)]
            public static extern void PQFLSHdll(double PkPa, double QUAL, ref double Z, ref Int32 kq, ref double TK, ref double RHOMOL, ref double RHOFMOL, ref double RHOGMOL, ref double X,
                ref double Y, ref double UJMOL, ref double HJMOL, ref double SJMOLK, ref double CV, ref double CP, ref double W, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);
    
    
            #endregion
    
            #region Single-Phase Flash Subroutines
    
            [DllImport(refpropDLL_path, EntryPoint = "PSFL1dll", SetLastError = true)]
            public static extern void PSFL1dll(double PKPA, double SJMOLK, ref double[] Z, ref Int32 KPH, ref double TK, ref double RHOMOL, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "TPRHOdll", SetLastError = true)]
            public static extern void TPRHOdll(ref double TK, ref double PkPa, ref double[] X, ref Int32 KPH, ref Int32 kguess, ref double RHO, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
    
            #endregion
    
            #region Therodynamic Property Subroutines
    
            [DllImport(refpropDLL_path, EntryPoint = "CRITPdll", SetLastError = true)]
            public static extern void CRITPdll(double[] X, ref double TCRIT, ref double PCRIT, ref double RHOCRIT, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);
    
    
            [DllImport(refpropDLL_path, EntryPoint = "THERMdll", SetLastError = true)]
            public static extern void THERMdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double PkPa, ref double UJMOL, ref double HJMOL, ref double SJMOLK, ref double CVJMOLK,
                ref double CPJMOLK, ref double GAMMAMS, ref double HJTKKPA);
    
            [DllImport(refpropDLL_path, EntryPoint = "THERM2dll", SetLastError = true)]
            public static extern void THERM2dll(ref double TK, ref double RHOMOLL, ref double[] X, ref double PkPa, ref double UJMOL, ref double HJMOL, ref double SJMOLK, ref double CVJMOLK,
                ref double CPJMOLK, ref double OMEGA, ref double z, ref double HJTKKPA, ref double aH, ref double G, ref double xkappa, ref double beta, ref double dpdrho, ref double d2PdD2, 
                ref double dPT, ref double drhodT, ref double drhodP, ref double d2PT2, ref double d2PdTD, ref double spare3, ref double spare4);
    
            [DllImport(refpropDLL_path, EntryPoint = "THERM0dll", SetLastError = true)]
            public static extern void THERM0dll(ref double TK, ref double RHO, ref double[] X, ref double p0, ref double e0, ref double h0, ref double s0, ref double cv0, ref double cp00,
                ref double w0, ref double A0, ref double G0);
    
            [DllImport(refpropDLL_path, EntryPoint = "ENTROdll", SetLastError = true)]
            public static extern void ENTROdll(double TK, double RHOMOLL, double X, ref double SJMOLK);
    
            [DllImport(refpropDLL_path, EntryPoint = "ENTHALdll", SetLastError = true)]
            public static extern void ENTHALdll(ref double TK, ref double RHOMOLL, ref double X, ref double HJMOL);
    
            [DllImport(refpropDLL_path, EntryPoint = "CVCPdll", SetLastError = true)]
            public static extern void CVCPdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double CVJMOLK, ref double CPJMOLK);
    
            [DllImport(refpropDLL_path, EntryPoint = "CVCPKdll", SetLastError = true)]
            public static extern void CVCPKdll(ref Int32 icomp, ref double t, ref double RHO, ref double cv, ref double cp);
    
            [DllImport(refpropDLL_path, EntryPoint = "GIBBSdll", SetLastError = true)]
            public static extern void GIBBSdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double Ar, ref double Gr);
    
            [DllImport(refpropDLL_path, EntryPoint = "AGdll", SetLastError = true)]
            public static extern void AGdll(ref double TK, ref double RHO, ref double[] X, ref double a, ref double g);
    
            [DllImport(refpropDLL_path, EntryPoint = "PRESSdll", SetLastError = true)]
            public static extern void PRESSdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double PkPa);
    
            [DllImport(refpropDLL_path, EntryPoint = "DPDDdll", SetLastError = true)]
            public static extern void DPDDdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double dpdrho);
    
            [DllImport(refpropDLL_path, EntryPoint = "DPDDKdll", SetLastError = true)]
            public static extern void DPDDKdll(ref Int32 icomp, ref double TK, ref double RHO, ref double dpdrho);
    
            [DllImport(refpropDLL_path, EntryPoint = "DPDD2dll", SetLastError = true)]
            public static extern void DPDD2dll(ref double TK, ref double RHOMOLL, ref double[] X, ref double d2PdD2);
    
            [DllImport(refpropDLL_path, EntryPoint = "DPDTdll", SetLastError = true)]
            public static extern void DPDTdll(double TK, double RHOMOLL, double[] X, ref double DPT);
    
            [DllImport(refpropDLL_path, EntryPoint = "DPDTKdll", SetLastError = true)]
            public static extern void DPDTKdll(ref Int32 icomp, ref double TK, ref double RHO, ref double dpt);
    
            [DllImport(refpropDLL_path, EntryPoint = "DDDPdll", SetLastError = true)]
            public static extern void DDDPdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double DRHODP);
    
            [DllImport(refpropDLL_path, EntryPoint = "DDDTdll", SetLastError = true)]
            public static extern void DDDTdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double DRHODT);
    
            [DllImport(refpropDLL_path, EntryPoint = "DHD1dll", SetLastError = true)]
            public static extern void DHD1dll(ref double TK, ref double RHO, ref double[] X, ref double dhdt_d, ref double dhdt_p, ref double dhdd_t, ref double dhdd_p, ref double dhdp_t,
                ref double dhdp_d);
    
            [DllImport(refpropDLL_path, EntryPoint = "FGCTYdll", SetLastError = true)]
            public static extern void FGCTYdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double[] FUGACITY);
    
            [DllImport(refpropDLL_path, EntryPoint = "CHEMPOTdll", SetLastError = true)]
            public static extern void CHEMPOTdll(ref double TK, ref double RHO, ref double[] X, ref double[] f);
    
            [DllImport(refpropDLL_path, EntryPoint = "VIRBdll", SetLastError = true)]
            public static extern void VIRBdll(ref double TK, ref double[] X, ref double b);
    
            [DllImport(refpropDLL_path, EntryPoint = "DBDTdll", SetLastError = true)]
            public static extern void VIRBdll(ref double TK,double [] X,double dbt);
    
            [DllImport(refpropDLL_path, EntryPoint = "VIRCdll", SetLastError = true)]
            public static extern void VIRCdll(ref double TK,ref double [] X, ref double b);
    
            [DllImport(refpropDLL_path, EntryPoint = "FPVdll", SetLastError = true)]
            public static extern void FPVdll(ref double TK, ref double RHO, ref double PkPa, ref double[] X, ref double f);
    
            [DllImport(refpropDLL_path, EntryPoint = "THERM3dll", SetLastError = true)]
            public static extern void THERM3dll(ref double TK, ref double RHO, ref double[] X, ref double xkappa, ref double beta, ref double xisenk, ref double xkt, ref double betas,
                ref double bs, ref double xkkt, ref double thrott, ref double pi, ref double spht);
    
            [DllImport(refpropDLL_path, EntryPoint = "HEATdll", SetLastError = true)]
            public static extern void HEATdll(ref double TK, ref double RHO, ref double[] X, ref double xkappa, ref double beta, ref double xisenk, ref double xkt, ref double betas,
                ref double bs, ref double xkkt, ref double thrott, ref double pi, ref double spht);
    
            #endregion
    
            #region Transport Property Subroutine
    
            [DllImport(refpropDLL_path, EntryPoint = "TRNPRPdll", SetLastError = true)]
            //public static extern void TRNPRPdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double ETA, ref double TCX, ref Int32 ierr, ref char[] HERR, ref Int32 LengthHERR);
            public static extern void TRNPRPdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double ETA, ref double TCX, ref Int32 ierr, char[] herr, Int32 LengthHERR);
    
    
            [DllImport(refpropDLL_path, EntryPoint = "DIELECdll", SetLastError = true)]
            public static extern void DIELECdll(ref double TK, ref double RHOMOLL, ref double[] X, ref double de);
    
            [DllImport(refpropDLL_path, EntryPoint = "MELTTdll", SetLastError = true)]
            public static extern void MELTTdll(ref double TK,ref double [] X,ref double PkPa, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            [DllImport(refpropDLL_path, EntryPoint = "MELTPdll", SetLastError = true)]
            public static extern void MELTPdll(ref double PkPa, ref double[] X, ref double TK, ref Int32 ierr, char[] HERR, Int32 LengthHERR);
    
            #endregion
    
            #region Utility Subroutines
    
            [DllImport(refpropDLL_path, EntryPoint = "INFOdll", SetLastError = true)]
            public static extern void INFOdll(Int32 icomp, ref double wmm, ref double ttrp, ref double tnbpt, ref double tc, ref double pc, ref double dc, ref double zc,
                ref double acf, ref double dip, ref double rgas);
    
            [DllImport(refpropDLL_path, EntryPoint = "XMASSdll", SetLastError = true)]
            public static extern void XMASSdll(double X, ref double XKG, ref double WMOL);
    
            [DllImport(refpropDLL_path, EntryPoint = "XMOLEdll", SetLastError = true)]
            public static extern void XMOLEdll(double X, ref double XKG, ref double WMOL);
    
            [DllImport(refpropDLL_path, EntryPoint = "LIMITXdll", SetLastError = true)]
            public static extern void LIMITXdll(char[] htyp, double TinK, double rhoin_Mol, double PinkPa, double[] X, ref double TminK, ref double TmaxK, ref double rhomaxMol,
                ref double PmaxkPa, ref Int32 ierr, ref char[] HERR, ref Int32 lnhtyp, ref Int32 lnherr);
    
    
            #endregion
    
    
    
    
    
    
            double[] x; //[compsize]={0};
            Int32 nc, ierr;
            char[] HFILES_ch, HRF_ch, HERR_ch, HFMIX_ch;
            string HFILES_st, HRF_st, HFMIX_st;  //HERR_st unable to return HERR as string
    
            public mf_net_wa__refprop()
            {
                // Now use the functions.
    
                // Refprop variables that need to be defined
                //
                // nc = Number of components in the mixture
                // x[NumberOfComponentsInMixtures] = Mole fraction of each component
                // ierr =  An integer flag defining an error
                // HFILES[] = a character array defining the fluids in a mixture
                // HRF[] = a character array denoting the reference state
                // HERR[] = a character array for storing a string - Error message
                // HFMIX[] a character array defining the path to the mixture file
    
                x = new double[compsize];
                HFILES_ch = new char[refpropfluidpathlength + 1];
                HRF_ch = new char[lengthofreference + 1];
                HERR_ch = new char[errormessagelength + 1];
                HFMIX_ch = new char[refpropcharlength + 1];
    
                nc = 1;
                x[0] = 1.0;
    
                //    Set HFILES to the path of the R134a.fld file.  Terminate with '\0'
                //    Set HFMIX to the path of the HMX.BNC file.  Terminate with '\0'
                //    Set HRF to a reference...DEF in this case.  Terminate with '\0'
                //    Set HERR to a default error..."ok" for now.  Terminate with '\0'
    
                // Note, the directory will likely be different on other machines
    
                HFILES_st="C:\\Program Files\\Refprop7\\Fluids\\R134a.fld"; // trigger error
    
                HRF_st = "DEF";
                //overwritten by dll HERR_st="Ok";
    
                // Call SETUPdll to initialize global variables in Refprop
    #if (setup_use_string)
                SETUPdll(ref nc, HFILES_st, HFMIX_st, HRF_st, ref ierr, HERR_ch,
                    refpropfluidpathlength, refpropcharlength,
                    lengthofreference, errormessagelength);
    #else
                convert_string_to_charArray(HFILES_st, HFILES_ch);
                convert_string_to_charArray(HFMIX_st, HFMIX_ch);
                convert_string_to_charArray(HRF_st, HRF_ch);
                //convert_string_to_charArray(HERR_st, HERR_ch);
    
    //		public static extern void SETUPdll(ref Int32 NumberOfComponents, char[] HFILES, char[] HFMIX, char[] HRF, [In, Out] ref Int32 ierr, [MarshalAs(UnmanagedType.LPArray, SizeConst=errormessagelength)] [In, Out] char[] HERR, Int32 l1, Int32 l2, Int32 l3, Int32 l4);
                SETUPdll(ref nc, HFILES_ch, HFMIX_ch, HRF_ch, ref ierr, HERR_ch,
                    refpropfluidpathlength,refpropcharlength,
                    lengthofreference,errormessagelength);
    #endif
    
                if (ierr != 0)
                {
                    CheckStatus(ierr, HERR_ch, "SETUPdll");
                }
    
                // Create variables that are to be
                // (a) Used to call the SATPdll function
                // (b) Returned by the SATPdll function
    
                double[] XLIQ = new double[compsize]; //={0.0};
                double[] XVAP = new double[compsize]; //={0.0};
                // Some values...
                double PkPa, TK, RHOF, RHOG;
                Int32 KPH;
    
                KPH = 2;    // Vapor
                PkPa = 101.325;
    
                TK = 0.0; // else unassigned, declared as ref in function
                RHOF = 0.0;
                RHOG = 0.0;
    
                //		public static extern void SATPdll(ref double PkPa, double[] X, ref Int32 KPH, [In, Out] ref double TK, [In, Out] ref double RHOF, [In, Out] ref double RHOG, [Out] double[] XLIQ, [Out] double[] XVAP, [In, Out] ref Int32 ierr, [MarshalAs(UnmanagedType.LPArray, SizeConst=errormessagelength)] [In, Out] char[] HERR, Int32 LengthHERR);
                SATPdll(ref PkPa, x, ref KPH, ref TK, ref RHOF, ref RHOG, XLIQ, XVAP, ref ierr, HERR_ch, errormessagelength);
                // Answers from the Refprop interface program, to check results
                if (ierr != 0)
                {
                    CheckStatus(ierr, HERR_ch, "SATPdll");
                }
                MessageBox.Show(String.Format("sat p {0} t {1} rhoL {2} rhoV {3}", PkPa, TK, RHOF, RHOG));
    
                // Tsat(101.325) = 247.08 K
                // rhof = 13.493. mol/m^3
                // rhog = 0.051533 mol/m^3
    
                // Now try a mixture...say R410a
    
                nc = 2;
                x[0] = 0.69762;
                x[1] = 0.30238;
                //    Set HFILES to the mixture of R32 and R125.  Terminate with '\0'
    
                HFILES_st = "C:/Program Files/NIST/Refprop7/Fluids/R32.fld|C:/Program Files/NIST/Refprop7/Fluids/R125.fld|";
                //HFILES_st="C:/Program Files/NIST/Refprop7/Fluids/R32.fld|C:/Program Files/NIST/Refprop7/Fluids/R125.fld"; 
                //  above missing '|' (can trigger error) when using char[] HFILES_ch, console: please enter file name   unit 12
                HFMIX_st = "C:/Program Files/NIST/Refprop7/Fluids/HMX.BNC";
                HRF_st = "DEF";
                //overwritten by dll HERR_st="Ok";
    
    #if (setup_use_string)
                SETUPdll(ref nc, HFILES_st, HFMIX_st, HRF_st, ref ierr, HERR_ch,
                    refpropfluidpathlength, refpropcharlength,
                    lengthofreference, errormessagelength);
    #else
                convert_string_to_charArray(HFILES_st, HFILES_ch);
                convert_string_to_charArray(HFMIX_st, HFMIX_ch);
                convert_string_to_charArray(HRF_st, HRF_ch);
                //convert_string_to_charArray(HERR_st, HERR_ch);
    
                SETUPdll(ref nc, HFILES_ch, HFMIX_ch, HRF_ch, ref ierr, HERR_ch,
                    refpropfluidpathlength,refpropcharlength,
                    lengthofreference,errormessagelength);
    #endif
    
                if (ierr != 0)
                {
                    CheckStatus(ierr, HERR_ch, "SETUPdll");
                }
    
                SATPdll(ref PkPa, x, ref KPH, ref TK, ref RHOF, ref RHOG, XLIQ, XVAP, ref ierr, HERR_ch, errormessagelength);
                if (ierr != 0)
                {
                    CheckStatus(ierr, HERR_ch, "SATPdll");
                }
                MessageBox.Show(String.Format("sat p {0} t {1} rhoL {2} rhoV {3}", PkPa, TK, RHOF, RHOG));
                // For vapor (KPH = 2)
                // Tsat(101.325) =  221.74 K
                // rhof =  18.594 mol/m^3  actually 18.213
                // rhog =  0.057501 mol/m^3
            }
            ~mf_net_wa__refprop()
            {
            }
    
            void convert_string_to_charArray(string s, char[] c)
            {
                int i;
                if (s.Length + 1 > c.Length)
                {
                    MessageBox.Show(String.Format("string Length {0} +1 > char Length {1} ", s.Length, c.Length));
                    Application.Exit();
                }
                for (i = 0; i < s.Length; i++)
                {
                    c[i] = s[i];
                }
                c[i] = '\0';
            }
    
            public bool CheckStatus(Int32 ierr, char[] err_msg, String RoutineName)
            {
                string text;
                bool fatal;
    
                text = new string(err_msg);
    
                //Int32 sugCap=12*80;  
                //StringBuilder msg=new StringBuilder("Status "+status+" returned by "+RoutineName+'\n', sugCap);
                //msg.Append("error   "+text+'\n');
    
                fatal = true;
                //if ierr!=0 should throw an exception, particularly if ierr>0
                MessageBox.Show(text);
                return fatal;
            }
        }
    }
    
    I am sorry this is my first asking question here. I do not know Code block. Please find the attached code. Thanks a lot for your help. Sometimes, the code does give correct results. But sometimes <g class="gr_ gr_283 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" data-gr-id="283" id="283">if</g> stops without results or a warning message.
    Friday, March 9, 2018 6:26 PM
  • What is the code you're posting now? Please only post the code fragments that are needed for the discussion. Nobody is going to read all those lines of code.

    Did you try using the wrapper library that I mentioned? If not did you try copying the import statement I provided? If you tried both and neither worked then please provide an update on the error you're getting.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, March 9, 2018 6:34 PM
    Moderator
  • Hi Michael Taylor,

    I have tried what you said, but it is not working. Meanwhile, after I removed "ref" in the third parameter "double [] X", the code works. Thanks a lot for your help. Have a good day!

    Saturday, March 10, 2018 10:32 AM