C++ 为什么我要用WriteFile写入0字节?
我正在尝试制作一个程序,在用户定义的目录中查找下一个.exe文件,并向其写入一个标志(对于计算机安全类) 它目前正在写入0字节,我不知道为什么 以下是我目前的代码:C++ 为什么我要用WriteFile写入0字节?,c++,windows,file-io,exe,C++,Windows,File Io,Exe,我正在尝试制作一个程序,在用户定义的目录中查找下一个.exe文件,并向其写入一个标志(对于计算机安全类) 它目前正在写入0字节,我不知道为什么 以下是我目前的代码: #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <Windows.h> #include <fstream> using namespace std; typedef wchar_t wchar; struct the
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <Windows.h>
#include <fstream>
using namespace std;
typedef wchar_t wchar;
struct thePlague{
long int origBytes;
wchar_t flag[6];
};
void main() {
WIN32_FIND_DATA findFileData; //Windows structure that receives info about a found file or directory
HANDLE hFind = INVALID_HANDLE_VALUE; //Handle that holds whether or not a file was successfully found with FindNextFile
LARGE_INTEGER fileSize;
wchar szDir[MAX_PATH];
DWORD dwError = 0;
wchar directory[MAX_PATH];
wcout<<L"Please enter a directory: ";
wcin>>directory;
// Check that the input path plus 7 is not longer than MAX_PATH.
// 7 characters are for the "\*.exe" plus NULL appended below.
if (wcslen(directory) > (MAX_PATH - 7)) {
wcout<<L"\nDirectory is too long\n";
return;
}
wcout<<L"Target directory is: "<<directory<<endl;
// Prepare string for use with FindFile functions. First, copy the
// string to a buffer, then append '\*' to the directory name.
wcscpy(szDir, directory);
wcscat(szDir, L"\\*.exe");
//Find the first file in the directory
hFind = FindFirstFile(szDir, &findFileData);
if (hFind == INVALID_HANDLE_VALUE) {
wcout<<L"FindFirstFile Failed"<<GetLastError()<<endl;
return;
}
do {
fileSize.LowPart = findFileData.nFileSizeLow;
fileSize.HighPart = findFileData.nFileSizeHigh;
wcout<<findFileData.cFileName<<L" "<<fileSize.QuadPart<<L" bytes\n";
DWORD dwBytesWritten = 0; //# of bytes written by WriteFile
wchar_t *buffer = (wchar *)malloc(sizeof(thePlague)); //A buffer the size of our struct (infection flag)
wofstream writeToBuffer(buffer, ios::binary); //Open the buffer for writing
thePlague newVictim; //New infection flag
writeToBuffer.write((const wchar_t*)&newVictim, sizeof(thePlague));
writeToBuffer.close();
DWORD dwErrorFlag;
dwErrorFlag = WriteFile(hFind, //The handle holding the file we are writing to
buffer, //The buffer holding what we want to write to the file, in this case a struct
sizeof(thePlague), //How many bytes to write to the file
&dwBytesWritten, //Where to store the # of bytes successfully written
NULL //Where to write to incase of overlap
);
if (dwErrorFlag == false)
wcout<<L"Error was: "<<GetLastError()<<endl;
wcout<<L"# of bytes written: "<<dwBytesWritten<<endl;
} while(FindNextFile(hFind, &findFileData) != 0);
FindClose(hFind);
return;
}
我知道这是一个错误\u无效\u句柄,但如何修复它?
FindFirstFile
返回一个搜索句柄,但WriteFile
需要一个打开的文件句柄。使用CreateFile
在写入文件之前打开文件,使用WriteFile
在写入文件之前需要打开文件。您的句柄hFind
只是一个遍历目录/文件的句柄,而不是一个打开的文件句柄
使用()打开文件您需要向文件传递句柄,而不是其他句柄。您从
FindFirstFile
获得的句柄仅用于FindNextFile
和FindClose
。哦,好的,谢谢!我认为CreateFile只是用来创建新文件,而不是打开一个已经存在的文件。谢谢你的帮助,我试试这个
Please enter a directory: C:\Infect
Target directory is: C:\Infect
hello.exe 65536 bytes
Error was: 6
# of bytes written: 0
Press any key to continue . . .