flat assembler
Message board for the users of flat assembler.

Index > Windows > Iczelion's Tutorial 12: Memory Management and File I/O

Author
Thread Post new topic Reply to topic
Slai



Joined: 11 Jan 2006
Posts: 40
Location: NY/Bulgaria
Slai 16 Jun 2006, 02:34
in this tutorial it says :
Quote:
1. Allocate a block of memory by calling GlobalAlloc. This function returns a handle to the requested memory block.
2. "Lock" the memory block by calling GlobalLock. This function accepts a handle to the memory block and returns a pointer to the memory block.
3. You can use the pointer to read or write memory.
4. "Unlock" the memory block by calling GlobalUnlock . This function invalidates the pointer to the memory block.
5. Free the memory block by calling GlobalFree. This function accepts the handle to the memory block.

The above method can be further simplified by using a flag in GlobalAlloc call, GMEM_FIXED. If you use this flag, the return value from Global/LocalAlloc will be the pointer to the allocated memory block, not the memory block handle. You don't have to call Global/LocalLock and you can pass the pointer to Global/LocalFree without calling Global/LocalUnlock first.
so I changed the code to this:
Code:
                invoke  GlobalAlloc,GMEM_MOVEABLE + GMEM_ZEROINIT + GMEM_FIXED,MEM_SIZE
;                mov     [hMem],eax
;                invoke  GlobalLock,eax
                mov     [pMem],eax
                invoke  ReadFile,[hFile],[pMem],MEM_SIZE-1,szReadWrite,NULL
                invoke  SendMessage,[hEdit],WM_SETTEXT,NULL,[pMem]
                invoke  CloseHandle,[hFile]
;                invoke  GlobalUnlock,[pMem]
                invoke  GlobalFree,[hMem]    
but with this code when I open a file it puts out only 3 strange symbols. What am I doing whrong ?
Also when I looked up GlobalAlloc on MSDN and it says : "Note The global functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions." so should I use them, or something else ?
Post 16 Jun 2006, 02:34
View user's profile Send private message Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 16 Jun 2006, 06:28
GMEM_MOVEABLE cannot be combined with GMEM_FIXED.
Post 16 Jun 2006, 06:28
View user's profile Send private message Reply with quote
Slai



Joined: 11 Jan 2006
Posts: 40
Location: NY/Bulgaria
Slai 16 Jun 2006, 14:08
Thanks, I knew I probably had some stupid mistake ..
Also I have to replace [hMem] in the last line with [pMem] , right ?

So is there a better way for allocating memory than this ?
Post 16 Jun 2006, 14:08
View user's profile Send private message Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM 17 Jun 2006, 01:21
Not really, though you can use HeapAlloc and HeapFree (look them up) in conjunction with GetProcessHeap.
Post 17 Jun 2006, 01:21
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.