关于拷贝硬盘代码的问题 ( 积分: 90 )

  • 主题发起人 主题发起人 xf47
  • 开始时间 开始时间
X

xf47

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在在WIN2000下进行一个物理硬盘到另一个物理硬盘的按磁道拷贝。但利用下面代码进行硬盘拷贝时,拷贝出的硬盘出现盘符与源盘不符(目标盘容量大于源盘容量),中文目录出现乱码的现象,请高手帮忙看一下代码那里有错误。<br>procedure TThread_Disk.ReadSector(DeviceNum,TagetNum,EndSector,BytesPerSector:integer);<br>var<br> &nbsp;SectorCount:integer;<br> &nbsp;SourceDrive:PChar;<br> &nbsp;TagetDrive:Pchar;<br> &nbsp;str:string;<br> &nbsp;p:pchar;<br> &nbsp;SectorStart:integer;<br> &nbsp;repeatcount,i:integer;<br>begin<br> &nbsp;SectorCount:=256;//一次拷贝256个扇区<br> &nbsp;SourceDrive:=PChar('//./PHYSICALDRIVE'+IntToStr(DeviceNum));//源盘<br> &nbsp;TagetDrive:=PChar('//./PHYSICALDRIVE'+IntToStr(TagetNum));//目标盘<br> &nbsp;hDeviceHandle := CreateFile(SourceDrive, GENERIC_READ, &nbsp;//如果只是读扇区,可以用GENERIC_READ<br> &nbsp; &nbsp;FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);<br> &nbsp;tDeviceHandle:= CreateFile(TagetDrive, GENERIC_All,<br> &nbsp; &nbsp;FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);<br><br> &nbsp;if (hDeviceHandle &lt;&gt; INVALID_HANDLE_VALUE) and (tDeviceHandle &lt;&gt; INVALID_HANDLE_VALUE)then<br> &nbsp;begin<br> &nbsp;try<br> &nbsp; &nbsp;SectorStart:=0; &nbsp;//从第一个扇区开始<br> &nbsp; &nbsp;RepeatCount:=(EndSector-SectorStart) div SectorCount;<br> &nbsp; &nbsp;P:=allocmem(SectorCount*BytesPerSector); //申请256个扇区的内存空间<br> &nbsp; &nbsp;for i:=0 to repeatcount do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;FileSeek(hDevicehandle,SectorStart*BytesPerSector,0); //查找指定扇区数据<br> &nbsp; &nbsp; &nbsp;if FileRead(hDevicehandle,p[0],SectorCount*BytesPerSector)&lt;&gt;SectorCount*BytesPerSector then<br> &nbsp; &nbsp; &nbsp; Break; &nbsp; //如果读取数不对,停止 <br><br> &nbsp; &nbsp; FileSeek(tDevicehandle,SectorStart*BytesPerSector,0);<br> &nbsp; &nbsp; if FileWrite(tDevicehandle,p[0],SectorCount*BytesPerSector)&lt;&gt;SectorCount*BytesPerSector then<br> &nbsp; &nbsp; &nbsp; break; &nbsp; &nbsp;//如果写入数不对,停止<br> &nbsp; &nbsp; &nbsp;Inc(SectorStart,SectorCount);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;freemem(p,SectorCount*BytesPerSector);<br><br> &nbsp; &nbsp;//剩余的扇区不到256个扇区时候<br> &nbsp; &nbsp;repeatcount:=(EndSector-StartSector) mod SectorCount;<br> &nbsp; &nbsp;if repeatcount&lt;&gt;0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;P:=allocmem(repeatcount*BytesPerSector); //申请新的内存空间<br> &nbsp; &nbsp; &nbsp;FileSeek(hDevicehandle,SectorStart*BytesPerSector,0); //查找制定扇区数据<br> &nbsp; &nbsp; &nbsp;if FileRead(hDevicehandle,p[0],repeatcount*BytesPerSector)&lt;&gt;repeatcount*BytesPerSector then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;FileSeek(tDevicehandle,SectorStart*BytesPerSector,0);<br> &nbsp; &nbsp; &nbsp;if FileWrite(tDevicehandle,p[0],repeatcount*BytesPerSector)&lt;&gt;repeatcount*BytesPerSector then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;freemem(p,repeatcount*BytesPerSector);<br> &nbsp; &nbsp;end;<br><br> &nbsp;finally<br> &nbsp; &nbsp;closehandle(hDeviceHandle);<br> &nbsp; &nbsp;closehandle(tDeviceHandle);<br> &nbsp;end;<br>end;<br>end;
 
