Windows CE下读写U盘的第一个扇区(进者有分)
在Windows XP系统下,我通过如下代码(VB编写)
可实现向U盘(映射到我的XP系统下之后,磁盘目录为“K”)
的第一个扇区写入数据(这里写入的为0)
这样就破坏了U盘的引导程序(或者破话了U盘的文件系统)
使得U盘已经无法正常使用
代码如下,我已经验证过:
——————————————————————————
Public Shared Function WriteUDisk(ByVal id As Byte) As String Dim ReturnStr As String = "ok" '<1>打开U盘并返回U盘的句柄 Dim drivename As String = String.Concat("\\.\K", ":") Dim device As Integer = CreateFile(drivename, generic_read Or generic_write, 0, 0, open_existing, 0, 0) If device = invalid_handle_value Then CloseHandle(device) ReturnStr = "打开U盘" + id.ToString + "错" Return ReturnStr End If '<2>定位写指针 Dim MyPointer As Int32 Try MyPointer = SetFilePointer(device, 0, 0, 0) Catch ex As Exception CloseHandle(device) ReturnStr = "定位U盘" + id.ToString + "指针错。" + ex.Message.ToString Return ReturnStr End Try '<2>写一个扇区的数据到U盘 Dim MyByteNumber As Integer Dim bBuffer(511) As Byte Dim i As Integer Dim MyResult As Integer bBuffer(0) = CByte(Asc("S")) bBuffer(1) = CByte(Asc("J")) bBuffer(2) = CByte(Asc("M")) For i = 3 To 511 bBuffer(i) = &H0 Next Try MyResult = WriteFile(device, bBuffer(0), 512, MyByteNumber, 0) Catch ex As Exception CloseHandle(device) ReturnStr = "写U盘" + id.ToString + "错。" + ex.Message.ToString Return ReturnStr End Try 'MyResult = FlushFileBuffers(device) If MyResult = 0 Then CloseHandle(device) ReturnStr = "U盘" + id.ToString + "错。" Return ReturnStr Else CloseHandle(device) ReturnStr = "写U盘" + id.ToString + "成功。" Return ReturnStr End If End FunctionEnd Class
void CURWDlg::OnBnClickedButton1(){ // TODO: 在此添加控件通知处理程序代码 LPDWORD lpFileSizeHigh; hFile = CreateFile(_T("DSK1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL); if (hFile == INVALID_HANDLE_VALUE) { MessageBox(_T("在SD/MMC卡上创建文件失败!")); return; } else { MessageBox(_T("在SD/MMC卡上创建文件成功!")); } DWORD pFile=SetFilePointer(hFile,0,NULL,FILE_BEGIN); if(pFile == INVALID_SET_FILE_POINTER) { MessageBox(_T("指针定位失败!")); } else { MessageBox(_T("指针定位成功!")); } BYTE DataT[511]; for(int i=0;i<512;i++) { DataT[i]=0x00; } DWORD ActNum; BOOL TF = WriteFile(hFile,&DataT[0],512,&ActNum,NULL); if(TF == TRUE) { MessageBox(_T("写文件成功!")); } else { MessageBox(_T("写文件失败!")); } CloseHandle(hFile);}
Public Shared Function WriteUDisk(ByVal id As Byte) As String Dim ReturnStr As String = "ok" '<1>打开U盘并返回U盘的句柄 Dim drivename As String = String.Concat("DSK", id.ToString() + ":") 'Dim device As Integer = CreateFile(drivename, generic_read Or generic_write, file_share_read Or file_share_write, 0, open_existing, 0, 0) Dim device As Integer = CreateFile(drivename, generic_write, file_share_write, 0, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, 0) If device = invalid_handle_value Then CloseHandle(device) ReturnStr = "打开U盘" + id.ToString + "错" Return ReturnStr End If '<2>定位写指针 Dim MyPointer As Integer Try MyPointer = SetFilePointer(device, 0, 0, 0) Catch ex As Exception CloseHandle(device) ReturnStr = "定位U盘" + id.ToString + "指针错。" + ex.Message.ToString Return ReturnStr End Try If MyPointer = 0 Then CloseHandle(device) ReturnStr = "定位U盘" + id.ToString + "错。" Return ReturnStr End If '<2>写一个扇区的数据到U盘 Dim bBuffer(511) As Byte Dim MyByteNumber As Integer Dim i As Integer Dim MyResult As Integer bBuffer(0) = CByte(Asc("S")) bBuffer(1) = CByte(Asc("J")) bBuffer(2) = CByte(Asc("M")) For i = 3 To 511 bBuffer(i) = &HFF Next Try MyResult = WriteFile(device, bBuffer(0), 512, MyByteNumber, 0) Catch ex As Exception CloseHandle(device) ReturnStr = "写U盘" + id.ToString + "指令错。" + ex.Message.ToString Return ReturnStr End Try If MyResult = 0 Then CloseHandle(device) ReturnStr = "写U盘" + id.ToString + "错。" Return ReturnStr Else CloseHandle(device) ReturnStr = "写U盘" + id.ToString + "成功。" Return ReturnStr End If End Function
如果一定要写,调用驱动里读写block的deviceioctol可以访问
[解决办法]
你的win7 哪个版本的啊?
实在不行,到网上找个第三方软件,做web服务器
[解决办法]
关注
[解决办法]
mark一下
[解决办法]
暂时不懂,顶下
[解决办法]
接分
[解决办法]
学习了,帮顶~~
[解决办法]
学习,关注中.....
顶帖
[解决办法]
不同操作系统的驱动程序有差异。
用 DeviceIoControl 函数发送 Mass storage device bulk SCSI 协议命令就可以了。
[解决办法]
不错