flat assembler
Message board for the users of flat assembler.

Index > Windows > HeapAlloc problems

Author
Thread Post new topic Reply to topic
Killswitch



Joined: 21 Jan 2006
Posts: 20
Killswitch
I've been trying to use HeapCreate/Heap alloc to dyncamically allocate memory to store strings. Anyway I've been doing this:

Code:
Format PE GUI 4.0
Entry Start
Include 'C:\Program Files\FASM\Include\win32a.inc'

section '.text' code readable executable 

  Start:
      Push 10240
      Push 1024
      Push HEAP_GENERATE_EXCEPTIONS
      Call [HeapCreate]
      mov dword [hHeap],eax
      cmp dword [hHeap],0

      push 10
      push HEAP_GENERATE_EXCEPTIONS
      push dword [hHeap]
      call [HeapAlloc]
      mov dword [va],eax

      Push s1
      Push sFormat
      Push Buffer
      Call [wsprintf]

      Push Buffer
      Push sFormat
      Push va
      Call [wsprintf]

      push 0
      push va
      push va
      push 0
      Call [MessageBox]

    ;End
      Push 0
      call [ExitProcess]

section '.data' Data readable writeable
  hHeap: dd 0
  sFormat: db '%s',0
  Buffer: rb 1024
  va: dd ?
  s0: db "error",0
  s1: db "0123456789abc",0


section '.idata' import Data readable writeable

  library kernel32, 'kernel32.dll', \
    user32, 'user32.dll'

  import kernel32, \
    ExitProcess,'ExitProcess', \
    HeapCreate,'HeapCreate',\
    HeapAlloc,'HeapAlloc'
  import user32, \
    MessageBox,'MessageBoxA',\
    wsprintf, 'wsprintfA'
        
    


Although I've declared va to hold 10 bytes of data it doesn't - it holds more. What am I doing wrong?
Post 30 Apr 2006, 14:15
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi
this is how i would do it, but you cant limit the heap for 10bytes Sad
Code:
format PE GUI 4.0
entry start
include '%fasminc%\win32a.inc'

section '.text' code readable executable 
start:
;heapcreate
      push       0
      push       0
      push       0
      call       [HeapCreate]
      mov        [hHeap], eax
;heapalloc 
      push       10
      push       0
      push       [hHeap]
      call       [HeapAlloc]
      mov        [va],eax


      push s1
      push sFormat
      push Buffer
      call [wsprintf]

      push Buffer
      push sFormat
      push va
      call [wsprintf]

      push 0
      push va
      push va
      push 0
      call [MessageBox]

;End
      push 0
      call [ExitProcess]

section '.data' Data readable writeable
  hHeap dd 0
  sFormat db '%s',0
  Buffer rb 1024
  va dd ?
  s0 db "error",0
  s1 db "0123456789abc",0

data import

  library kernel32, 'kernel32.dll', \
    user32, 'user32.dll'

  import kernel32, \
    ExitProcess,'ExitProcess', \
    HeapCreate,'HeapCreate',\
    HeapAlloc,'HeapAlloc'
  import user32, \
    MessageBox,'MessageBoxA',\
    wsprintf, 'wsprintfA'
        
end data    


Quote:

dwMaximumSize
[in] The maximum size of the heap, in bytes. The HeapCreate function rounds dwMaximumSize up to the next page boundary, and then reserves a block of that size in the process's virtual address space for the heap.

...
If dwMaximumSize is 0, the heap is growable. The heap's size is limited only by the available memory. Requests to allocate blocks larger than 0x7FFF8 bytes do not automatically fail. The system calls the VirtualAlloc function to obtain the memory that is needed for large blocks. Applications that need to allocate large memory blocks should set dwMaximumSize to 0.


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/heapcreate.asp
Post 30 Apr 2006, 14:49
View user's profile Send private message MSN Messenger Reply with quote
Killswitch



Joined: 21 Jan 2006
Posts: 20
Killswitch
Thanks, but I've just tested your code, and it's still got the same problem - va contains '0123456789abc' which exceeds the 10 bytes I allocated to va.
Post 30 Apr 2006, 16:32
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi
Killswitch wrote:
Thanks, but I've just tested your code, and it's still got the same problem - va contains '0123456789abc' which exceeds the 10 bytes I allocated to va.



so you didnt bother to read what i quoted from msdn, it rounds it to next page boundary, no matter what you do...

_________________
When We Ride On Our Enemies
support reverse smileys |:
Post 30 Apr 2006, 16:48
View user's profile Send private message MSN Messenger Reply with quote
Killswitch



Joined: 21 Jan 2006
Posts: 20
Killswitch
Sorry - thanks a lot for your help! Smile
Post 30 Apr 2006, 17:05
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.