5786|9

75

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

用DDK和WriteFile读写U盘扇区的问题 [复制链接]

最近在研究U盘过滤驱动,需要读取U盘扇区。

我用WriteFile向U盘第二个扇区写入一个标识,是我自己生成的一个序列号。

但是随后我用DDK读取第二个扇区时没有读取到这个标识。

然后我用DDK直接向U盘扇区写入一个标识,再用DDK读取时能读取到。

我想是不是用WriteFile 和 DDK读取的扇区不是一个扇区?

请大家指点一下,谢谢。

最新回复

谢谢pla_007 我已经搞定了。 不过不是采用你的方法 你的方法有局限性 不能区分是本地硬盘、移动硬盘或是U盘。  详情 回复 发表于 2009-10-16 13:57
点赞 关注

回复
举报

46

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
在应用层向U盘扇区写入标识的代码如下:
BOOL writeSectors( char pDiskPath[], BYTE cWriteBuffer[],DWORD dwStartSector, DWORD dNumberOfBytesToWrite){
       
        HANDLE hFile = CreateFile(pDiskPath, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ , NULL, OPEN_EXISTING, 0, NULL );
       
        if(hFile == INVALID_HANDLE_VALUE){
                MessageBox(0, "can't open the disk!", 0, 0);
                return 0;
        }//end of if
       
        SetFilePointer(hFile, dwStartSector * 512, 0, FILE_BEGIN);
       
        DWORD dwWriteByte;        //被写入的字节数
        BOOL bWrite = WriteFile(hFile,cWriteBuffer,dNumberOfBytesToWrite, &dwWriteByte,NULL);
                if(dwWriteByte == 0){
                MessageBox(0, "Write disk error!", 0, 0);
                CloseHandle(hFile);
                return 0;
        } //end of if
       
        CloseHandle(hFile);
        return bWrite;       
       
}//end of method writeSectors
 
 

回复

51

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
在我的U盘过滤驱动的代码中,读取U盘扇区是采用构造srb的方式读取扇区的。
部分代码如下:

SCSI_REQUEST_BLOCK   srbbuf;
PSCSI_REQUEST_BLOCK        srb = &srbbuf;
PCDB  cdb;
int sector = 2;

RtlZeroMemory(srb, sizeof(SCSI_REQUEST_BLOCK));
srb->Length = sizeof(SCSI_REQUEST_BLOCK);
srb->Function = SRB_FUNCTION_EXECUTE_SCSI;

srb->CdbLength = 10;
cdb = (PCDB)srb->Cdb;

cdb->CDB10.LogicalBlockByte0 = ((PFOUR_BYTE)§or)->Byte3;
cdb->CDB10.LogicalBlockByte1 = ((PFOUR_BYTE)§or)->Byte2;
cdb->CDB10.LogicalBlockByte2 = ((PFOUR_BYTE)§or)->Byte1;
cdb->CDB10.LogicalBlockByte3 = ((PFOUR_BYTE)§or)->Byte0;
cdb->CDB10.TransferBlocksMsb = 0;
cdb->CDB10.TransferBlocksLsb = 1;
cdb->CDB10.OperationCode = SCSIOP_READ;
cdb->CDB10.ForceUnitAccess = TRUE;

Srb->SenseInfoBufferLength = SENSE_BUFFER_SIZE;
Srb->SenseInfoBuffer = senseInfoBuffer;
Srb->DataBuffer = BufferAddress;
Srb->SrbFlags=SRB_FLAGS_DATA_IN;
Srb->QueueAction = SRB_SIMPLE_TAG_REQUEST;
Srb->SrbFlags |=SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
Srb->SrbFlags |=SRB_FLAGS_NO_QUEUE_FREEZE;
................
status = IoCallDriver(pdx->LowerDeviceObject, irp);

没有读取到我用writefile写入的标识,
sector 我换成0,1..随便几个数也没读取到

是不是因为writefile写入的是物理扇区 而DDK读取的是逻辑扇区?
请高手指点

 
 
 

回复

63

帖子

0

TA的资源

一粒金砂(初级)

4
 
