&quot。\\。不是核心转储:未识别的文件格式
我一直在研究幻想控制台类型项目。我决定将闪光灯驱动器归零,然后将其用作幻想控制台的ROM输入。在尝试写代码直接从驱动器读取字节时(而不是从驱动器上的文件中),我发现了一个特殊的错误。
尝试通过createFile
和readfile
使用文件路径\\。 。
如上所示。关于这个问题的信息是我的研究非常稀缺,我发现的信息很少,它特定于Linux,并且涉及非常不同的情况。
这是我的代码(不是最清洁的代码,但应该工作):
#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;
}
我使用gdb
来调试代码并获取错误消息。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您显示的代码中没有任何内容会产生您声称的错误消息。该错误消息是调试器消息,而不是编译器错误或运行时系统错误。这个问题与您显示的代码无关,以及与您使用调试器的方式有关。
per gdb文档:
在注释中,您说您正在运行
gdb ./bin/clt \\。\\。 /code>,这意味着您正在使用
\\。\\ yralthdrive1
在core> core
参数中调用GDB,但是该路径没有指向有效的核心转储文件,因此错误。您期望GDB将\\。\\物理Drive1
作为您程序的命令行参数,但这不是实际在做的。为此,请使用- args
参数(请参见如何使用commandline参数运行一个使用GDB的程序,一个bash脚本?),例如:gdb -args ./bin/clt \\。\ yralthdrive1
也就是说,您的代码中有许多错误。
您没有检查
argc&gt; 1
在访问argv [1]
。之前是正确的
lpcstr
在lpfileName
createfile()
的参数中都施放,不需要删除。argv [1]
是char*
,您将其施放到const char*
(哪个createfile()
)想要,如果您要编译MBC构建而不是Unicode构建)。编译器可以为您隐含地做到这一点。您正在将
null
指针转到lpnumberofbytesread
readfile()
的参数。 per documentation P>您没有执行 asynchronous 阅读,因此您必须提供指向实际
dword
变量的指针:您的错误消息时
readfile()失败的格式不正确。您忘记了
%s
argv [1]
的占位符,因此您正在传递argv [1]
(achar*
)到%d
占位符,它期望int
,并且您不会将getLasterRor()
的值传递给任何占位符。您需要修复格式字符串,例如: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\\.\\PhysicalDrive1
in thecore
parameter, but that path does not point to a valid core dump file, hence the error. You are expecting gdb to pass\\.\\PhysicalDrive1
as a command-line parameter to your program, but that is not what it is actually doing. To do that, use the--args
parameter (see How do I run a program with commandline arguments using GDB within a Bash script?), eg:gdb --args ./bin/clt \\.\PhysicalDrive1
That said, there are a number of bugs in your code.
you are not checking if
argc > 1
is true before accessingargv[1]
.the
LPCSTR
cast in thelpFileName
parameter 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
NULL
pointer to thelpNumberOfBytesRead
parameter ofReadFile()
. Per the documentation:You are not performing an asynchronous read, so you must provide a pointer to an actual
DWORD
variable instead:your error message when
ReadFile()
fails is being formatted incorrectly. You forgot to include a%s
placeholder forargv[1]
, so you are passingargv[1]
(achar*
) to the%d
placeholder, which expects anint
, and you are not passing the value ofGetLastError()
to any placeholder at all. You need to fix your format string, eg: