用DDK和WriteFile读写U盘扇区的问题

matrix2009 2009-10-15 01:27:54
最近在研究U盘过滤驱动,需要读取U盘扇区。

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

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

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

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

请大家指点一下,谢谢。
...全文
482 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wdp_victor 2011-05-22
  • 打赏
  • 举报
回复
你好,我也正在学习USB 驱动过虑,请问你是怎么解决的啊,能把代码发给我参考下吗?我的QQ:172092689 ,发到我QQ邮箱也行,多谢!
pla_007 2009-10-16
  • 打赏
  • 举报
回复
[code=c/c++]
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// FAT32 File IO Library
// V2.0
// Rob Riglar
// Copyright 2003 - 2007
//
// Email: rob@robriglar.com
//
//-----------------------------------------------------------------------------
//
// This file is part of FAT32 File IO Library.
//
// FAT32 File IO Library is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// FAT32 File IO Library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FAT32 File IO Library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#include "..\define.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>

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

#ifdef SOURCE_WINDOWS_PHYSICAL_DRIVE

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

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

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


if (hDevice == INVALID_HANDLE_VALUE)
return FALSE;

DiskFileSeek(hDevice, (startinglogicalsector),FILE_BEGIN);

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

CloseHandle(hDevice);
return TRUE;
}
//---------------------------------------------------------------------
// WriteDiskSector: Write disk sector (512 bytes) in Windows
//---------------------------------------------------------------------
extern "C" BOOL WriteDiskSector(int drive, DWORD startinglogicalsector, int numberofsectors, BYTE *buffer)
{
HANDLE hDevice;
DWORD byteswritten;

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


if (hDevice == INVALID_HANDLE_VALUE)
return FALSE;

DiskFileSeek(hDevice, (startinglogicalsector),FILE_BEGIN);

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

CloseHandle(hDevice);
return TRUE;
}
//---------------------------------------------------------------------
// DiskFileSeek: Allow seeking through large files (a disk opened as a
// file).
//---------------------------------------------------------------------
static __int64 DiskFileSeek(HANDLE hf,__int64 distance, DWORD MoveMethod)
{
__int64 seekDistance=0;
seekDistance = distance*512;
LARGE_INTEGER li;
li.QuadPart = seekDistance;
li.LowPart = SetFilePointer (hf, li.LowPart, &li.HighPart, MoveMethod);
return li.QuadPart;
}

#endif
[/code]
pla_007 2009-10-16
  • 打赏
  • 举报
回复
这是google代码搜索里找的一个示例,可以参考一下。


//---------------------------------------------------------------------
// WriteDiskSector: Write disk sector (512 bytes) in Windows
//---------------------------------------------------------------------
extern "C" BOOL WriteDiskSector(int drive, DWORD startinglogicalsector, int numberofsectors, BYTE *buffer)
{
HANDLE hDevice;
DWORD byteswritten;

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


if (hDevice == INVALID_HANDLE_VALUE)
return FALSE;

DiskFileSeek(hDevice, (startinglogicalsector),FILE_BEGIN);

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

CloseHandle(hDevice);
return TRUE;
}

pla_007 2009-10-16
  • 打赏
  • 举报
回复
用WinObj可以看到磁盘的设备连接符。
matrix2009 2009-10-16
  • 打赏
  • 举报
回复
To:zwd2009101
我是菜鸟 呵呵
不想去深圳
估计会一直在北京

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


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

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

matrix2009 2009-10-16
  • 打赏
  • 举报
回复
谢谢pla_007
我已经搞定了。
不过不是采用你的方法
你的方法有局限性
不能区分是本地硬盘、移动硬盘或是U盘。
西山小月 2009-10-15
  • 打赏
  • 举报
回复
为什么不用DeviceIoControl
zwd2009101 2009-10-15
  • 打赏
  • 举报
回复
楼主很强大,是否有兴趣到我们公司工作:
深圳知名通信企业,高薪诚聘底层软件设计师、工程师,
4年以上嵌入式系统开发设计经验;
熟悉PowerPC/mips/ARM/X86至少两种或以上处理器架构;
熟悉CPU的多核、多线程、cache、MMU等常用硬件技术,深入了解嵌入式OS(VXWORKS,linux等);
熟悉常用硬件单元或芯片,如网口,flash,硬件加速引擎的软件驱动技术;
如果有意向可简历发到:
zwd2009101@126.com
matrix2009 2009-10-15
  • 打赏
  • 举报
回复
在我的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读取的是逻辑扇区?
请高手指点

matrix2009 2009-10-15
  • 打赏
  • 举报
回复
在应用层向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

21,595

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 驱动开发/核心开发
社区管理员
  • 驱动开发/核心开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