flat assembler
Message board for the users of flat assembler.

Index > Windows > How to use memory allocated with HeapAlloc?

Author
Thread Post new topic Reply to topic
rohagymeg



Joined: 19 Aug 2011
Posts: 77
rohagymeg
Code:
invoke HeapAlloc, [hHeap], 0, [config_size]
mov [allocated_mem], eax    


If i'm right, [allocated_mem] holds the 32bit value returned by HeapAlloc, which is the starting address of the heap. But how do I assign a label to an address, whose value I don't know, because it will be stored in [allocated_mem] at runtime? I just want to be able to address my allocated heap to use it.

Either I'm missing something, or I made a really beginner mistake(which is very possible).
Post 08 Jan 2012, 22:14
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
Here's a code snippet from my project that I;m working on right now.

It is highly recommended to check if the returned pointer is not NULL otherwise your app will crash with possibly an Access violation

Code:
call      [GetProcessHeap]
      push      2028                         ; Unicode buffer size in my project
      push      HEAP_ZERO_MEMORY  ; zero out the buffer
      push      eax
      call      [HeapAlloc]

      or        eax,    eax
      jnz       __no_err

      invoke    MessageBox,0,'Error allocating memory','Windows Wins. Fatality !', MB_OK + MB_ICONSTOP
      push      -$1
      call      [ExitProcess]

__no_err:
      mov       [temp_buffer],     eax          ; move the      pointer  
    


After use, then you call HeapFree
Post 08 Jan 2012, 22:42
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Quote:

But how do I assign a label to an address, whose value I don't know
You can't, but to get something near to that:
Code:
virtual at ebx
someLabel:
.a rb 16
.b dd ?

moreData rb 512

SIZE = $ - $$
end virtual

invoke HeapAlloc, [hHeap], 0, SIZE
mov [allocated_mem], eax
mov ebx, eax

; access .b
mov eax, [someLabel.b] ; fasm assembles "mov eax, [ebx+16]"    
Remember however that you must have EBX set to a heap pointer containing the same data structure the virtual block has before accessing anything, otherwise it won't work.

Additionally, if you have a STRUCT defined, then you can just do the pointer arithmetic yourself too (e.g. "mov eax, [ebx+BITMAPINFOHEADER.biSize]")
Post 08 Jan 2012, 23:07
View user's profile Send private message Reply with quote
rohagymeg



Joined: 19 Aug 2011
Posts: 77
rohagymeg
I better start reading the whole FASM documentation. If I still don't get it, I'll tell. Thanks LocoDelAssembly for help.
Post 08 Jan 2012, 23:44
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
Put it on your iPhone / iPad / iPod.

That is what I did, Razz
Post 09 Jan 2012, 00:30
View user's profile Send private message Reply with quote
rohagymeg



Joined: 19 Aug 2011
Posts: 77
rohagymeg
*Runs into the closest istore and buys an ipad*
Post 09 Jan 2012, 05:39
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
^^Really ?
Post 09 Jan 2012, 06:05
View user's profile Send private message Reply with quote
rohagymeg



Joined: 19 Aug 2011
Posts: 77
rohagymeg
typedef wrote:
^^Really ?

True story bro:). If I had the money...
Post 09 Jan 2012, 14:27
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
Haha. Nice sense of humor
Post 09 Jan 2012, 17:23
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. Also on YouTube, Twitter.

Website powered by rwasa.