楼主很强大,是否有兴趣到我们公司工作:
深圳知名通信企业,高薪诚聘底层软件设计师、工程师,
4年以上嵌入式系统开发设计经验;
熟悉PowerPC/mips/ARM/X86至少两种或以上处理器架构;
熟悉CPU的多核、多线程、cache、MMU等常用硬件技术,深入了解嵌入式OS(VXWORKS,linux等);
熟悉常用硬件单元或芯片,如网口,flash,硬件加速引擎的软件驱动技术;
如果有意向可简历发到:
zwd2009101@126.com
 
 
 

回复

70

帖子

0

TA的资源

一粒金砂(中级)

5
 
为什么不用DeviceIoControl
 
 
 

回复

70

帖子

0

TA的资源

一粒金砂(初级)

6
 
To:zwd2009101
我是菜鸟 呵呵
不想去深圳
估计会一直在北京

To:
因为代码别人已经写好了,是在应用层写的。
然后我才开始研究U盘过滤驱动
我前一段一直用srb读取那个用WriteFile写入的标识,一直没读到。
我是菜鸟,一直以为是自己的代码有问题,昨天自己试着用srb写入一个标识,然后用srb去读,能读取到.
才知道自己的代码没问题。耽误了我不少时间。


谁能给我解释一下用WriteFile和用srb写磁盘到底有什么区别?
其中,CreateFile里面的char cFilePath[] = "\\\\.\\H:";之类的。

还有,谁能告诉我磁盘设备连接符是什么?
我在google上没找到

 
 
 

回复

72

帖子

0

TA的资源

一粒金砂(初级)

7
 
用WinObj可以看到磁盘的设备连接符。
 
 
 

回复

66

帖子

0

TA的资源

一粒金砂(初级)

8
 
这是google代码搜索里找的一个示例,可以参考一下。


  1. //---------------------------------------------------------------------
  2. // WriteDiskSector: Write disk sector (512 bytes) in Windows
  3. //---------------------------------------------------------------------
  4. extern "C" BOOL WriteDiskSector(int drive, DWORD startinglogicalsector, int numberofsectors, BYTE *buffer)
  5. {
  6.         HANDLE hDevice;
  7.         DWORD byteswritten;

  8.         // Creating a handle to drive a: using CreateFile () function ..
  9.         char _devicename[] = "\\\\.\\PhysicalDrive0";
  10.         _devicename[17] += drive;
  11.         hDevice = CreateFile(_devicename,  // drive to open
  12.                                                 GENERIC_READ|GENERIC_WRITE,     // access type
  13.                                                 FILE_SHARE_READ | // share mode
  14.                                                 FILE_SHARE_WRITE,
  15.                                                 NULL,             // default security attributes
  16.                                                 OPEN_EXISTING,    // disposition
  17.                                                 0,                // file attributes
  18.                                                 NULL);            // do not copy file attributes


  19.     if (hDevice == INVALID_HANDLE_VALUE)
  20.         return FALSE;

  21.         DiskFileSeek(hDevice, (startinglogicalsector),FILE_BEGIN);

  22.         if (!WriteFile (hDevice, buffer, 512*numberofsectors, &byteswritten, NULL) )
  23.                  return FALSE;

  24.         CloseHandle(hDevice);
  25.         return TRUE;
  26. }
复制代码

 
 
 

回复

76

帖子

0

TA的资源

一粒金砂(初级)

