Using DLLs with WinAPI
-
- Enthusiast
- Posts: 731
- Joined: Wed Apr 21, 2004 7:12 pm
Using DLLs with WinAPI
Hey,
I'm wondering if there is a way to load a .dll and use its functions in PureBasic soley through the use of the WinAPI?
Thanks for your help,
Killswitch
I'm wondering if there is a way to load a .dll and use its functions in PureBasic soley through the use of the WinAPI?
Thanks for your help,
Killswitch
~I see one problem with your reasoning: the fact is thats not a chicken~
Have a look at this thread (good sample by Jack):
viewtopic.php?t=14787
and this one from MSDN
http://msdn.microsoft.com/library/defau ... erence.asp
viewtopic.php?t=14787
and this one from MSDN
http://msdn.microsoft.com/library/defau ... erence.asp
Athlon64 3800+ · 1 GB RAM · Radeon X800 XL · Win XP Prof/SP1+IE6.0/Firefox · PB 3.94/4.0
Intel Centrino 1.4 MHz · 1.5 GB RAM · Radeon 9000 Mobility · Win XP Prof/SP2+IE6.0/Firefox · PB 3.94/4.0
Intel Centrino 1.4 MHz · 1.5 GB RAM · Radeon 9000 Mobility · Win XP Prof/SP2+IE6.0/Firefox · PB 3.94/4.0
-
- Enthusiast
- Posts: 731
- Joined: Wed Apr 21, 2004 7:12 pm
-
- Enthusiast
- Posts: 731
- Joined: Wed Apr 21, 2004 7:12 pm
Here is an example calling GetCurrentDirectory which is found in the Kernel32.DLL ...
First using PB's built in ability to access API:
Next using PB commands to access command in DLL:
Finally using strickly Windows API:
Hope this helps
First using PB's built in ability to access API:
Code: Select all
CurDir$=Space(512)
GetCurrentDirectory_(512,@CurDir$)
Debug CurDir$
Next using PB commands to access command in DLL:
Code: Select all
If OpenLibrary(0,"kernel32.dll")
CurDir$=Space(512)
CallFunction(0,"GetCurrentDirectoryA",512,@CurDir$)
Debug CurDir$
CloseLibrary(0)
EndIf
Finally using strickly Windows API:
Code: Select all
module=LoadLibrary_("kernel32.dll")
If module
GetCurrentDirectory=GetProcAddress_(module,"GetCurrentDirectoryA")
size.l=512
CurDir.l= 0
ptrDir.l = @CurDir
!PUSH [v_ptrDir]
!PUSH [v_size]
!CALL [v_GetCurrentDirectory]
Debug PeekS(@CurDir)
FreeLibrary_(module)
EndIf
Hope this helps
-
- Enthusiast
- Posts: 731
- Joined: Wed Apr 21, 2004 7:12 pm
i might be going mad here but browsing that code i noticed something that didn't quite gel. doesn't this line:Paul wrote:Code: Select all
module=LoadLibrary_("kernel32.dll") If module GetCurrentDirectory=GetProcAddress_(module,"GetCurrentDirectoryA") size.l=512 CurDir.l= 0 ptrDir.l = @CurDir !PUSH [v_ptrDir] !PUSH [v_size] !CALL [v_GetCurrentDirectory] Debug PeekS(@CurDir) FreeLibrary_(module) EndIf
Code: Select all
CurDir.l= 0
Code: Select all
CurDir.s= ""
Last edited by Kale on Thu Aug 11, 2005 2:48 pm, edited 1 time in total.
LOL ... If it had to be then you would get an errorKale wrote: i might be going mad here but browsing that code i noticed something that didn't quite gel. doesn't this line:have to be:Code: Select all
CurDir.l= 0
Code: Select all
CurDir.s= ""
I happened to use memory addresses in the last example, which is why I used PeekS() to display the string which was located at the memory address in CurDir.l
It was more to show the use of pointers. Since PB is so flexable with the way it handles pointers to strings, I could have just as easily done this:
Code: Select all
module=LoadLibrary_("kernel32.dll")
If module
GetCurrentDirectory=GetProcAddress_(module,"GetCurrentDirectoryA")
size.l=512
CurDir.s= ""
!PUSH [v_CurDir]
!PUSH [v_size]
!CALL [v_GetCurrentDirectory]
Debug CurDir
FreeLibrary_(module)
EndIf
no i'm not trying to optimise anything i just thought i spotted an error (or feature). The second example you've just posted make more sense because you are using a string to store a string and then referencing it using the pointer contained within the string var but in the first example there is no string created to store the directory path. I'm just not sure how the first example works.
does this help:
pauls code just throws bytes against the memory adress in ptrDir.l.
basically its the same. look at it as in assembler, a string is basically just an array of characters in ascii value's.
Code: Select all
blabla.l=1635345768
Debug PeekS(@blabla)
basically its the same. look at it as in assembler, a string is basically just an array of characters in ascii value's.
Not really.thefool wrote:does this help:
pauls code just throws bytes against the memory adress in ptrDir.l.Code: Select all
blabla.l=1635345768 Debug PeekS(@blabla)
basically its the same. look at it as in assembler, a string is basically just an array of characters in ascii value's.
Where is the reserving of or allocating of that memory space done?
As we agreed elsewhere, I can be quite stupid sometimes ....
@}--`--,-- A rose by any other name ..
heheDare2 wrote: Where is the reserving of or allocating of that memory space done?
As we agreed elsewhere, I can be quite stupid sometimes ....
well, i guess it is dynamic..
like:
Code: Select all
variableone=0
variabletwo=292991
variableone=variabletwo
My guess is, that it is not needed for longs and that stuff, only for strings!
(only a guess, but sounds correct in my ears)