1

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 :)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
N.S
  • 1,363
  • 15
  • 18
  • 2
    from the "C" and "D" drive I guess you are on one of Microsoft's operating systems. All of them support full-path for file operations, i.e. you can open "C:\somedir\somefile.ext" through system service calls or support libraries (like libc). If you are programming in the obsolete 16b DOS environment, keep in mind the file names are "8.3" short, and the long filenames are supported as extension, so your DOS code should instead use the shortened ones (use "dir" command in command line with some switches to see yourself, how the shortened name looks, it's like "PROGRA~1" for "Program Files",...) – Ped7g Aug 14 '18 at 10:00
  • About extra information.. it's not extra, it's base/mandatory to get meaningful answer: What OS/platform is your target, what assembler you are using, and on x86 there's even subtlety in the way which CPU you take as minimal target, in which mode you operate, etc.. usually showing [MCVE] with instructions how you compile + run + debug it and what are your results is helpful. Your question at this moment is worth of downvoting because it's too broad/unclear. BTW it sounds like you may profit a lot from reading about file system used on your system, your question is not related to "assembly". – Ped7g Aug 14 '18 at 10:02
  • Also in DOS the executable has "working directory", where it was executed from... so if you launch your executable from the directory where your file resides, you can use just "filename.ext" to open it, or you can use OS services to change your working directory to "C:\somedir" first and then open file with the local "filename.ext" only without full path (full paths hardcoded in source are cumbersome for real applications, because then every user must have the file at the same absolute path). AFAIK windows supports "working directory" concept too, but I haven't used win for decade+. – Ped7g Aug 14 '18 at 10:08
  • The Linux `open` system call only works on files that Linux can see. If you're running Linux in a VM, you need to make the files you want to access available inside the VM, so this is a VirtualBox config question. (Or it was until you completely changed it without fixing the tags by saying you're writing asm for Windows, not Ubuntu in a VM.) – Peter Cordes Aug 14 '18 at 10:30
  • sorry I use ubuntu to connect to net not programming. sorry. i edited my post – N.S Aug 14 '18 at 10:33
  • 1
    It looks like you should call the `OpenFile` function in the Win32 API. [Windows System Programming OpenFile function](https://stackoverflow.com/q/19915251) has an example in C. Compile it to see the ASM, or just translate it to asm yourself. The function takes a path which can include a drive letter. – Peter Cordes Aug 14 '18 at 10:34
  • thanks. I try to do what you said. – N.S Aug 14 '18 at 10:36

1 Answers1

3

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`.

Margaret Bloom
  • 41,768
  • 5
  • 78
  • 124