9
 

  1. //-----------------------------------------------------------------------------
  2. //-----------------------------------------------------------------------------
  3. //                                              FAT32 File IO Library
  4. //                                                                  V2.0
  5. //                                                               Rob Riglar
  6. //                                                  Copyright 2003 - 2007
  7. //
  8. //                                        Email: rob@robriglar.com
  9. //
  10. //-----------------------------------------------------------------------------
  11. //
  12. // This file is part of FAT32 File IO Library.
  13. //
  14. // FAT32 File IO Library is free software; you can redistribute it and/or modify
  15. // it under the terms of the GNU General Public License as published by
  16. // the Free Software Foundation; either version 2 of the License, or
  17. // (at your option) any later version.
  18. //
  19. // FAT32 File IO Library is distributed in the hope that it will be useful,
  20. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. // GNU General Public License for more details.
  23. //
  24. // You should have received a copy of the GNU General Public License
  25. // along with FAT32 File IO Library; if not, write to the Free Software
  26. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  27. //-----------------------------------------------------------------------------
  28. //-----------------------------------------------------------------------------
  29. #include "..\define.h"
  30. #include
  31. #include
  32. #include
  33. #include

  34. // Warning: If you get the wrong physical drive ID you could erase your main hard disk!!

  35. #ifdef SOURCE_WINDOWS_PHYSICAL_DRIVE

  36. // File Functions
  37. static __int64 DiskFileSeek(HANDLE hf, __int64 distance, DWORD MoveMethod);

  38. //---------------------------------------------------------------------
  39. // ReadSector: Read disk sector (512 bytes) in Windows
  40. //---------------------------------------------------------------------
  41. extern "C" BOOL ReadDiskSector(int drive, DWORD startinglogicalsector, int numberofsectors, BYTE *buffer)
  42. {
  43.         HANDLE hDevice;
  44.         DWORD bytesread;

  45.         // Creating a handle to drive a: using CreateFile () function ..
  46.         char _devicename[] = "\\\\.\\PhysicalDrive0";
  47.         _devicename[17] += drive;
  48.         hDevice = CreateFile(_devicename,  // drive to open
  49.                                                 GENERIC_READ,     // access type
  50.                                                 FILE_SHARE_READ | // share mode
  51.                                                 FILE_SHARE_WRITE,
  52.                                                 NULL,             // default security attributes
  53.                                                 OPEN_EXISTING,    // disposition
  54.                                                 0,                // file attributes
  55.                                                 NULL);            // do not copy file attributes


  56.     if (hDevice == INVALID_HANDLE_VALUE)
  57.         return FALSE;

  58.         DiskFileSeek(hDevice, (startinglogicalsector),FILE_BEGIN);

  59.         if (!ReadFile (hDevice, buffer, 512*numberofsectors, &bytesread, NULL) )
  60.                  return FALSE;

  61.         CloseHandle(hDevice);
  62.         return TRUE;
  63. }
  64. //---------------------------------------------------------------------
  65. // WriteDiskSector: Write disk sector (512 bytes) in Windows
  66. //---------------------------------------------------------------------
  67. extern "C" BOOL WriteDiskSector(int drive, DWORD startinglogicalsector, int numberofsectors, BYTE *buffer)
  68. {
  69.         HANDLE hDevice;
  70.         DWORD byteswritten;

  71.         // Creating a handle to drive a: using CreateFile () function ..
  72.         char _devicename[] = "\\\\.\\PhysicalDrive0";
  73.         _devicename[17] += drive;
  74.         hDevice = CreateFile(_devicename,  // drive to open
  75.                                                 GENERIC_READ|GENERIC_WRITE,     // access type
  76.                                                 FILE_SHARE_READ | // share mode
  77.                                                 FILE_SHARE_WRITE,
  78.                                                 NULL,             // default security attributes
  79.                                                 OPEN_EXISTING,    // disposition
  80.                                                 0,                // file attributes
  81.                                                 NULL);            // do not copy file attributes


  82.     if (hDevice == INVALID_HANDLE_VALUE)
  83.         return FALSE;

  84.         DiskFileSeek(hDevice, (startinglogicalsector),FILE_BEGIN);

  85.         if (!WriteFile (hDevice, buffer, 512*numberofsectors, &byteswritten, NULL) )
  86.                  return FALSE;

  87.         CloseHandle(hDevice);
  88.         return TRUE;
  89. }
  90. //---------------------------------------------------------------------
  91. // DiskFileSeek: Allow seeking through large files (a disk opened as a
  92. // file).
  93. //---------------------------------------------------------------------
  94. static __int64 DiskFileSeek(HANDLE hf,__int64 distance, DWORD MoveMethod)
  95. {
  96.         __int64 seekDistance=0;
  97.         seekDistance = distance*512;
  98.         LARGE_INTEGER li;
  99.         li.QuadPart = seekDistance;
  100.         li.LowPart = SetFilePointer (hf, li.LowPart, &li.HighPart, MoveMethod);
  101.         return li.QuadPart;
  102. }

  103. #endif
复制代码
 
 
 

回复

64

帖子

0

TA的资源

一粒金砂(初级)

10
 
谢谢pla_007
我已经搞定了。
不过不是采用你的方法
你的方法有局限性
不能区分是本地硬盘、移动硬盘或是U盘。
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表