I have a need to load at runtime specific custom dll's by customer. I used some examples from the forum to get the idea and came up with the following. It works but I'm concerned if there are problems with this method.
Declare and Macro code
PBMAIN code snippet
LBCHNPRD is the root of the DLL name
GET_CHN_PRD is the name of the function to call
CHN_PRD$ is the returned value EXPORTED AS STRING
1 is the number of the DLL being called to keep the pointers unique to the DLL being called.
BYCOPY F1.ACTNR$ is the single parameter I'm passing to the DLL.
I'm considering using OPTIONAL and ANY to make the MACRO more generic.
Any comments and/or warnings and/or suggestions are welcome.
Bob Mechler
Declare and Macro code
Code:
DECLARE FUNCTION GET_CHN_PRD(STRING) AS STRING MACRO CALLDLL1(prm1,prm2,prm3,prm4,prm5) 'This variable passes a single parameter 'Other variations would just have prm6,prm7 etc. 'prm1 root name of dll 'prm2 function name in dll 'prm3 return value. can be -1,0,any type of number or string 'prm4 Number of DLL call in program. If more than 1 dll is called then it may be up to 10 (arbitrary limit) 'prm5 Variable 1 passed BYREF DIM hLibrary???(10) DIM pProc???(10) 'The DLL would be in PBSRC if running at out office and 'in the program directory at the customer site. IF NOT FNSP(CUST_DIR$) THEN DLLPATH$ = "J:\PBSRC\TNET\" + CUST_DIR$ + "\" ELSE DLLPATH$ = "" END IF DLLNAME$ = prm1 DLLNAME$ = DLLPATH$ + DLLNAME$ + ".DLL" hLibrary???(prm4) = LoadLibrary(BYCOPY DLLNAME$) IF hLibrary???(prm4) THEN pProc???(prm4) = GetProcAddress(hLibrary???(prm4),prm2) IF pProc???(prm4) THEN CALL DWORD pProc???(prm4) USING GET_CHN_PRD(prm5) TO prm3 CALL FreeLibrary(hLibrary???(prm4)) END IF ELSE MSGBOX "Required DLL " + DLLNAME$ + " is not present. Aborting program." ,%MB_OK OR %MB_ICONWARNING,"File not found" GOTO EJOB END IF END MACRO
Code:
IF CTRU THEN 'where CTRU is the customer code CALLDLL1("LBCHNPRD","GET_CHN_PRD",CHN_PRD$,1,BYCOPY F1.ACTNR$) END IF
GET_CHN_PRD is the name of the function to call
CHN_PRD$ is the returned value EXPORTED AS STRING
1 is the number of the DLL being called to keep the pointers unique to the DLL being called.
BYCOPY F1.ACTNR$ is the single parameter I'm passing to the DLL.
I'm considering using OPTIONAL and ANY to make the MACRO more generic.
Any comments and/or warnings and/or suggestions are welcome.
Bob Mechler
Comment