flat assembler
Message board for the users of flat assembler.

Index > Windows > GlobalReAlloc - is not work

Author
Thread Post new topic Reply to topic
Everhest



Joined: 26 Jun 2008
Posts: 83
Location: Russia
Everhest 23 Nov 2008, 10:16
Hi, I do not understand in than my mistake.

Code:
format PE GUI 4.0
entry start

include '../../include/win32a.inc'

ERR_AUTO        = 0
soFromBeginning = 0

section '.data' data readable writable

        error_memory    db 'Íå âûïîëíåí çàïðîñ íà ïîëó÷åíèå\ðàñïðåäåëåíèå ãëîáàëüíîé ïàìÿòè äëÿ ïðèëîæåíèÿ.',0
        str1            db 'String One',0
        str2            db 'Str 2, this text proected to global memory block as machin memory computer!',0
        hglb1           dd ?
                        dd ?
        hglb2           dd ?
                        dd ?

section '.code' code readable executable

  start:
        invoke  lstrlen, str1
        inc     eax
        stdcall MemoryInit, hglb1, eax
        mov     [hglb1+4], eax
        invoke  lstrcpy, eax, str1
        invoke  MessageBox,0,[hglb1+4],0,MB_ICONINFORMATION
     .@f:
        invoke  lstrlen, str2
        inc     eax
        stdcall MemoryReSize, hglb1, eax
        mov     [hglb1+4], eax
        invoke  lstrcpy, eax, str2
        invoke  MessageBox,0,[hglb1+4],0,MB_ICONINFORMATION

  exit:
        stdcall MemoryFree, hglb1, [hglb1+4]
        invoke  ExitProcess,0

proc MemoryInit HGLOBAL, size
        invoke  GlobalAlloc, GMEM_ZEROINIT, [size]
        mov     [HGLOBAL], eax
        cmp     eax, 0
        je      mem_error
        invoke  GlobalLock, eax   ; Âîçðàùàåò àäðåñ ïàìÿòè
        cmp     eax, 0
        je      mem_error
        ret
endp

proc MemoryReSize HGLOBAL, size ; - not work
        invoke  GlobalReAlloc, [HGLOBAL], [size], GMEM_ZEROINIT
        xor     ecx, ecx
        cmp     eax, ecx
        je      mem_error
        mov     [HGLOBAL], eax
        ret
endp

proc MemoryFree HGLOBAL
        invoke  GlobalUnlock,[HGLOBAL]
        invoke  GlobalFree,[HGLOBAL]
        ret
endp                                          

include 'winapi.inc'                       

Please help me...

_________________
Forgive for my bad english, I from russia...
Post 23 Nov 2008, 10:16
View user's profile Send private message ICQ Number Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 23 Nov 2008, 11:07
Everhest,
Code:
…
        stdcall MemoryInit, hglb1, eax
…
proc MemoryInit HGLOBAL, size
        invoke  GlobalAlloc, GMEM_ZEROINIT, [size]
        mov     [HGLOBAL], eax; <- look here!
…
proc MemoryReSize HGLOBAL, size ; - not work
        invoke  GlobalReAlloc, [HGLOBAL], [size], GMEM_ZEROINIT; <- and here
Isn't HGLOBAL a pointer to HGLOBAL?
Post 23 Nov 2008, 11:07
View user's profile Send private message Reply with quote
Everhest



Joined: 26 Jun 2008
Posts: 83
Location: Russia
Everhest 23 Nov 2008, 12:53
But as it is correct this do that not to break syntax of the language?
Post 23 Nov 2008, 12:53
View user's profile Send private message ICQ Number Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 23 Nov 2008, 14:22
Everhest,

It's not about language syntax. IA-32 doesn't allow double indirection. You passed address of hglb1 structure to MemoryInit as HGLOBAL parameter on stack, then successfully overwrite that parameter (address on stack, not contents of the memory it points to) with pointer to memory allocated in global heap (GMEM_FIXED==0, so GlobalAlloc() returns pointer, not handle; you should use GMEM_MOVEABLE additionally (or GHND — it's defined as GMEM_MOVEABLE | GMEM_ZEROINIT) to get handle).

Something like
Code:
        invoke  GlobalAlloc, GMEM_ZEROINIT, [size]
        mov     ecx, [HGLOBAL]; retrieve pointer parameter (ecx==hglb1)
        mov     [ecx], eax; save heap pointer    
will do the trick.
Post 23 Nov 2008, 14:22
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.