Calling and external DLL to calculate a variable
The fastest and the most flexible way to calculate a custom variable is to call a external DLL that can be written and compiled in Visual basic 6. The DLL has access to all the internal variables in the software as well as the constants and has the "responsibility" to pass back a single calculated value back to the software. There are no predefined limits on the functionality of the DLL and it can contain code, static variables, complex mathematical and logical code etc. Everything that can be done as a script can also be done in a DLL - however you loose a little convenience, but gain processing speed.
In general if you are only going to use the script to analyse a few runs, or the runs are short - then it is probably best to use just the scripting option, however if you are processing lots of long runs then an external DLL is the best solution.
To reference an external DLL is very simple, within the variables manager - go to the units or edit equation and click the Custom option. In the description enter the name of the DLL as follows:
Note the exact syntax, the line must contain "external dll" with no spelling changes and the name of the DLL must be specified in quotation marks. When you click okay the analysis program will check the DLL can be accessed and can be used.
The format of the external DLL is relatively simple, but there are a few important notes:
1. The project name must be unchanged as "RtExternalVars"
2. The name of the class module and the DLLs file name must be exactly the same, and must be as specified in the variable manager, in this example it is simple called "MyClass"
3. Only 2 calls are made to the DLL "Initialise" which is called first, then "CalcVariable" which is called for each time step
4. The DLL must be registered using regsvr32 and in the main analysis program directory
5. When using Vista there maybe problems registering the DLL depending the security that is set - consult the Microsoft documentation for details
Within the DLL there are only 2 functions, namely:
In the case of an external DLL being called to convert a variable:
1. The initialise function is called to set up all the constants in the DLL (things like temperature of the run, CdA of the car etc)
2. Then for each time step, the "CalcVariable" function is called with all the other variable data for that time step:
Public Function CalcVariable(ByRef RT_Var() As Single, ByRef RT_VarIndexs() As Long) As Single
For each call the "RT_Var" array contains the values of all the other variables, and "RT_VarIndexs" contains the information of how to map or index the variable index into the RT_Var array. So for example if you want to access the Time Variable, which is VAR_0001 in the software, this would be accessed using RT_Var(RT_VarIndexs(1)) in the DLL. The function must return a single value back to the software using:
CalcVariable = "value to return here"
As a final note, the software makes no attempt to put a time out on the calls to the DLL, so if the DLL crashes then it is entirely possible that it will lock up the software.