问:QT(C ++)上的memmove()和memcpy()
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。
相关文章:
- Linux的Cpp上的计时器
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 物理键盘上的触发器按键
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- SSL上的`curl_easy_send`和`curl_asy_recv`:如何处理`CURLE_AGAIN`
- PowerPC ppc64le上的Gcc Woverloaded虚拟错误
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 集合上的输出迭代器:assign和increment迭代器
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 对于set上的循环-获取next元素迭代器
- 如何从棋盘上的箱号中找到行和列
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- if数组上的随机数
- 向量上的线性搜索
- 位阵列上的快速AND运算
- 无法将 GLFW 库与 Ubuntu 18.04 上的头文件链接
- 为什么一个向量上的多线程操作很慢
- 64位机器上的C++内存对齐
- 问:QT(C ++)上的memmove()和memcpy()