I have been working on a fantasy console type project. I decided to zero a flash drive I had laying around and use it as the ROM input to the fantasy console. While trying to write code to read bytes directly from the drive (rather than from a file on the drive) I have found a peculiar bug.
Trying to open the drive via CreateFile and ReadFile with the file path \\.\\PhysicalDrive1 results in the error ""\\.\\PhysicalDrive1" is not a core dump: file format not recognized"
I have tried changing input options and I have tried alternate file paths, but anything I try results in the drive either not being found or the error shown above. Information on this issue is very scarse from my research, and what little info I have found is specific to Linux, and involves very different situations.
Here is my code (not the cleanest but it should work):
#include <windows.h>
#include <fileapi.h>
#include <stdio.h>
#include <stdint.h>
int main(int argc, char *argv[]) {
printf("Opening %s\n", argv[1]);
HANDLE romf = CreateFile((LPCSTR)argv[1],
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (romf == INVALID_HANDLE_VALUE) {
printf("File %s not found\n", argv[1]);
return -1;
}
uint8_t rom[4] = { 0 };
LPDWORD read = 0;
int res = ReadFile(romf, rom, 4, read, NULL);
if (res == 0) {
printf("Error reading file (%d)\n", argv[1], GetLastError());
CloseHandle(romf);
return -1;
}
printf("Read $%x bytes\n", read);
printf("%02x %02x %02x %02x\n", rom[0], rom[1], rom[2], rom[3]);
CloseHandle(romf);
return 0;
}
I have used gdb to debug the code and to get the error message.
Nothing in the code you have shown produces the error message that you have claimed. That error message is a debugger message, not a compiler error, or a runtime system error. This issue has nothing to do with the code you have shown, and everything to do with how you are using the debugger.
Per the gdb documentation:
In comments, you state that you are running
gdb ./bin/clt \\.\\PhysicalDrive1, which means you are calling gdb with\\.\\PhysicalDrive1in thecoreparameter, but that path does not point to a valid core dump file, hence the error. You are expecting gdb to pass\\.\\PhysicalDrive1as a command-line parameter to your program, but that is not what it is actually doing. To do that, use the--argsparameter (see How do I run a program with commandline arguments using GDB within a Bash script?), eg:gdb --args ./bin/clt \\.\PhysicalDrive1That said, there are a number of bugs in your code.
you are not checking if
argc > 1is true before accessingargv[1].the
LPCSTRcast in thelpFileNameparameter ofCreateFile()is not needed and should be removed.argv[1]is achar*and you are casting it toconst char*(whichCreateFile()wants, if you are compiling a MBCS build and not a UNICODE build). The compiler can do that cast implicitly for you.you are passing a
NULLpointer to thelpNumberOfBytesReadparameter ofReadFile(). Per the documentation:You are not performing an asynchronous read, so you must provide a pointer to an actual
DWORDvariable instead:your error message when
ReadFile()fails is being formatted incorrectly. You forgot to include a%splaceholder forargv[1], so you are passingargv[1](achar*) to the%dplaceholder, which expects anint, and you are not passing the value ofGetLastError()to any placeholder at all. You need to fix your format string, eg: