【发布时间】:2021-12-26 15:08:14
【问题描述】:
我在重写文件时遇到了问题,我收到了这个错误。也许有人可以告诉我如何转换数据类型以便可以重写文件
.\wInaPi.cpp: In function 'INT main(INT, CHAR**)':
.\wInaPi.cpp:30:54: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
30 | PIMAGE_DOS_HEADER image_dos_header = (PIMAGE_DOS_HEADER) file_read;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\wInaPi.cpp:76:63: error: invalid conversion from 'BOOL' {aka 'int'} to 'LPCVOID' {aka 'const void*'} [-fpermissive]
76 | WriteFile(PEFile, file_read, file_size, &returned_bytes, NULL);
| ^~~~~~~~~
| |
| BOOL {aka int}
In file included from C:/mingw64/x86_64-w64-mingw32/include/winbase.h:18,
from C:/mingw64/x86_64-w64-mingw32/include/windows.h:70,
from .\wInaPi.cpp:1:
C:/mingw64/x86_64-w64-mingw32/include/fileapi.h:186:62: note: initializing argument 2 of 'WINBOOL WriteFile(HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED)'
186 | WINBASEAPI WINBOOL WINAPI WriteFile (HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped);
|
我的部分代码
HANDLE PEfile = CreateFileA(target_process, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (PEfile != INVALID_HANDLE_VALUE)
{
DWORD file_size = GetFileSize(x_file, NULL);
PBYTE file_buffer = PBYTE(LocalAlloc(LPTR, file_size));
DWORD returned_bytes;
BOOL file_read = ReadFile(x_file, file_buffer, file_size, &returned_bytes, NULL);
if (file_read == TRUE && returned_bytes == file_size)
{
if (SetFilePointer(PEfile, 0, NULL, FILE_BEGIN) != INVALID_SET_FILE_POINTER)
{
WriteFile(PEfile, file_read, file_size, &returned_bytes, NULL); // got error here
}
}
}
【问题讨论】:
-
考虑将minimal reproducible example 放在一起,这样我们就可以看到您正在使用的变量的类型。
-
我在上面添加了部分代码
-
您需要将
file_buffer传递给WriteFile(),而不是file_read。 -
@RemyLebeau 是的,我明白了,太累了。谢谢。