flat assembler
Message board for the users of flat assembler.
Index
> Windows > open/read file (how ?) |
Author |
|
Tyler 10 Aug 2010, 08:50
CreateFile - to open
ReadFile - to read Does that help? I get the feeling that I don't fully understand what you need. |
|||
10 Aug 2010, 08:50 |
|
ouadji 10 Aug 2010, 09:14
Quote: I don't fully understand what you need This file contains a list of keywords which I would like to access. For the moment I put this list in the data area, as a initialized variable, but I would like to put this list in a file, then read that file and put this list of keywords in memory. sorry for my bad english. |
|||
10 Aug 2010, 09:14 |
|
sinsi 10 Aug 2010, 09:26
The basic steps are usually
CreateFile GetFileSize GlobalAlloc ReadFile CloseHandle |
|||
10 Aug 2010, 09:26 |
|
ManOfSteel 10 Aug 2010, 09:35
ouadji wrote: load the contents of a file in memory. That's what the APIs mentioned by Tyler do. CreateFile can open an existing file and returns a handler. ReadFile uses this handler to read the file into a memory location pointed by lpBuffer. This could be a simple buffer inside your application or it could be allocated using GlobalAlloc. GlobalAlloc returns a handler. GlobalLock uses this handler and returns the pointer you need. Read the MSDN pages for these APIs and fill the parameters accordingly. And of course don't forget to clean up afterwards. |
|||
10 Aug 2010, 09:35 |
|
Tyler 10 Aug 2010, 09:50
ouadji wrote:
Just follow sinsi's list and it should work fine. MSDN(The site I linked to above.) is a good ref for WinAPI. |
|||
10 Aug 2010, 09:50 |
|
ouadji 10 Aug 2010, 10:07
Quote: Nah man, your English is good Smile ok i beginning to understand. Quote: It was confusing to me why an asm master like yourself,would be asking something I know the answer to yes indeed, api windows are a unknow world for me. to allocate memory, i use "virtualalloc", as TG (like TG ?) in Fasmw. ... last short question, how to insert "simple db" in a text file i.e. ... db 3h or db 17h how to convert this : db 'abcde',00h,17h,03h ... in text ? abcde ... ok ... but for 00h,17h,03h ??? |
|||
10 Aug 2010, 10:07 |
|
Tyler 10 Aug 2010, 11:17
VirtualAlloc is for allocating whole pages, overkill for most memory allocations.
To insert text, you would just allocate "size_of_file"+"size_of_str_to_insert" the move/shift all that is to the right of the insertion "size_of_str_to_insert" to the right. I suggest libc for moving/shifting the memory. memmove is made just for it. |
|||
10 Aug 2010, 11:17 |
|
vid 10 Aug 2010, 11:29
To write couple of bytes to file, use something like:
Code: bytes_writted dd ? string_to_write db "I hope I end up inside a file" size_of_string = $ - string_to_write ... push 0 ;overlapped - not needed push offset bytes_written push size_of_string push string_to_write push handle ;handle is dword returned (in EAX) by CreateFileA call [WriteFile] ;test if write succeeded test eax, eax jz error_on_write Quote: how to convert this : db 'abcde',00h,17h,03h ... in text ? You mean you want to convert values 00h, 17h, 03h to their textual representations? (eg. strings "0", "23", "3", etc.?). You need to write a routine for that, or use one that already exists. However, the most convenient way would be to use C API instead of Win32API, where you have the beautiful formatted I/O functions (eg. fprintf() in your case). |
|||
10 Aug 2010, 11:29 |
|
sinsi 10 Aug 2010, 11:47
ManOfSteel, using GlobalAlloc with GMEM_FIXED means you don't need GlobalLock to get a pointer, this is returned by the GlobalAlloc call.
|
|||
10 Aug 2010, 11:47 |
|
Picnic 10 Aug 2010, 12:20
ouadji wrote: How to open a text file and copy its contents into memory ? Hi ouadji, Here is something very basic just to get you started. Code: FileTitle db 'somefile.txt',0 hFile dd ? nSize dd ? lpBytesRead dd ? lpBuffer rb 8192 invoke CreateFile, FileTitle, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 ; TestApiError mov [hFile], eax invoke GetFileSize, [hFile], 0 ; TestApiError mov [nSize], eax invoke ReadFile, [hFile], lpBuffer, [nSize], lpBytesRead, 0 ; TestApiError invoke CloseHandle, [hFile] A lightweight solution using fasm file directive. Code: myfile: file 'somefile.txt' myfile.size = $-myfile |
|||
10 Aug 2010, 12:20 |
|
ouadji 10 Aug 2010, 13:20
thank you Picnic, vid, Tyler, sinsi, ManOfSteel I have enough information to get by! again, thank you all. |
|||
10 Aug 2010, 13:20 |
|
roboman 10 Aug 2010, 14:05
ouadji wrote:
Well if you only need to make a couple of those files, you can just make a text file of db statements and run it through fasm. The other one I've done is to use a hex editor. |
|||
10 Aug 2010, 14:05 |
|
ouadji 10 Aug 2010, 15:02
Quote: you can just make a text file of db statements and run it through fasm. it's a perfect solution. I just tried, the result is exactly what I need.. thank you roboman |
|||
10 Aug 2010, 15:02 |
|
LocoDelAssembly 10 Aug 2010, 15:56
|
|||
10 Aug 2010, 15:56 |
|
ouadji 10 Aug 2010, 16:55
This article about memory allocation is very interesting. (thank you LocoDelAssembly)
Personally, I like "VirtualAlloc" (from ring3) ... yes, VirtualAlloc use page granularity but I usually need large allocation, then this is not a problem. I also like a memory allocation with page alignment. (and from ring0, i use "ExAllocatePoolWithTag") |
|||
10 Aug 2010, 16:55 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.