[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
Tue Nov 29 20:44:16 PST 2016


Below is VBA version:


Public Declare PtrSafe Function D2R_VBGetSingleResultsing Lib "doe22\dll32.ref\D2Result.dll" Alias "D2R_VBGetSingleResult" (ByVal a As String, _
    ByVal b As String, _
    ByVal c As LongPtr, _
    ByRef d As Single, _
    ByVal e As LongPtr, _
    ByVal f As String, _
    ByVal g As String _
) As LongPtr


Dim fReslt As Single
pszDoe = "doe22\exent.ref\"
pszFileName = "Project 10\Project 10 - Baseline Design"
iEntryID = 2001001
D2R_VBGetSingleResultsing pszDoe, pszFileName, iEntryID, fReslt, 1, "", ""
Sheet1.Cells(1, 1).Value = fReslt



On 11/25/2016 20:58, Chris Jones<Christopher.Jones at RWDI.com>wrote:

Thanks for this example.

 

I have wondered if anyone has used the VB version within an Excel application?

 

 

Christopher Jones, P. Eng.

Rowan Williams Davies & Irwin Inc.
Consulting Engineers & Scientists
901 King Street West, Suite 400, Toronto, Ontario, M5V 3H5
T: (519) 823-1311 ext 2052

M: (416) 697-0056

 

From: Equest-users [mailto:equest-users-bounces at lists.onebuilding.org] On Behalf Of zhaoyongqing1987 via Equest-users
Sent: Friday, November 25, 2016 4:36 AM
To: equest-users
Subject: [Equest-users] A method for reading result from DOE2/eQUEST by calling function in D2Result.dll

 

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();

        }

    }

}

 

 

 

RWDI - One of Canada's 50 Best Managed Companies - This communication is intended for the sole use of the party to whom it was addressed and may contain information that is privileged and/or confidential. Any other distribution, copying or disclosure is strictly prohibited. If you received this email in error, please notify us immediately by replying to this email and delete the message without retaining any hard or electronic copies of same. Outgoing emails are scanned for viruses, but no warranty is made to their absence in this email or attachments. If you require any information supplied by RWDI in a different format to facilitate accessibility, contact the sender of the email, email solutions at rwdi.com or call +1.519.823.1311.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.onebuilding.org/pipermail/equest-users-onebuilding.org/attachments/20161130/4be4042a/attachment-0002.htm>


More information about the Equest-users mailing list