簡體   English   中英

問:關於QT的memmove()和memcpy()(c ++)

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

簡單的問題,當我使用它時,我有memmove()和memcpy()的問題。 我真的不明白我的代碼有什么問題。 順便說一句,我使用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不是指針。 memmove需要兩個指針。 通過正確聲明hMapView來解決此問題。 它應該是LPVOID

MapViewOfFile返回一個指針,或者在出現錯誤時返回NULL (0),而不是INVALID_HANDLE_VALUE (-1)。

編輯:您的代碼還有很多其他問題:

  • QString::constData()返回QChar* ,而不是wchar_t* ,你必須使用QString::utf16()
  • 如果CreateFileMappingW失敗,則返回NULL ,而不是INVALID_HANDLE_VALUE
  • MapViewOfFile訪問參數是FILE_MAP_READ ,而不是GENERIC_READ
  • uint通常大於2個字節,因此如果只讀取2個字節,則應在memmove之前將變量初始化為0。

這是一個應該工作的最小代碼(僅在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)是一個明顯未記錄的Qt函數,它返回上一個錯誤的錯誤字符串(來自GetLastError()errno返回的錯誤代碼)。

如果您使用的是Qt,則可以使用QFile :: map()將文件映射到內存。
要執行初始代碼應該執行的操作,您只需要在找到的代碼示例中添加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(); 
    }
} 

順便說一句,我使用QT。

你並沒有在你的例子中使用它。 Qt有QFile :: map方法,可以(在我看來應該)使用而不是特定於平台的MapViewOfFile。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM