简体   繁体   English

问:关于QT的memmove()和memcpy()(c ++)

[英]ask : memmove() and memcpy() on QT (c++)

simple question, I have problem with memmove() and memcpy() when i'm using it. 简单的问题,当我使用它时,我有memmove()和memcpy()的问题。 I really don't understand what wrong with my code. 我真的不明白我的代码有什么问题。 by the way i use QT. 顺便说一句,我使用QT。

HANDLE hFile;
HANDLE hMapFile;
HANDLE hMapView;

hFile = CreateFileW((const wchar_t*) objPath.constData(), GENERIC_READ , 0, NULL, OPEN_EXISTING, 0, NULL);
if (hFile != INVALID_HANDLE_VALUE){

    hMapFile = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
    if (hMapFile != INVALID_HANDLE_VALUE){

        hMapView = MapViewOfFile(hMapFile, GENERIC_READ, 0, 0,0);
        if (hMapView != INVALID_HANDLE_VALUE){
            uint DefineWord;
            memmove((void *) &DefineWord, hMapView,2); // <- always error right here
            qDebug()<<DefineWord;
        }
    }
}

hMapView is not a pointer. hMapView不是指针。 memmove requires two pointers. memmove需要两个指针。 Fix this by declaring hMapView properly. 通过正确声明hMapView来解决此问题。 It should be a LPVOID . 它应该是LPVOID

MapViewOfFile returns a pointer, or NULL (0) when there is an error, not INVALID_HANDLE_VALUE (-1). MapViewOfFile返回一个指针,或者在出现错误时返回NULL (0),而不是INVALID_HANDLE_VALUE (-1)。

Edit: There was a lot of other problems with your code: 编辑:您的代码还有很多其他问题:

  • QString::constData() returns QChar* , not wchar_t* , you have to use QString::utf16() instead. QString::constData()返回QChar* ,而不是wchar_t* ,你必须使用QString::utf16()
  • If CreateFileMappingW fails it returns NULL , not INVALID_HANDLE_VALUE . 如果CreateFileMappingW失败,则返回NULL ,而不是INVALID_HANDLE_VALUE
  • MapViewOfFile access parameter is FILE_MAP_READ , not GENERIC_READ . MapViewOfFile访问参数是FILE_MAP_READ ,而不是GENERIC_READ
  • uint is often bigger than 2 bytes, so you should initialize the variable to 0 before memmove if you only read 2 bytes. uint通常大于2个字节,因此如果只读取2个字节,则应在memmove之前将变量初始化为0。

Here is a minimal code that should work (only tested on wineg++/wine): 这是一个应该工作的最小代码(仅在wineg ++ / wine上测试):

#include <windows.h>
#include <QtCore/QString>
#include <QtCore/QDebug>
#include <QtCore/QTextStream>

int main(int argc, char const *argv[])
{
    if (argc < 2) {
        QTextStream(stdout) << "Usage :" << argv[0] << " filename" << endl;
        return 1;
    }

    QString objPath(argv[1]);
    // Qt source uses C-Style cast from utf16() to (wchar_t*),
    // so it should be safe
    HANDLE hFile = CreateFileW((const wchar_t *) objPath.utf16(), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    if (hFile == INVALID_HANDLE_VALUE) {
        qDebug() << qt_error_string(); 
    } else {
        HANDLE hMapFile = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
        if (!hMapFile) {
            qDebug() << qt_error_string(); 
        } else {
            void *pMapView = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
            if (!pMapView) {
                qDebug() << qt_error_string();
            } else {
                uint DefineWord = 0;
                memmove((void *) &DefineWord, pMapView, 2);
                qDebug() << DefineWord;
            }
            CloseHandle(hMapFile);
        }
        CloseHandle(hFile);
    }
    return 0;
}

PS: QString qt_error_string(int errorCode = -1) is an apparently undocumented Qt function that returns the error string of the last error (from the error code returned from GetLastError() or errno ). PS: QString qt_error_string(int errorCode = -1)是一个明显未记录的Qt函数,它返回上一个错误的错误字符串(来自GetLastError()errno返回的错误代码)。

If you are using Qt, you can map a file to memory with QFile::map() . 如果您使用的是Qt,则可以使用QFile :: map()将文件映射到内存。
To do what your initial code was supposed to do, you only had to add 2 lines to the code sample you found (plus the error checking): 要执行初始代码应该执行的操作,您只需要在找到的代码示例中添加2行(加上错误检查):

QFile file("foo"); 
if(!file.open(QFile::ReadOnly)) {
   qDebug() << file.errorString();
} else {
    uchar *memory = file.map(0, file.size()); 
    if (!memory) {
        qDebug() << file.errorString();
    } else {            
        uint DefineWord = 0;
        memmove(&DefineWord, memory, 2);

        file.unmap(); 
    }
} 

by the way i use QT. 顺便说一句,我使用QT。

You aren't really using it in your example. 你并没有在你的例子中使用它。 Qt has QFile::map method which can (and in my opinion should) be used instead of platform-specific MapViewOfFile. Qt有QFile :: map方法,可以(在我看来应该)使用而不是特定于平台的MapViewOfFile。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM