invalid pointer operation 问题(100分)

  • 主题发起人 主题发起人 zhxdiannao
  • 开始时间 开始时间
Z

zhxdiannao

Unregistered / Unconfirmed
GUEST, unregistred user!
我做了一个程序目的是在pe启动光盘环境下修复硬盘主引导记录<br>过程是先读取0扇区内容和要恢复的MBR样本文件(三个扇区大小)<br>然后用现在硬盘的分区表替换MBR样本文件的分区表,<br>即0扇区的从447开始的64字节<br>现在程序可以达到目的,就是出现invalid pointer operation提示<br>还有,每次运行只能点击一次,再次点击就出现自定义的'Write错误'<br>我写的一个备份MBR的小程序也是只能备份一次,再次点击就提示<br>要写入的文件正在被使用。<br><br>procedure TForm1.SpeedButton14Click(Sender: TObject);<br>const<br> &nbsp;BytesPerSector=512;<br> &nbsp;SectorCount=3;<br> &nbsp;SectorStart=0;<br> &nbsp;drive='//./PHYSICALDRIVE0';<br> &nbsp;var<br> &nbsp;str:string;<br> &nbsp;phd:pchar;<br> &nbsp;pfile:pchar;<br> &nbsp;//i:integer;<br> &nbsp;//tempstream:Tfilestream;<br> &nbsp;tempstream2:Tfilestream;<br>begin<br>//读取硬盘0扇区内容,获取分区表信息<br>hDeviceHandle := CreateFile(drive, GENERIC_ALL, &nbsp;//如果只是读扇区,可以用GENERIC_READ<br> &nbsp; &nbsp;FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);<br> &nbsp;if (hDeviceHandle &lt;&gt; INVALID_HANDLE_VALUE) then<br> &nbsp;begin<br> &nbsp; &nbsp;phd:=allocmem(1*512);<br> &nbsp; &nbsp;FileSeek(hDevicehandle,0*512,0);<br> &nbsp; &nbsp;if FileRead(hDevicehandle,phd[0],1*512)&lt;&gt;1*512 then<br> &nbsp; &nbsp; &nbsp; raise exception.create('Read错误');<br> &nbsp;end ;<br>memo1.text:=memo1.text+#13#10+'读取硬盘0扇区内容';<br>//读取文件内容<br> &nbsp;str:= exepath+'/yjhumbr.bin';<br> hDeviceHandle2 := CreateFile(drive, GENERIC_ALL, &nbsp;//如果只是读扇区,可以用GENERIC_READ<br> &nbsp; &nbsp; &nbsp; &nbsp;FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);<br> &nbsp; &nbsp; &nbsp;if (hDeviceHandle2 &lt;&gt; INVALID_HANDLE_VALUE) then<br> &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pfile:=allocmem(SectorCount*BytesPerSector);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileSeek(hDevicehandle,SectorStart*BytesPerSector,0);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempstream2:=Tfilestream.Create(str,fmopenread);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempstream2.Read(pfile[0],SectorCount*BytesPerSector);<br> &nbsp; &nbsp; &nbsp; end;<br>memo1.text:=memo1.text+#13#10+'读取文件内容';<br><br>//以硬盘现有分区表信息替换文件中的分区表信息<br>copymemory(@phd[446],@pfile[446],64);*****这一句会出错,应该写呢,我想替换64字节<br>memo1.text:=memo1.text+#13#10+'分区表信息替换';<br><br>//修改后的MBR写入硬盘0扇区<br><br>FileSeek(hDevicehandle,SectorStart*BytesPerSector,0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if FileWrite(hDevicehandle,pfile[0],SectorCount*BytesPerSector)<br><br>&lt;&gt;SectorCount*BytesPerSector then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise exception.create('Write错误');<br>memo1.text:=memo1.text+#13#10+'MBR写入硬盘0扇区';<br><br>//释放资源<br>freemem(phd,SectorCount*BytesPerSector);<br>freemem(pfile,SectorCount*BytesPerSector);<br><br>closehandle(hDeviceHandle2);<br>closehandle(hDeviceHandle);<br>end;
 
后退
顶部