Hello,
For those that are interested in Dynamic API call, I wrote years ago a simple API Call layer that was used into AutoHotkey scripting language and co.
Here's the C code MSVC++ and LCC WIn32 compatible (may be adapted for GCC) :
//--------------------------------------------------------------------------------
//Author : Gerome GUILLEMIN
//Dynamic API :: the ultimate code !
//--------------------------------------------------------------------------------
//!!!!!!!!!!!! POWER !!!!!!!!!!!!
//If you appreciate this sample code, please register to FBSL
//Enjoy and take care
//--------------------------------------------------------------------------------
// Examples :
/*
CallDllFx("Sleep","kernel32",1, 1000);
CallDllFx("SleepEx","kernel32",2, 2000, 1);
CallDllFx("WritePrivateProfileString","kernel32", 4, "Section1", "SecondKey", "By golly, it works.", "c:\\appname.ini");
*/
// C implementation:
int WINAPI CallDllFx (char *FuncName, char *DllName, int nArgs, ...)
{
HINSTANCE hInst = 0;
FARPROC lpAddr = 0;
int arg = 0;
int result = 0;
char buff[128] = {'\0'};
va_list ap;
int argtable[128]; //[nArgs]; // in LCC WIN32 because of the Dynamic Support !
memset( &argtable, 0, sizeof(argtable) );
hInst=GetModuleHandle(DllName);
if(hInst==NULL) {
hInst=LoadLibrary(DllName);
}
else {
//DBS( "(0)Library '%s' already loaded !", DllName );
}
lpAddr=(FARPROC)GetProcAddress(hInst,FuncName);
if(lpAddr==NULL) {
sprintf(buff,"%s%s",FuncName,"A");
lpAddr=(FARPROC)GetProcAddress(hInst,buff);
}
if(lpAddr==NULL) {
sprintf(buff,"%s%s","_",FuncName);
lpAddr=(FARPROC)GetProcAddress(hInst,buff);
}
if (lpAddr) {
va_start(ap,nArgs);
for (register int i=0; i<nArgs;i++) {
arg = va_arg(ap,int);
argtable[i] = arg;
}
for (register int ii=nArgs-1;ii >=0; ii--) {
arg = argtable[ii];
}
va_end( ap );
__try {
result = lpAddr();
} __except ( EXCEPTION_EXECUTE_HANDLER ) {
if (hInst) FreeLibrary(hInst);
return -1;
}
}
if(hInst) FreeLibrary(hInst);
return result;
}
Enjoy !