[Equest-users] A method for reading result from DOE2/eQUEST by calling function in D2Result.dll

zhaoyongqing1987 via Equest-users equest-users at lists.onebuilding.org
Fri Nov 25 01:36:20 PST 2016


I share a method for reading result from DOE2/eQUEST by calling function in D2Result.dll.
Before run the program,you must the D2Result.dll to your project folder.Below is the source code for c#.
-------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace ConsoleApplication1
{


    [StructLayout(LayoutKind.Sequential,Pack=4)]
    public struct MultResultsType
    {
        public int  iEntryID;      // from NHRList.txt
        public int  iReturnValue;  // success/failure
        [MarshalAs(UnmanagedType.ByValTStr,SizeConst =40)]
        public string pszReportKey;
        [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 40)]
        public string pszRowKey;
    }
    class Program
    {
        [DllImport("kernel32.dll")]
        public extern static IntPtr LoadLibrary(string dllToLoad);
        [DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
        public extern static IntPtr GetProcAddress(IntPtr hModule, string procedureName);
        [DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
        public extern static bool FreeLibrary(IntPtr hModule);
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate long D2R_GetSingleResult([MarshalAs(UnmanagedType.LPStr)]string pszDOE2Dir,
            [MarshalAs(UnmanagedType.LPStr)]string pszFileName,
            int iEntryID,
            float[] pfData,
            int iMaxValues,
            [MarshalAs(UnmanagedType.LPStr)]string pszReportKey,
            [MarshalAs(UnmanagedType.LPStr)]string pszRowKey);


        private delegate long D2R_GetMultipleResult([MarshalAs(UnmanagedType.LPStr)]string pszDOE2Dir,
            [MarshalAs(UnmanagedType.LPStr)]string pszFileName,
            int iFileType,
            //[MarshalAs(UnmanagedType.LPArray)]
            float[] pfData,
            int iMaxValues,
            int iNumMRTs,
            [In,Out]IntPtr pMRTs);


        unsafe public static  IntPtr MarshalArray(ref MultResultsType[] bodies)
        {
            int iSizeOfOneBodyPos = Marshal.SizeOf(typeof(MultResultsType));
            int iTotalSize = iSizeOfOneBodyPos * bodies.Length;
            IntPtr pUnmanagedBodies = Marshal.AllocHGlobal(iTotalSize);
            byte* pbyUnmanagedBodies = (byte*)(pUnmanagedBodies.ToPointer());


            for (int i = 0; i < bodies.Length; i++, pbyUnmanagedBodies += (iSizeOfOneBodyPos))
            {
                IntPtr pOneBodyPos = new IntPtr(pbyUnmanagedBodies);
                Marshal.StructureToPtr(bodies[i], pOneBodyPos, false);
            }


            return pUnmanagedBodies;
        }
       
        static void Main(string[] args)
        {
            // Dynamically Load Library
            var pDll = LoadLibrary("D2Result.dll"); // Path to D2Result.dll
            // Get Address of Function
            var pFunctionAddress = GetProcAddress(pDll, "D2R_GetMultipleResult");
            // Instantiate Delegate
            D2R_GetMultipleResult D2R_GetMultipleResult = (D2R_GetMultipleResult)Marshal.GetDelegateForFunctionPointer(pFunctionAddress, typeof(D2R_GetMultipleResult));


            string pszDOE2Dir = "doe22\\exent.ref\\"; // Insert path to DOE-2 Dir i.e "C:\\DOE-2\\
            string pszFileName = "Project 10\\Project 10 - Baseline Design"; // Insert file path of where the input and run files are kept
           
            float[] pfData = new float[1] ;
            //string pszReportKey = null;
            //string pszRowKey = null;
            MultResultsType[] MRTs=new MultResultsType[4];
            MRTs[0].iEntryID = 2309007;    // EM1 array from PS-F
            MRTs[0].pszReportKey = "EM1";
            MRTs[0].pszRowKey = "";


            MRTs[1].iEntryID = 2309007;    // EM2 array from PS-F
            MRTs[1].pszReportKey = "EM1";
            MRTs[1].pszRowKey = "";


            MRTs[2].iEntryID = 2305005;    // Elec Mtr Totals from PS-E
            MRTs[2].pszReportKey = "";
            MRTs[2].pszRowKey = "";     
            IntPtr pt = MarshalArray(ref MRTs);
            float[] fResults=new float[39];
            long lRetVal = D2R_GetMultipleResult(
                                 pszDOE2Dir,
                                 pszFileName,
                                 1, fResults, 39, 3, pt);




           // long result = getSingleResult(pszDOE2Dir, pszFileName, iEntryID, pfData, iMaxValues, pszReportKey, pszRowKey);
            for (int i = 0; i < 39;i++ )
                Console.WriteLine("pfData is {0}", fResults[i]);


            bool freedom = FreeLibrary(pDll);
           
            Console.ReadKey();
        }
    }
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.onebuilding.org/pipermail/equest-users-onebuilding.org/attachments/20161125/0e351613/attachment.htm>


More information about the Equest-users mailing list