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

Revision history [back]

click to hide/show revision 1
initial version

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

}