Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question
4

How to Load D2Result.dll from eQUEST/DOE-2

asked 2016-11-14 14:05:21 -0500

pflaumingo's avatar

updated 2016-11-28 21:16:48 -0500

Has anyone got some nice instructions on how to load and work with the D2Result.dll library distributed with eQUEST? It's used for parsing results. I've been able to register is with regsvr32 successfully, but whenever I try to load it into a Visual Studio 2015 I run into the following error:

"A reference to 'C:\Program Files (x86)\eQUEST 3-64\D2Result.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assmebly or COM component.

I'm on Windows 7 64-bit and have tried both the 32 and 64 bit regsvr32.

edit retag flag offensive close merge delete

Comments

1

@pflaumingo Do you really need to load it into Visual Studio? If yes, I cannot help but I've done it in the past using Python (here - not sure if the files are up-to-date on the repo...)

Jeremy's avatar Jeremy  ( 2016-11-14 14:26:06 -0500 )edit

Awesome, thanks for the link! I've got it up and running. I was mainly just asking for Visual Studio because C# is my preferred platform/IDE. If anyone has any insight into how to get up and running in Visual Studio (VB or C#) then that'd be awesome. I'll run with @Jeremy's work for now.

pflaumingo's avatar pflaumingo  ( 2016-11-14 16:01:00 -0500 )edit

@pflaumingo just in case, I've committed my last mods. Glad you were able to get it to run!

Jeremy's avatar Jeremy  ( 2016-11-14 16:12:29 -0500 )edit

Do you know what a -99999.0 return value means? Or more generally where I can find descriptions of the return values?

pflaumingo's avatar pflaumingo  ( 2016-11-14 19:28:40 -0500 )edit

I can still only seem to get it to return -99999.0 even using your test files. I'm using 64-bit python with the 64-bit DLLs, have you seen any differences between the DLLs?

pflaumingo's avatar pflaumingo  ( 2016-11-15 10:38:00 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2016-11-15 13:44:44 -0500

pflaumingo's avatar

Thanks to @Jeremy for helping me get up and running. Here is my solution for C#:

  1. Create a static class to access the Win32 functions needed to load an unmanaged DLL dynamically.
  2. Then create a delegate matching the form of the D2Result.dll function that is desired. This can then be used in a program as follows:
    1. pfData will store the value you are trying to retrieve.

using System;

using System.Runtime.InteropServices;

namespace ConsoleApplication21 { static class DLLMethods { [DllImport("kernel32.dll")] public static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

    [DllImport("kernel32.dll")]
    public static extern bool FreeLibrary(IntPtr hModule);
}

class Program
{
    [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);

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

        string pszDOE2Dir = ; // Insert path to DOE-2 Dir i.e "C:\\DOE-2\\
        string pszFileName = ; // Insert file path of where the input and run files are kept
        int iEntryID = 2001001;
        int iMaxValues = 1;
        float[] pfData = new float[iMaxValues];
        string pszReportKey = null;
        string pszRowKey = null;

        long result = getSingleResult(pszDOE2Dir, pszFileName, iEntryID, pfData, iMaxValues, pszReportKey, pszRowKey);

        bool freedom = DLLMethods.FreeLibrary(pDll);
    }
}

}

edit flag offensive delete link more

Comments

Can someone fix the formatting? I've given up...

pflaumingo's avatar pflaumingo  ( 2016-11-15 13:45:07 -0500 )edit
1

answered 2016-11-25 03:13:37 -0500

yongqingzhao's avatar

Blow program can read mutiple results:


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;    // FM1 array from PS-F
        MRTs[1].pszReportKey = "FM1";
        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);

        for (int i = 0; i < 39;i++ )
            Console.WriteLine("pfData is {0}", fResults[i]);

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

}

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Careers

Question Tools

1 follower

Stats

Asked: 2016-11-14 14:05:21 -0500

Seen: 561 times

Last updated: Nov 25 '16