I'm relatively new to using Visual Basic for programming, but not new to programming using other procedural languages. I'm currently working on a personal project that requires an implementation that I can't seem to be able to find a VB example of in all my reference books on VB version 5.0. If any of you might be able to guide me, I'd be very grateful.
Here's the problem. I have a section of code that computes a particular function or calls a subroutine that looks like this:
Private Sub Example()
Dim varbl as Single, a as Single, b as Integer, c as Single
varbl = FX(a, b, c)
call FXY(a, b, c, varbl)
FX is a 'dll' file, say present in the path: "c:\path\fx1.dll"
FXY is also a 'dll', say, "c:\path\fxy1.dll"
So far, pretty standard stuff. But,.........Now, here's what I need help on. I'd like to be able to DYNAMICALLY (while the program is running) be able to substitute different functional/subroutine calculation procedures, each being contained within its own unique 'dll', WITHOUT CHANGING the calling code OR function/subr NAME in the example, i.e., 'FX(a, b, c)' or 'FXY(a,b,c,varbl). I'd have different "dll's" for each unique calculation, such as: fx2.dll, fx3.dll, ...., fxn.dll for the functions, and similarly, 'fxy2.dll, fxy3.dll, .....,fxyn.dll' for the subroutines, all in, say, the "c:\path\...." path. The I/O argument list would be identical (both in variable order and type) for each similar function and/or subr. Only the actual calculation procedure would be different for each function/subr..
My intent is to set up index numbers and 'path' strings in an array so that I could selectively choose which function calc or subr calc I want. If a new function/subr is desired, I could easily add it for selection, as long as its associated '...dll' file was on the disk. The purpose of this is to be able to RETAIN all the old calcuation algorithms, and AT RUN TIME, INDEFINITELY ADD new, trial ALTERNATIVE test algorithms for a GIVEN, FIXED procedure name, WITHOUT EVER HAVING TO RECODE AND RECOMPILE the main 'calling' test program.
In other words, I could ADD and test these identically called (having the SAME entry-point NAME and argument list, but differently calculated) procedures by ONLY CHANGING THE ALGORITHMS in each newly compiled 'DLL', and dynamically selecting (by its path/filename) the chosen dll's procedure to use, but WITHOUT having to alter any compiled-code in the main calling program, which would be compiled once and 'cast in concrete'. I'd be able to enter any new 'dll' filename and path into a textbox, and using '.additem' be able to append each new test algorithm to an existing list of available test dll's in a combobox. A string array would hold the unique names I'd assign to each function (or Subr), such as 'Trial Function 1' or 'Trial Function 2', each of these names being passed BACK thru a string variable in the argument list of its function/subr (to identify which calc is being tested).
Operationally, by highlighting and clicking on the desired name in the combobox, its 'click' event would then procede to assign and set up the addressing/access links to the arbitrarily-selected 'dll' file and load and link the code of this selected procedure DURING RUNTIME. As new test functions/subr's are added, their file location info would be updated in an appended sequential file, which would be opened and read into the proper arrays each time the main program was run.
(Note that using a new 'Declare....' statement for each new routine does NOT solve the problem, as this requires altering source code at design/compile time of the main test program, and as far as I can tell, VB does not directly provide a way to alter 'Declare...' statements AT RUNTIME.)
So the real question that I need answered is: how do I program VB to load and link ('point' to) the correct entry location for each of the dll's that have the SAME IDENTIFIER entry calling name, such as the 'FX(....)' and 'FXY(.....)' in the Example above. How can VB be programmed to LINK an arbitrary 'new' dll procedure at ...RUN... time, NOT AT COMPILE time, from a NEWLY ADDED 'dll' having the same 'FX(...)' entry-point identifier. It seems to me that some kind of pointer is required that is coupled to the 'entry name' identifier of a function or subr, and when that identifier is invoked during a call, where that pointer points is where the system goes for continuing its execution flow. This would allow for dynamically adding and switching/selecting different routines, but ALL having the SAME calling NAME AND ARGUMENT list during a program's execution, WITHOUT having to recompile and add unique code to the main program for each new different function/subr. (The 'uniqueness' could be embedded in the value of the 'index' number.)
It would be okay to 'release' memory back to the system from the 'replaced' function/subr load if necessary to load the new 'dll' code, OR it would also be okay to simply ADD newly loaded code to all the 'dll' algorithms loaded so far and used during a given run of the main program. If a new load request is made, and the code is already in memory, only the entry-address and argument pointers would need to be changed.
Expanding the original example above to clarify the concept, the code might look something like this:
Private Sub Example()
Dim varbl as Single, a as Single, b as Integer, c as String
Dim index as Integer, dllarray(25) as String, pointer as Long
' run the FX and FXY procedures that do four different algorithm's:
For index = 1 to 4
' this info is specified and obtained in conventional ways:
path = dllarray(index) ' e.g.,dllarray(?) = "c:\test\fx?.dll", where ?=index number
' (the following is the area I DON'T know how to do!!)
' load and set up to link/use code from each 'dll', "fx?.dll" and "fxy?.dll":
pointer = ? ' this points to particular entry pt FX(...) of "fx?.dll"
' (there may also have to be pointers set here for the input[array-data,variables,strings] & output [array data,variables,strings] arguments here as well?)
' (setups for the FXY dll's would be put here:
' this is the calling code that is to STAY THE SAME:
varbl = FX(a, b, c) ' resulting calc depends upon the selected '...dll' chosen,
call FXY(a, b, c, varbl) ' and likewise here.
I'm assuming there IS a way to do this!! Can any of you help? Please let me know if you need further clarification of the problem, or how to solve it if you know how.
Thanks a bunch!!!! (:->)