May 07, 2024, 12:15:28 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Dynamic API

Started by WayneA, February 15, 2009, 11:59:19 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

WayneA

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
99 little bugs in the code,
99 bugs in the code,
Fix one bug,
Compile again,
104 little bugs in the code...

All code I post is in the public domain.

REDEBOLT

May I ask:

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

Regards,
Bob

John Syl.

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.


Intel 3.6 p4 ht, XP home,2 gb mem, 400 gb hd 20gb raid 0, Nvidia 6600le.
AMD k6-2 500, 40gb.

Started on PDP11 Assembler, BASIC, GWBASIC, 6502, Z80, 80x86, Java, Pascal, C, C++, 
IBasic (std & pro), Aurora, EBasic.  (Master of none, but it's been fun!)