flat assembler
Message board for the users of flat assembler.

Index > Windows > open/read file (how ?)

Author
Thread Post new topic Reply to topic
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 10 Aug 2010, 07:25

I am a king with asm (yes, it's true Razz Wink ) ,
but I have no knowledge about windows api. Embarassed

How to open a text file and copy its contents into memory ?

I have to open the file with the path ... ( Confused ???) ... a little help would be welcome!

thank you all.
Code:
i.e.
toto.txt = "abcde..."
virtualalloc ...
toto.txt ---> in memory
[eax] = "abcde .. " ; eax = result from virtualalloc
    


_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 10 Aug 2010, 07:25
View user's profile Send private message Send e-mail Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
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.
Post 10 Aug 2010, 08:50
View user's profile Send private message Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 10 Aug 2010, 09:14
Quote:
I don't fully understand what you need
load the contents of a file in memory.

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.

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 10 Aug 2010, 09:14
View user's profile Send private message Send e-mail Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 794
Location: Adelaide
sinsi 10 Aug 2010, 09:26
The basic steps are usually
CreateFile
GetFileSize
GlobalAlloc
ReadFile
CloseHandle
Post 10 Aug 2010, 09:26
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
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.
Post 10 Aug 2010, 09:35
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 10 Aug 2010, 09:50
ouadji wrote:
Quote:
I don't fully understand what you need
load the contents of a file in memory.

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.
Nah man, your English is good Smile, I've heard it's pretty hard to learn for non-native speakers. I just thought you were asking for something more complex than I know. It was confusing to me why an asm master like yourself(Razz Wink) would be asking something I know the answer to. Very Happy

Just follow sinsi's list and it should work fine. MSDN(The site I linked to above.) is a good ref for WinAPI.
Post 10 Aug 2010, 09:50
View user's profile Send private message Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 10 Aug 2010, 10:07
Quote:
Nah man, your English is good Smile
thank you, i do my best, it's not easy !

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
Very Happy Very Happy Embarassed
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 ???

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 10 Aug 2010, 10:07
View user's profile Send private message Send e-mail Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
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.
Post 10 Aug 2010, 11:17
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
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).
Post 10 Aug 2010, 11:29
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 794
Location: Adelaide
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.
Post 10 Aug 2010, 11:47
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1403
Location: Piraeus, Greece
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. Smile
Code:
myfile:
        file 'somefile.txt'
        myfile.size = $-myfile  
    
Post 10 Aug 2010, 12:20
View user's profile Send private message Visit poster's website Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 10 Aug 2010, 13:20

thank you Picnic, vid, Tyler, sinsi, ManOfSteel Razz

I have enough information to get by!

again, thank you all.

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 10 Aug 2010, 13:20
View user's profile Send private message Send e-mail Reply with quote
roboman



Joined: 03 Dec 2006
Posts: 122
Location: USA
roboman 10 Aug 2010, 14:05
ouadji wrote:

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 ???


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.
Post 10 Aug 2010, 14:05
View user's profile Send private message Visit poster's website Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 10 Aug 2010, 15:02
Quote:
you can just make a text file of db statements and run it through fasm.
I had not thought of !!!! Rolling Eyes
it's a perfect solution. I just tried, the result is exactly what I need.. Razz Razz
thank you roboman

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 10 Aug 2010, 15:02
View user's profile Send private message Send e-mail Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 10 Aug 2010, 15:56
Post 10 Aug 2010, 15:56
View user's profile Send private message Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
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")

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 10 Aug 2010, 16:55
View user's profile Send private message Send e-mail 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.