N.S
N.S

Reputation: 1477

how to open a file in a directory in assembly

I need to open a text file in my C drive and my code is in drive D. Is there a way to read that file?

(I recently started assembly programming, so I don`t know if there are any extra information I should write here ._.)

I`m using windows 10 x 64 and nasm

but right now I'm trying to find a exe file data accesses using ollydbg and I know they are on another drive and searching for mov eax,3 didn't help :)

Upvotes: 1

Views: 3333

Answers (1)

Margaret Bloom
Margaret Bloom

Reputation: 44046

To get a file handle you should use CreateFile (OpenFile is deprecated).
Besides its name, it can OPEN_EXISTING files.
Windows supports absolute paths, the drive letter is part of the absolute path, so opening C:\path\to\file will always open a file in the C: drive regardless of your executable position.
Actually, Dos devices names can change while Volume names won't but that's far too overkill in this context.


Once you have a file handle, you can ReadFile from it or CreateFileMapping from it.
The former being easier.


To add dynamicity to your application, you can use FindFirstFile and similar to search for files.


When done, remember to 'CloseHandle`.

Upvotes: 3

Related Questions