我想在在WIN2000下进行一个物理硬盘到另一个物理硬盘的按磁道拷贝。但利用下面代码进行硬盘拷贝时,拷贝出的硬盘出现盘符与源盘不符(目标盘容量大于源盘容量),中文目录出现乱码的现象,请高手帮忙看一下代码那里有错误。<br>procedure TThread_Disk.ReadSector(DeviceNum,TagetNum,EndSector,BytesPerSector:integer);<br>var<br> &nbsp;SectorCount:integer;<br> &nbsp;SourceDrive:PChar;<br> &nbsp;TagetDrive:Pchar;<br> &nbsp;str:string;<br> &nbsp;p:pchar;<br> &nbsp;SectorStart:integer;<br> &nbsp;repeatcount,i:integer;<br>begin<br> &nbsp;SectorCount:=256;//一次拷贝256个扇区<br> &nbsp;SourceDrive:=PChar('//./PHYSICALDRIVE'+IntToStr(DeviceNum));//源盘<br> &nbsp;TagetDrive:=PChar('//./PHYSICALDRIVE'+IntToStr(TagetNum));//目标盘<br> &nbsp;hDeviceHandle := CreateFile(SourceDrive, GENERIC_READ, &nbsp;//如果只是读扇区,可以用GENERIC_READ<br> &nbsp; &nbsp;FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);<br> &nbsp;tDeviceHandle:= CreateFile(TagetDrive, GENERIC_All,<br> &nbsp; &nbsp;FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);<br><br> &nbsp;if (hDeviceHandle &lt;&gt; INVALID_HANDLE_VALUE) and (tDeviceHandle &lt;&gt; INVALID_HANDLE_VALUE)then<br> &nbsp;begin<br> &nbsp;try<br> &nbsp; &nbsp;SectorStart:=0; &nbsp;//从第一个扇区开始<br> &nbsp; &nbsp;RepeatCount:=(EndSector-SectorStart) div SectorCount;<br> &nbsp; &nbsp;P:=allocmem(SectorCount*BytesPerSector); //申请256个扇区的内存空间<br> &nbsp; &nbsp;for i:=0 to repeatcount do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;FileSeek(hDevicehandle,SectorStart*BytesPerSector,0); //查找指定扇区数据<br> &nbsp; &nbsp; &nbsp;if FileRead(hDevicehandle,p[0],SectorCount*BytesPerSector)&lt;&gt;SectorCount*BytesPerSector then<br> &nbsp; &nbsp; &nbsp; Break; &nbsp; //如果读取数不对,停止 <br><br> &nbsp; &nbsp; FileSeek(tDevicehandle,SectorStart*BytesPerSector,0);<br> &nbsp; &nbsp; if FileWrite(tDevicehandle,p[0],SectorCount*BytesPerSector)&lt;&gt;SectorCount*BytesPerSector then<br> &nbsp; &nbsp; &nbsp; break; &nbsp; &nbsp;//如果写入数不对,停止<br> &nbsp; &nbsp; &nbsp;Inc(SectorStart,SectorCount);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;freemem(p,SectorCount*BytesPerSector);<br><br> &nbsp; &nbsp;//剩余的扇区不到256个扇区时候<br> &nbsp; &nbsp;repeatcount:=(EndSector-StartSector) mod SectorCount;<br> &nbsp; &nbsp;if repeatcount&lt;&gt;0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;P:=allocmem(repeatcount*BytesPerSector); //申请新的内存空间<br> &nbsp; &nbsp; &nbsp;FileSeek(hDevicehandle,SectorStart*BytesPerSector,0); //查找制定扇区数据<br> &nbsp; &nbsp; &nbsp;if FileRead(hDevicehandle,p[0],repeatcount*BytesPerSector)&lt;&gt;repeatcount*BytesPerSector then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;FileSeek(tDevicehandle,SectorStart*BytesPerSector,0);<br> &nbsp; &nbsp; &nbsp;if FileWrite(tDevicehandle,p[0],repeatcount*BytesPerSector)&lt;&gt;repeatcount*BytesPerSector then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;freemem(p,repeatcount*BytesPerSector);<br> &nbsp; &nbsp;end;<br><br> &nbsp;finally<br> &nbsp; &nbsp;closehandle(hDeviceHandle);<br> &nbsp; &nbsp;closehandle(tDeviceHandle);<br> &nbsp;end;<br>end;<br>end;
 
我得可用分数没了,只能给这点了。希望大家帮忙
 
分不是问题,只是我不会,会有高手来的,放心吧
 
谢谢老哥!!<br>在WIN2000下CREATEFILE不是可以把物理硬盘当作一个文件来操作吗?<br>而且我读取的扇区内容跟用WINHEX读取的扇区内容是相同的,为什么写到另一个硬盘就有问题呢?
 
请高手帮忙看一下
 

Similar threads

后退
顶部