Qbasicnews.com

Full Version: How to call a api-function by pointer?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hallo all,

how to call a API-Function directly by the functions-address?

This example works fine in GFA-Basic:

Code:
hUser32Dll = LoadLibraryEx("USER32.DLL", 0, 0)
addr = GetProcAddress(hUser32Dll, "MessageBeep")
StdCall(addr)(0)  ' here you hear "Beep"
FreeLibrary(hUser32Dll)

I tried to use "StdCall()" like in GFA-Basic and I tried also "CALL DWORD ..." like in PowerBasic but it will not work like that in freeBASIC. Maybe it will work with "asm" but sorry, I didn't know anything about assembler.

Kind regards from Germany

Peter
I don't think those are freeBasic functions. In FreeBASIC, to use API calls, you have to declare them, something like this:

Code:
Declare function whatever alias "whatever[a,b,c]" (byref argument1,...) as returntype

Remember, FreeBASIC doesn't use PowerBASIC or GFA-BASIC syntaxes. It uses QBASIC syntax, and some of the time VB code will work too. The above sample is how to use API functions in VB, and it's the same in FB.
0.13 has two built-in functions that let you deal with loading dynamic libs at runtime in a platform independent way: DYLIBLOAD and DYLIBSYMBOL.
On CVS there's an example that shows their usage:

http://cvs.sourceforge.net/viewcvs.py/fb...iew=markup
In FB you use function pointers for that, not ugly CALL DWORD's or asm.

With 0.13 you will be able to create real function pointer typedef's, in 0.12 you can do:

Code:
dim myfunc as function( ...arg decl... ) returntype
    myfunc = ...function address...
    result = myfunc( ...params... )
Quote:I don't think those are freeBasic functions. In FreeBASIC, to use API calls, you have to declare them, something like this:

Yes, I know, but my problem is: In freeBASIC until now I can only declare them, if I have an lib*.dll.a - file. And I have much problems to create this files because sometimes pexprorts is crashing and sometimes dlltool is crashing. :-(

But now I found out: It's very easy to call e Windows-Api-Function without a dll.a-LibFile, here is an example:

Code:
hUser32Dll = LoadLibraryEx("USER32.DLL", 0, 0)
  addr = GetProcAddress(hUser32Dll,"MessageBeep")
  Asm
    Push 0    
    Call [addr]
  End Asm
  FreeLibrary(hUser32Dll)

Kind regards from Germany

Peter
Hallo lillo!

Quote:0.13 has two built-in functions that let you deal with loading dynamic libs at runtime in a platform independent way: DYLIBLOAD and DYLIBSYMBOL.

That's very good! Did you create also a DYLIBFREE? Or must I call the FreeLibrary of Windows?

Bye bye

Peter
Don't use asm, please.. that makes you code un-portable and hard to maintain..

You can do this:

Code:
dim msgbeep as function( byval uType as uinteger ) as integer
    msgbeep = GetProcAddress(hUser32Dll,"MessageBeep")

    msgbeep( 0 )
Hallo V1ctor!

Quote:
Code:
dim msgbeep as function( byval uType as uinteger ) as integer
    msgbeep = GetProcAddress(hUser32Dll,"MessageBeep")

    msgbeep( 0 )

Thank you very much for your example! I don't know before that I can DIM ... AS FUNCTION ()

Thanks again!

Have a nice sunday!

Peter