PDA

View Full Version : Declare Api



peter
09-10-2008, 00:59
Hello Eros Olmi,

i need an explanation !
why will sndplaysound be found, but setpixel will not be found ?
with many other API routines i will get the same problem.

take a look here in this example:

Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long

Dim Sec as Long
Sec = 1000

SetPixel(0,100,100,&hFF0000)
sndPlaySound("c:/Windows/Media/notify.wav",1)
Sleep(Sec)
MsgBox(0,"did you hear the tone ? ")

i get the following message: Unknown Keyword

ErosOlmi
09-10-2008, 08:11
Missing ".dll" in LIB indication:

You should write:
DECLARE FUNCTION SetPixel LIB "GDI32.DLL" (BYVAL hdc AS DWORD, BYVAL x AS LONG, BYVAL y AS LONG, BYVAL crColor AS DWORD) AS DWORD

Ciao
Eros

peter
09-10-2008, 10:11
Hi Eros Olmi,

nothing happens ! No change !

DECLARE FUNCTION SetPixel LIB "GDI32.DLL" (BYVAL hdc AS DWORD, BYVAL x AS LONG, BYVAL y AS LONG, BYVAL crColor AS DWORD) AS DWORD

Has no function here !

'Dword' and 'Long ' are the same, 32 Bit !
Thank you for your help .

ErosOlmi
09-10-2008, 10:47
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Declare Function SetPixel Lib "gdi32.dll" alias "SetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long

Dim lSec as Long
lSec = 1000

SetPixel(0,100,100,&hFF0000)
sndPlaySound("c:/Windows/Media/notify.wav",1)
Sleep(lSec)
MsgBox(0,"did you hear the tone ? ")

Hi peter. I think I've found the problem: ALIAS was not specified. Try above code.

Problem is that external API names are case sensitive and without the ALIAS indication SETPIXEL (all caps) is used instead. So be sure to always indicate ALIAS.

Ciao
Eros

peter
09-10-2008, 15:43
Hi Eros,

it seems you found the problem ! Well done !
I am glad over it .

Now, i will try to write a game with your thinBasic.
I think that i can do it !

When problems appear, i know where i can found you .
Thank you for your great help.

best wishes .....
Peter

ISAWHIM
13-11-2008, 20:05
This is just to extend upon the answer provided previously.

The name before the ALIAS, is just what YOU call the function. You can call it "BOB" if you want.

ALIAS is the name of the DLL entry point, as windows needs to look for it. ("BOB" here is "SetPixel", in the DLL.)

".DLL" should not be needed on the DLL API CALL. (There is only one file extension that DLL API CALL is made to interact with.)

Additional notes: (Research for more details if desired. This is just a quick set of info, not code gospel. Code is abused all the time, and there are exceptions to every rule.)

There are two TYPES of LIB files, and API is not designed to talk to them. A DLL may use Symbols.LIB and Static.LIB, usually this is not a user-interface. Though, I do believe you can "Trick" API to load a .LIB entry-point... But it is not designed for "Static Library Files", only "Dynamic Linked Libraries".

"__imp__" I believe, is prepended to the symbols library ".LIB"
("__imp__SetPixel") This gets the import symbol. (Some LIB's are named ".a" to note them as symbols LIBs.)

"_" and "@##" is attached to the other ".LIB" which actually contains the static calls. (But this is not entirely true for all entry points. Which makes it trial and error, when talking to a LIB file.)
("_SetPixel@48") This number is not static. It changes or may change from version to version. (I believe this is the number of bytes the call expects in return, or it may be the ordinal internal number location alias?

If a LIB file has an API entry routine... it is a DLL with the wrong name. (MSVC DLL's or Win32 DLL's all use "STDCALL" which is "__stdcall" for a call routine, and may also have "__cdecl" for C/C++, but it does not always have it.)