欧恩意 2021-08-13 10:44 采纳率: 0%
浏览 168
已结题

DeviceIoControl 错误87:参数错误

使用了微软官方的获取缓冲区变更日志的demo,但是在使用的时候报错(87)

调用 DeviceIoControl - Win32 apps | Microsoft Docs 调用 DeviceIoControl https://docs.microsoft.com/zh-cn/windows/win32/devio/calling-deviceiocontrol

#include <Windows.h>
#include <WinIoCtl.h>
#include <stdio.h>
#include <ntddser.h>

#define BUF_LEN 4096

struct MY_USN_RECORD
{
    DWORDLONG FileReferenceNumber;
    DWORDLONG ParentFileReferenceNumber;
    LARGE_INTEGER TimeStamp;
    DWORD Reason;
    WCHAR FileName[MAX_PATH];
};

void main()
{
   HANDLE hVol;
   CHAR Buffer[BUF_LEN];

   USN_JOURNAL_DATA JournalData;
   
   PUSN_RECORD UsnRecord;  

   DWORD dwBytes;
   DWORD dwRetBytes;
   int I;


   // 创建设备HANDLE
   hVol = CreateFile( TEXT("\\\\.\\C:"), 
               GENERIC_READ | GENERIC_WRITE, 
               FILE_SHARE_READ | FILE_SHARE_WRITE,
               NULL,
               OPEN_EXISTING,
               0,
               NULL);

   if( hVol == INVALID_HANDLE_VALUE )
   {
      printf("CreateFile failed (%d)\n", GetLastError());
      return;
   }

   // 查询HANDLE ID
   if( !DeviceIoControl( hVol, 
          FSCTL_QUERY_USN_JOURNAL, 
          NULL,
          0,
          &JournalData,
          sizeof(JournalData),
          &dwBytes,
          NULL) )
   {
      printf( "Query journal failed (%d)\n", GetLastError());
      return;
   }

   // 赋值
   READ_USN_JOURNAL_DATA ReadData = { 0, -1 , 0, 0, 0, JournalData.UsnJournalID };


   printf("sizeof dwBytes: %d\n",sizeof(dwBytes));

   printf("Journal ID: %I64x\n", JournalData.UsnJournalID);
   printf( "FirstUsn: %I64x\n\n", JournalData.FirstUsn );


   // 遍历
   std::deque<MY_USN_RECORD> con;
   for (; DeviceIoControl(hVol,
       FSCTL_READ_USN_JOURNAL,
       &ReadData,
       sizeof(ReadData),
       &Buffer,
       BUF_LEN,
       &dwBytes,
       NULL); ReadData.StartUsn = *(USN*)&Buffer)
   {
       DWORD dwRetBytes = dwBytes - sizeof(USN);
       PUSN_RECORD UsnRecord = (PUSN_RECORD)((PCHAR)Buffer + sizeof(USN));
       if (dwRetBytes == 0)
       {
           break;
       }

       while (dwRetBytes > 0)
       {
           MY_USN_RECORD myur = { UsnRecord->FileReferenceNumber, UsnRecord->ParentFileReferenceNumber, UsnRecord->TimeStamp, UsnRecord->Reason };
           memcpy(myur.FileName, UsnRecord->FileName, UsnRecord->FileNameLength);
           myur.FileName[UsnRecord->FileNameLength / 2] = L'\0';

           con.push_back(myur);

           dwRetBytes -= UsnRecord->RecordLength;
           UsnRecord = (PUSN_RECORD)((PCHAR)UsnRecord + UsnRecord->RecordLength);
       }

       printf("For循环:%s",Buffer);
   }

 

   // 官方遍历
   for(I=0; I<=10; I++)
   {
      memset( Buffer, 0, BUF_LEN );

      if( !DeviceIoControl( hVol, 
            FSCTL_READ_USN_JOURNAL, 
            &ReadData, 
            sizeof(ReadData),
            &Buffer,
            BUF_LEN,
            &dwBytes,
            NULL) )
      {
         printf( "Read journal failed (%d)\n", GetLastError());
         printf("Buffer:%s\n",Buffer);
         return;
      }

      dwRetBytes = dwBytes - sizeof(USN);

      // Find the first record
      UsnRecord = (PUSN_RECORD)(((PUCHAR)Buffer) + sizeof(USN));  

      printf( "****************************************\n");

      // This loop could go on for a long time, given the current buffer size.
      while( dwRetBytes > 0 )
      {
         printf( "USN: %I64x\n", UsnRecord->Usn );
         printf("File name: %.*S\n", 
                  UsnRecord->FileNameLength/2, 
                  UsnRecord->FileName );
         printf( "Reason: %x\n", UsnRecord->Reason );
         printf( "\n" );

         dwRetBytes -= UsnRecord->RecordLength;

         // Find the next record
         UsnRecord = (PUSN_RECORD)(((PCHAR)UsnRecord) + 
                  UsnRecord->RecordLength); 
      }
      // Update starting USN for next call
      ReadData.StartUsn = *(USN *)&Buffer; 
   }

   CloseHandle(hVol);

}

在Release下用管理员在cmd运行的程序,有朋友能看出来问题在哪?

img

控制码使用FSCTL_QUERY_USN_JOURNAL的时候DeviceIoControl函数是没有问题的;但是当使用FSCTL_READ_USN_JOURNAL的时候就会失败,这是个什么问题

  • 写回答

1条回答 默认 最新

  • 欧恩意 2021-08-13 17:36
    关注

    补充一下:使用管理员cmd运行的release程序

    评论

报告相同问题?

问题事件

  • 系统已结题 8月21日
  • 创建了问题 8月13日

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?