Its never wise to overflow a buffer! You could be writing over data that could be important, e.g. OS stuff.thefool wrote:probably. but is that really a good thing here? i mean, sometimes it is used wisely, sometimes not
Using DLLs with WinAPI
Yes but where is the buffer, what points to it?Paul wrote:I'm not sure what you are finding so confusing?
We defined the max buffer size with "size.l=512"
The memory address of the buffer is returned in our other variable.
The API command we are calling does the rest of the work.
CurDir.l is a long with a value of 0
ptrDir.l is a long pointing to CurDir
i still don't see where the string is going?
Code: Select all
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)
Code: Select all
CurDir.l = 606915
peekL(@CurDir)
Code: Select all
CurDir.l = 606915
peekS(@CurDir)
Now i am home i can run these examples:
This produces a buffer overflow and crashes the process will have to be killed via the IDE. Because a string is being stuffed into a bufer only 4 bytes long.
this is the correct way:
This produces a buffer overflow and crashes the process will have to be killed via the IDE. Because a string is being stuffed into a bufer only 4 bytes long.
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
this is the correct way:
Code: Select all
module=LoadLibrary_("kernel32.dll")
If module
GetCurrentDirectory=GetProcAddress_(module,"GetCurrentDirectoryA")
size.l=512
CurDir.s= ""
ptrDir.l = @CurDir
!PUSH [v_ptrDir]
!PUSH [v_size]
!CALL [v_GetCurrentDirectory]
Debug PeekS(ptrDir)
FreeLibrary_(module)
EndIf
I posted 2 different versions.
Do what you want with it, doesn't really matter to me.
Do what you want with it, doesn't really matter to me.
Last edited by Paul on Thu Aug 11, 2005 7:47 pm, edited 2 times in total.
I think I know what confuses Kale and yep, I think he is right.Paul wrote:I posted 2 different versions, both of which work fine here.
You say it crashes with a buffer overflow but I cannot reproduce your problem. Both code do the same thing.
If you do not understand how it works, I'm sorry but there is nothing more I can help you with. I cannot explain it any better since I am a programmer, not a teacher.
You pass a pointer to a long instead of a pointer of a string, which can work but also can't.
Code: Select all
module=LoadLibrary_("kernel32.dll")
If module
GetCurrentDirectory=GetProcAddress_(module,"GetCurrentDirectoryA")
size.l=65000
CurDir.l= 0
ptrDir.l = @CurDir
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
!PUSH [v_ptrDir]
!PUSH [v_size]
!CALL [v_GetCurrentDirectory]
Debug PeekS(@CurDir)
Debug a
Debug b
Debug c
Debug d
Debug e
Debug f
FreeLibrary_(module)
EndIf
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
Re: Using DLLs with WinAPI
Code updated For 5.62
Code: Select all
hMod = GetModuleHandle_("kernel32.dll")
If hMod
GetCurrentDirectory = GetProcAddress_(hMod, UTF8("GetCurrentDirectoryW"))
size.l=512
CurDir.s= Space(#MAX_PATH)
ptrDir.l = @CurDir
!PUSH [v_ptrDir]
!PUSH [v_size]
!CALL [v_GetCurrentDirectory]
Debug PeekS(ptrDir)
CloseHandle_(hMod)
EndIf
Re: Using DLLs with WinAPI
If you use UTF8(), you also need to free the memory yourself (like Fred mentioned in another thread)!
Also, GetModuleHandle_() is freed with FreeLibrary_() and not with CloseHandle_()...
Also, GetModuleHandle_() is freed with FreeLibrary_() and not with CloseHandle_()...
Code: Select all
hMod = GetModuleHandle_("kernel32.dll")
If hMod
*utf8 = UTF8("GetCurrentDirectoryW")
GetCurrentDirectory = GetProcAddress_(hMod, *utf8)
path${#MAX_PATH}
!PUSH v_path$
!PUSH 260 ;#MAX_PATH
!CALL [v_GetCurrentDirectory]
Debug path$
FreeMemory(*utf8)
FreeLibrary_(hMod)
EndIf
Et cetera is my worst enemy