The MASM Forum

Projects => Rarely Used Projects => GoAsm => Topic started by: shankle on August 28, 2014, 08:00:24 AM

Title: Heap APIs
Post by: shankle on August 28, 2014, 08:00:24 AM

                             8-27-2014
                     
My 1st time using the Heap Marcos
Heapcreate seems to work but HeapAlloc does not even show either
error message below.
I am trying to create a growing heap.
Suggestion would be appreciated

HeapH   dd     0

    invoke HeapCreate, HEAP_GENERATE_EXCEPTIONS,10000,0
    mov [HeapH],eax

; test begin code   
;tmsg14    db  'HeapCreate seems to work',0
;tmsg15    db  'HeapCreate failed',0
   cmp eax,0h  ;if NuLL, HeapAlloc failed
    jne >>.tout
   invoke MessageBox, [hWnd],addr tmsg15,addr tmsg15,MB_OK ;true
   jmp >>.tout2
.tout
   invoke MessageBox, [hWnd],addr tmsg14,addr tmsg14,MB_OK ;true
.tout2 
; test end codecode

    invoke HeapAlloc, [HeapH],HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY,10000

; test begin code   
;tmsg10    db  'HeapAlloc seems to work',0
;tmsg11    db  'HeapAlloc failed',0
   cmp eax,0h  ;if NuLL, HeapAlloc failed
    jne >>.tout3
   invoke MessageBox, [hWnd],addr tmsg11,addr tmsg11,MB_OK ;true
   jmp >>.tout4
.tout3
   invoke MessageBox, [hWnd],addr tmsg10,addr tmsg10,MB_OK ;true
.tout4 ; test end code
Title: Re: Heap APIs
Post by: dedndave on August 28, 2014, 09:19:12 AM
there should be no need to create a heap
i generally use GetProcessHeap to get the system-created "default process" heap

if you successfully allocate a heap block, you should free it when done, even for a test piece
INVOKE  HeapFree,hHeap,lpBlock
lpBlock being the address returned by HeapAlloc

because you have specified HEAP_GENERATE_EXCEPTIONS, there is no need to test for success
if it is successful, there will be a block pointer in EAX
otherwise, it will generate an exception, and the program will terminate (possibly opening the debugger)
Title: Re: Heap APIs
Post by: dedndave on August 28, 2014, 09:24:39 AM
here is a piece using MASM syntax

    .DATA?

hHeap   HANDLE ?
lpBlock LPVOID ?

    .CODE

Start:
    INVOKE  GetProcessHeap
    mov     hHeap,eax

    INVOKE  HeapAlloc,hHeap,HEAP_GENERATE_EXCEPTIONS or HEAP_ZERO_MEMORY,10000
    mov     lpBlock,eax

    INVOKE  HeapFree,hHeap,NULL,lpBlock
Title: Re: Heap APIs
Post by: Yuri on August 28, 2014, 01:05:41 PM
Quote from: shankle on August 28, 2014, 08:00:24 AM
Heapcreate seems to work but HeapAlloc does not even show either
error message below
Both work for me in your code. I see tmsg10.
Title: Re: Heap APIs
Post by: shankle on August 29, 2014, 12:49:27 AM
Thanks guys for responding.
To Yuri:
I checked my code again and recompiled the program.
Same results. HeapAlloc shows nothing.
Then the dreaded C000005 error.............
This is a 64-bit program I am trying to convert from MASM32.
Title: Re: Heap APIs
Post by: qWord on August 29, 2014, 12:54:44 AM
Handles are pointer types and these are 64 bit width for Windows x86-64.
Title: Re: Heap APIs
Post by: shankle on August 29, 2014, 01:33:28 AM
Changing HeapH to "dq" fixed the problem but opened up another can of worms
in that it will involve several hundred changes for "PtrMem and ecx".
I will make the changes and hope for the best.
Title: Re: Heap APIs
Post by: dedndave on August 29, 2014, 03:14:40 AM
i didn't think about 64-bit - i was thinking 32-bit   :P
the heap handle AND the pointer returned by HeapAlloc are both 64-bits wide