&quot。\\。不是核心转储:未识别的文件格式

发布于 2025-02-04 10:17:53 字数 1417 浏览 1 评论 0原文

我一直在研究幻想控制台类型项目。我决定将闪光灯驱动器归零,然后将其用作幻想控制台的ROM输入。在尝试写代码直接从驱动器读取字节时(而不是从驱动器上的文件中),我发现了一个特殊的错误。

尝试通过createFilereadfile使用文件路径\\。 。

​如上所示。关于这个问题的信息是我的研究非常稀缺,我发现的信息很少,它特定于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2025-02-11 10:17:53

您显示的代码中没有任何内容会产生您声称的错误消息。该错误消息是调试器消息,而不是编译器错误或运行时系统错误。这个问题与您显示的代码无关,以及与您使用调试器的方式有关。

per gdb文档

您也可以从指定的可执行程序和核心文件开始:

GDB程序核心

在注释中,您说您正在运行gdb ./bin/clt \\。\\。 /code>,这意味着您正在使用\\。\\ yralthdrive1core> core参数中调用GDB,但是该路径没有指向有效的核心转储文件,因此错误。您期望GDB将\\。\\物理Drive1作为您程序的命令行参数,但这不是实际在做的。为此,请使用- args参数(请参见如何使用commandline参数运行一个使用GDB的程序,一个bash脚本?),例如:

gdb -args ./bin/clt \\。\ yralthdrive1


也就是说,您的代码中有许多错误。

  • 您没有检查argc&gt; 1在访问argv [1]

    之前是正确的

  • lpcstrlpfileName createfile()的参数中都施放,不需要删除。 argv [1]char*,您将其施放到const char*(哪个createfile())想要,如果您要编译MBC构建而不是Unicode构建)。编译器可以为您隐含地做到这一点。

  • 您正在将null指针转到lpnumberofbytesread readfile()的参数。 per documentation P>

    [out,可选] lpnumberofbytesread

    使用同步HFILE参数时接收字节数的变量的指针。读取文件将此值设置为零,然后进行任何工作或错误检查。如果这是一个异步操作以避免可能错误的结果。

    只有当lpoverlapped参数不是null时,此参数才能为null。

    您没有执行 asynchronous 阅读,因此您必须提供指向实际dword变量的指针:

      dword read = 0;
    
    int res = readfile(romf,rom,4,&amp; read,null);
     
  • 您的错误消息时readfile()失败的格式不正确。您忘记了%s argv [1]的占位符,因此您正在传递argv [1](a char*)到%d占位符,它期望int,并且您不会将getLasterRor()的值传递给任何占位符。您需要修复格式字符串,例如:

      printf(“错误读取文件%s(%d)\ n”,argv [1],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:

You can also start with both an executable program and a core file specified:

gdb program core

In comments, you state that you are running gdb ./bin/clt \\.\\PhysicalDrive1, which means you are calling gdb with \\.\\PhysicalDrive1 in the core 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 accessing argv[1].

  • the LPCSTR cast in the lpFileName parameter of CreateFile() is not needed and should be removed. argv[1] is a char* and you are casting it to const char* (which CreateFile() 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 the lpNumberOfBytesRead parameter of ReadFile(). Per the documentation:

    [out, optional] lpNumberOfBytesRead

    A pointer to the variable that receives the number of bytes read when using a synchronous hFile parameter. ReadFile sets this value to zero before doing any work or error checking. Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results.

    This parameter can be NULL only when the lpOverlapped parameter is not NULL.

    You are not performing an asynchronous read, so you must provide a pointer to an actual DWORD variable instead:

    DWORD read = 0;
    
    int res = ReadFile(romf, rom, 4, &read, NULL);
    
  • your error message when ReadFile() fails is being formatted incorrectly. You forgot to include a %s placeholder for argv[1], so you are passing argv[1] (a char*) to the %d placeholder, which expects an int, and you are not passing the value of GetLastError() to any placeholder at all. You need to fix your format string, eg:

    printf("Error reading file %s (%d)\n", argv[1], GetLastError());
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文