Functions

ID #1034

How to call function from DLL using LoadLibrary and GetProcAddress with condition that function may take parameters and return result?

  MAP
MODULE('RTL')
LoadLibrary(STRING _cLibName),LONG,RAW, |
NAME('Clw$LoadLibrary')
GetProcAddr(LONG _LibHandle,STRING _cProcName),LONG,RAW,PASCAL, |
NAME('GetProcAddress')
END
MODULE('')
dll_CallProc(LONG _ProcAddr,LONG _Param1,*CSTRING _Param2),LONG, |
NAME('dll_CallProc')
END
dll::ProcType(LONG _Param1,*CSTRING _Param2),LONG,RAW,PASCAL,TYPE
CallProc(dll::ProcType _Proc,LONG _Param1,*CSTRING _Param2),LONG, |
NAME('dll_CallProc')
END
csStr  CSTRING(40)
CODE
hLib# = LoadLibrary('MYLIB.DLL<0>')
IF hLib#
ProcAddr# = GetProcAddr(hLib#,'MyFunc<0>')
IF ProcAddr#
csStr = 'Hello!'
a# = dll_CallProc(ProcAddr#,1,csStr)
END
END
RETURN
CallProc PROCEDURE(dll::ProcType _Proc,LONG _Param1,*CSTRING _Param2)
CODE
RETURN(_Proc(_Param1,_Param2))

This code is written in single program and without additional modules. Below detail explanation:
  • declare typed function dll::ProcType, so it must be called from DLL module. It is naturally, if this function work with parameters as usual C-funciton, you should use attribute PASCAL instead C.
  • Create function-adapter for calling of DLL-function. Pay your attention, you must use attribute NAME to override name that will be given by Clarion.
  • in order to pass into this function-adapter address of wanted function received with help of GetProcAddr, we declare it as external dll_CallProc with any type of parameters.

Tags: external function, function adapter, dll

Related entries:

Last update: 2007-07-15 19:36
Author: Oleg Rudenko
Revision: 1.0

Digg it! Print this record Send to a friend Show this as PDF file
Propose a translation for Propose a translation for
Please rate this entry:

Average rating: 5 out of 5 (1 Votes )

completely useless 1 2 3 4 5 most valuable

You can comment on this entry