Author Topic: Heap APIs  (Read 5532 times)

shankle

  • Member
  • ****
  • Posts: 868
Heap APIs
« 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
 

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: Heap APIs
« Reply #1 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
Code: [Select]
INVOKE  HeapFree,hHeap,lpBlocklpBlock 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)

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: Heap APIs
« Reply #2 on: August 28, 2014, 09:24:39 AM »
here is a piece using MASM syntax

Code: [Select]
    .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

Yuri

  • Member
  • **
  • Posts: 179
Re: Heap APIs
« Reply #3 on: August 28, 2014, 01:05:41 PM »
Heapcreate seems to work but HeapAlloc does not even show either
 error message below
Both work for me in your code. I see tmsg10.

shankle

  • Member
  • ****
  • Posts: 868
Re: Heap APIs
« Reply #4 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.

qWord

  • Member
  • *****
  • Posts: 1475
  • The base type of a type is the type itself
    • SmplMath macros
Re: Heap APIs
« Reply #5 on: August 29, 2014, 12:54:44 AM »
Handles are pointer types and these are 64 bit width for Windows x86-64.
MREAL macros - when you need floating point arithmetic while assembling!

shankle

  • Member
  • ****
  • Posts: 868
Re: Heap APIs
« Reply #6 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.

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: Heap APIs
« Reply #7 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