IonicWind Software

IWBasic => The Roundtable => Topic started by: WayneA on February 15, 2009, 11:59:19 PM

Title: Dynamic API
Post by: WayneA on February 15, 2009, 11:59:19 PM
A port of original C code by a fella named Gerome

http://uolang.org/sourcecode/c/dynamic_api3.txt

AutoDefine "Off"
Export CallDllFx
Declare Import,LoadLibrary Alias LoadLibraryA(lpLibFileName As String),Int
Declare Import,FreeLibrary(hLibModule As Int),Int
Declare Import,GetModuleHandle Alias GetModuleHandleA(lpModuleName As String),Int
Declare Import,GetProcAddress(hModule As Int,lpProcName As String),Int
Declare CDecl Extern _sprintf(buffer As String,format As String,...)
Declare CDecl Extern _memset(dst As Pointer,value As Char,count As UInt)
Declare CallDllFx(FuncName As String,DllName As String,nArgs As Int,...),Int

CallDllFx("MessageBox","user32",4,0,"World","Hello",32)

Sub CallDllFx(FuncName As String,DllName As String,nArgs As Int,...),Int
Dim arg As Int
Dim result As Int
Dim lpAddr As UInt
Dim hInst,argtable[128],i As Int
Dim buff As String
Dim ap As Pointer
SetType ap,Int
_memset(argtable,0,Len(argtable))
buff=""
hInst=GetModuleHandle(DllName)
If hInst=NULL Then hInst=LoadLibrary(DllName)
lpAddr=GetProcAddress(hInst,FuncName)
If lpAddr=NULL Then
_sprintf(buff,"%s%s",FuncName,"A")
lpAddr=GetProcAddress(hInst,buff)
EndIf
If lpAddr=NULL Then
_sprintf(buff,"%s%s","_",FuncName)
lpAddr=GetProcAddress(hInst,buff)
EndIf
If lpAddr Then
ap=Va_Start(nArgs)
ap-=4
For i=0 to nArgs-1
ap+=4
arg=#ap
argtable[i]=arg
Next i
For i=nArgs-1 to 0 Step -1
arg=argtable[i]
_asm
push dword [ebp-4]
_endasm
Next i
_asm
call [ebp-12]
mov [ebp-8],eax
_endasm
EndIf
If hInst Then FreeLibrary(hInst)
Return result
EndSub


Special thanks to Fletchie for his assistance with the last 2 asm commands. Without his help this would not have ever worked most likely.

The original posting of this (as IBPro code) can be found here:

http://uolang.org/sourcecode/basic/dynamicapi.txt
Title: Re: Dynamic API
Post by: REDEBOLT on February 16, 2009, 10:39:54 AM
May I ask:

"What is the program for, and what does it do?"

Title: Re: Dynamic API
Post by: John Syl. on February 16, 2009, 01:46:54 PM
Looking at the original, it allows one to call functions from a dll using a simplified call rather than a library load and declaring of all the functions that are required normally.