下面是我尝试备份文件的 C++ 代码,包括安全信息。我使用了备份读取,但每当调用代码时,exe 就会崩溃。
char buff[225280];
DWORD numberOfBytesToRead = 225280;
DWORD dwBytesRead=0, dwBytesWritten, dwBytesRead2=0;
BOOL bProcessSecurity = TRUE;
LPWSTR sourceBackupFile = L"E:\\myFolder\\backup.txt";
HANDLE source = CreateFile(sourceBackupFile, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// Check for errors
if (source == INVALID_HANDLE_VALUE) {
cout<<"The Handle is Invalid:"<<GetLastError()<<endl;
}
else
{
cout<< "\n The source file is in E:\\myFolder\\backup.txt" <<endl ;
}
LPDWORD numberofbytedsreadinreadFile = 0;
cout << "Point Of Crash" << endl;
if (!BackupRead(
source,
&buff,
numberOfBytesToRead,
numberofbytedsreadinreadFile,
FALSE,
TRUE,
NULL
))
{
cout << "Backup Read Failed with the error::" << GetLastError() << endl ;
}
它在崩溃前打印这个
The source file is in E:\\myFolder\\backup.txt
"Point of Crash"
最佳答案
您将 NULL
作为最后一个参数传递给 BackupRead
,根据 docs 这显然是无效的.
lpContext [out] Pointer to a variable that receives a pointer to an internal data structure used by BackupRead to maintain context information during a backup operation. You must set the variable pointed to by lpContext to NULL before the first call to BackupRead for the specified file or directory. The function allocates memory for the data structure, and then sets the variable to point to that structure. You must not change lpContext or the variable that it points to between calls to BackupRead. To release the memory used by the data structure, call BackupRead with the bAbort parameter set to TRUE when the backup operation is complete.
您应该传递一个指向它的指针,该指针指向 null,而不是 null 值。 (LPVOID
是void*
,LPVOID**
表示void**
)
与 numberofbytedsreadinreadFile
相同:您应该将参数传递给现有变量,而不是空指针,它是一个输出参数。
void* backupContext = NULL;
int numberOfBytesRead = 0;
cout << "Point Of Crash" << endl;
if (!BackupRead(
source,
&buff,
numberOfBytesToRead,
&numberOfBytesRead,
FALSE,
TRUE,
&backupContext
))
如果您得到一个无效的句柄,您也应该从此方法返回而不是继续。
关于c++ - Exe 在 BackupRead Windows 函数中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38525647/