c++ - Exe 在 BackupRead Windows 函数中崩溃

标签 c++ windows file winapi backup

下面是我尝试备份文件的 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 值。 (LPVOIDvoid*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/

相关文章:

c++ - 如何将链表对象写入文件

c++ - 对构造函数调用的约束

c++ - 为什么不为动态加载的 Qt 插件类调用析构函数?

c++ - 优化:昂贵的分支与便宜的比较

java - 将音频从 PC 流式传输到智能手机?

python - 有没有办法从 Windows 单击调用 python?

c++ - 从 linux 交叉编译到 windows 的 visual studio 项目

c++ - 如何解析聚合管道结果?

javascript - 为什么即使调用 evt.preventDefault();,更改 JavaScript 中的内部 html 也会导致默认操作?

java - 按照原样将字符串写入文件中