gpt4 book ai didi

c++ - Qt 在其自己的二进制文件中抛出工作代码错误

转载 作者:行者123 更新时间:2023-11-30 04:41:35 31 4
gpt4 key购买 nike

我自己编写了这个程序并且它运行良好,但是当作为头文件导入到 Qt 项目时,它会抛出一个错误代码,否则它可以运行。错误: error thrown from qt creator debug

这是头文件

#include <iostream>
#include <string>
#include <sstream>

//global vars
FILE *pFile;
char * buffer;
long lSize;

std::basic_stringstream<char> e_lfanew() {
pFile = fopen("hello.exe", "r");

//get file size
fseek(pFile, 0, SEEK_END);
long lSize = ftell(pFile);
rewind(pFile);

//alloc memory
buffer = (char *) malloc(sizeof(char) * lSize);

//read file to memory
auto result = fread(buffer, 1, lSize, pFile);

auto e_lfanew = (unsigned char *) malloc(4);
fseek(pFile, 0x3c, SEEK_SET);
fread(e_lfanew, 0x1, 0x4, pFile);
std::stringstream pe_offset;
for(size_t i=0; i<4; i++){
std::cout << std::hex << (int)e_lfanew[i];
pe_offset << std::hex << (int)e_lfanew[i];
}
std::cout << pe_offset.rdbuf();

return pe_offset;
}

这是qt mainwindow.c文件(有按键报错的函数)

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "e_lfanew.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow(){
delete ui;
}

void MainWindow::on_pushButton_clicked(){
QString e_lfanew_value = QString::fromStdString(e_lfanew().str());
ui->label_2->setText(e_lfanew_value);
}

最佳答案

您的程序不会从 Qt 二进制文件中抛出错误,而是从 minkernel/crts/ucrt/src/appcrt/stdio/fseek.cpp 中抛出错误。因此,当您尝试 fseek 时,我认为问题出在您的代码中。你的代码有一些问题,一个大问题是你在使用它之前没有检查文件描述符是否为空。

您可以从 fopen() 文档中阅读。

If the file is successfully opened, the function returns a pointer to a FILE object that can be used to identify the stream on future operations. Otherwise, a null pointer is returned. On most library implementations, the errno variable is also set to a system-specific error code on failure.

这意味着您可能会在打开文件后在 pFile 变量中得到一个空指针。尝试添加以下行。

std::basic_stringstream<char> e_lfanew() {
pFile = fopen("hello.exe", "r");

if (pFile == nullptr) // or simply if (!pFile)
// Cannot open file. Handle the situation

fseek(pFile, 0, SEEK_END);
long lSize = ftell(pFile);
rewind(pFile);

编辑1

正如 @Scheff 在评论中提到的,在 MS Windows 上,读取二进制文件应该总是明确地完成:fopen(file_name, "rb")(或 std::ifstream(file_name , std::ios::binary))。否则,您可能无法准确了解文件中的实际内容。

关于c++ - Qt 在其自己的二进制文件中抛出工作代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59099858/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com