A method for reading result from DOE2/eQUEST by calling function in D2Result.dll

4 posts / 0 new
Last post

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

via Equest-users's picture
Joined: 2016-07-15
Reputation: 400

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

via Equest-users's picture
Joined: 2016-07-15
Reputation: 400

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 Joneswrote?

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

via Equest-users's picture
Joined: 2016-07-15
Reputation: 400

Thank you!

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

via Equest-users's picture
Joined: 2016-07-15
Reputation: 400