【问题标题】:How can I convert DWORD or string to BYTE*如何将 DWORD 或字符串转换为 BYTE*
【发布时间】:2020-01-21 05:07:48
【问题描述】:

您好,我正在创建一个文件加密代码,我读取了一个文件,我需要将正在读取的文件转换为 BYTE* 我尝试搜索,但每次我得到"CL.exit"。这就是我读取文件的方式。

HANDLE getFile = createFile();
    DWORD reciveBytes = 0;
    //If it's byte or kilobyte the size of the buffer will be 1024.
    //If it's megabytes or gigabyte the size of the buffer will be 4096.
    const DWORD Buffersize = 66232; // gave me warning for 1024
    DWORD buffer[Buffersize];
    string fileInput;
    if (ReadFile(
        getFile,
        buffer,
        Buffersize,
        &reciveBytes,
        NULL
    )) {
    }
    else {
        cout << "Faild!" << endl;
        cout << GetLastError() << endl;
    }
    /*
    for (unsigned int i = 0; i < reciveBytes; i++) {
        if (buffer[i] != '\0') {
            fileInput = fileInput + buffer[i];
        }
    }
    */
    return buffer[reciveBytes];

现在,我需要将返回类型转换为 BYTE*,这样我就可以执行以下操作:BYTE* protect = (BYTE*)"Hello world!"; 这是 createFile():

HANDLE getFile = CreateFileA(
        fileName,
        GENERIC_READ,
        0,
        NULL,
        OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, 
        NULL
    );

【问题讨论】:

  • 您不能返回指向局部变量的指针。一旦你的函数返回,buffer 就永远消失了。您可能首先应该拥有BYTE *buffer = malloc(Buffersize);,然后是return buffer;。但无论如何,这个问题仍然很不清楚,尤其是BYTE* protect = (BYTE*)"Hello world!"; 部分
  • @Jabberwocky 这只是我试图表明你想要我想做的一个例子。就像参数应该如何工作一样。所以参数应该像Hello world。所以我需要做的是做“*buffer = malloc(Buffersize);”在阅读文件中,然后返回缓冲区?
  • 另外,您的错误处理是错误的。您需要在 API 调用返回后立即调用 GetLastError。在您的代码中,您将流式传输到cout,这可能会调用另一个 API 函数并因此修改错误代码。
  • 读取文件。分配一个缓冲区并使用ReadFile 读入它。或者更好的是,使用 C++ 标准库流类来读取文件。为什么要使用标准库简单的Win32 API?您询问如何将 DWORD 或字符串转换为字节数组,但您展示的代码是关于读取文件的。没有 DWORD 或字符串。您的代码已经尝试直接读入字节数组。而我们只能看到一小部分代码。
  • 如果要读取BYTE*,为什么不直接将buffer定义为BYTE数组类型呢? BYTE buffer [Buffersize*sizeof (DWORD)];return buffer [reciveBytes]”,好像是想返回数组,但是这样写是不对的。你需要先申请一个不会在函数结束时自动释放的内存(比如说BYTE* memory),从缓冲区中复制(或者将memory直接传入ReadFile),然后return memory;

标签: c++ file winapi


【解决方案1】:
#include <windows.h>
#include <iostream>
HANDLE createFile()
{
    HANDLE getFile = CreateFileA(
        "xxx.txt",
        GENERIC_READ,
        0,
        NULL,
        OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
        return getFile;
}
BYTE* memoryfromfile()
{
    HANDLE getFile = createFile();
    DWORD reciveBytes = 0;
    //If it's byte or kilobyte the size of the buffer will be 1024.
    //If it's megabytes or gigabyte the size of the buffer will be 4096.
    const DWORD Buffersize = 66232; // gave me warning for 1024
    BYTE* memory = new BYTE[Buffersize];
    memset(memory, 0, Buffersize);
    std::string fileInput;
    if (ReadFile(
        getFile,
        memory,
        Buffersize,
        &reciveBytes,
        NULL
    )) {
    }
    else {
        std::cout << GetLastError() << std::endl;
        std::cout << "Faild!" << std::endl;
    }
    /*
    for (unsigned int i = 0; i < reciveBytes; i++) {
        if (buffer[i] != '\0') {
            fileInput = fileInput + buffer[i];
        }
    }
    */
    return memory;
}
int main()
{
    BYTE * temp = memoryfromfile();
    std::cout << "temp = " << temp << std::endl;
    delete temp;
    system("pause");
    return 0;
}

【讨论】:

  • 这很奇怪。我得到了“CL.exe”
  • 能否分享您的可重现示例?或者使用我更新的示例代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
  • 2015-09-04
  • 1970-01-01
相关资源
最近更新 更多