关于一段硬盘拷贝代码的问题。 ( 积分: 100 )

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

xf47

Unregistered / Unconfirmed
GUEST, unregistred user!
来由:我想在WINDOWS2000/XP下对2个物理硬盘进行按磁道拷贝,查了一下资料说WIN2000/XP下物理硬盘可以当作一个FILE来进行操作,因此写了一下代码。<br>问题:复制完毕的硬盘可以看到主分区C和扩展分区,但看不到逻辑分区(d、E、F等),在主分区C中有些文件夹名出现问题打不开,而且有些文件也存在乱码的问题。按道理说我做的是按磁道拷贝,拷贝完后的硬盘与源盘应该一摸一样才对啊,望高手解惑!!<br>代码:<br>var<br> &nbsp;SectorCount:integer; //每次读取扇区个数<br> &nbsp;SourceDrive:PChar; &nbsp;//源盘<br> &nbsp;TagetDrive:Pchar; &nbsp; &nbsp;//目标盘<br> &nbsp;P:Pointer; &nbsp; &nbsp; &nbsp; //缓冲区<br> &nbsp;SectorStart:integer; //开始复制的扇区数<br> &nbsp;repeatcount,i:integer;<br>begin<br> &nbsp;SectorCount:=256; //一次复制256个扇区<br> &nbsp;SourceDrive:=PChar('//./PHYSICALDRIVE0'); &nbsp;//源盘为第一个物理硬盘<br> &nbsp;TagetDrive:=PChar('//./PHYSICALDRIVE1'); &nbsp;//目标盘为第二个物理硬盘<br><br> &nbsp;hDeviceHandle := CreateFile(SourceDrive, &nbsp;//源盘句柄<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GENERIC_READ, &nbsp;//如果只是读扇区,可以用GENERIC_READ<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);<br> &nbsp;tDeviceHandle:= CreateFile(TagetDrive, &nbsp; //目标盘句柄<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GENERIC_All, &nbsp;//如果只是读扇区,可以用GENERIC_READ<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp;//循环次数<br> &nbsp; &nbsp;P:=allocmem(SectorCount*BytesPerSector+1); //申请新的内存空间<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^,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^,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+1);<br><br><br><br> &nbsp; &nbsp;//剩余的字节数不到一个标准缓冲时候<br> &nbsp; &nbsp;repeatcount:=(EndSector-StartSector) mod SectorCount;<br> &nbsp; &nbsp;if repeatcount&lt;&gt;0 then &nbsp; &nbsp; //如果剩余的扇区数不足SectorCount个扇区时,复制剩余扇区<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;P:=allocmem(repeatcount*BytesPerSector+1); //申请新的内存空间<br> &nbsp; &nbsp; &nbsp;FileSeek(hDevicehandle,SectorStart*BytesPerSector,0); //查找制定扇区数据<br> &nbsp; &nbsp; &nbsp;if FileRead(hDevicehandle,p^,repeatcount*BytesPerSector)&lt;&gt;repeatcount*BytesPerSector then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Self.Terminate;<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;FileSeek(tDevicehandle,SectorStart*BytesPerSector,0);<br> &nbsp; &nbsp; if FileWrite(tDevicehandle,p^,repeatcount*BytesPerSector)&lt;&gt;repeatcount*BytesPerSector then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Self.Terminate;<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;freemem(p,repeatcount*BytesPerSector+1);<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;
 
来由:我想在WINDOWS2000/XP下对2个物理硬盘进行按磁道拷贝,查了一下资料说WIN2000/XP下物理硬盘可以当作一个FILE来进行操作,因此写了一下代码。<br>问题:复制完毕的硬盘可以看到主分区C和扩展分区,但看不到逻辑分区(d、E、F等),在主分区C中有些文件夹名出现问题打不开,而且有些文件也存在乱码的问题。按道理说我做的是按磁道拷贝,拷贝完后的硬盘与源盘应该一摸一样才对啊,望高手解惑!!<br>代码:<br>var<br> &nbsp;SectorCount:integer; //每次读取扇区个数<br> &nbsp;SourceDrive:PChar; &nbsp;//源盘<br> &nbsp;TagetDrive:Pchar; &nbsp; &nbsp;//目标盘<br> &nbsp;P:Pointer; &nbsp; &nbsp; &nbsp; //缓冲区<br> &nbsp;SectorStart:integer; //开始复制的扇区数<br> &nbsp;repeatcount,i:integer;<br>begin<br> &nbsp;SectorCount:=256; //一次复制256个扇区<br> &nbsp;SourceDrive:=PChar('//./PHYSICALDRIVE0'); &nbsp;//源盘为第一个物理硬盘<br> &nbsp;TagetDrive:=PChar('//./PHYSICALDRIVE1'); &nbsp;//目标盘为第二个物理硬盘<br><br> &nbsp;hDeviceHandle := CreateFile(SourceDrive, &nbsp;//源盘句柄<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GENERIC_READ, &nbsp;//如果只是读扇区,可以用GENERIC_READ<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);<br> &nbsp;tDeviceHandle:= CreateFile(TagetDrive, &nbsp; //目标盘句柄<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GENERIC_All, &nbsp;//如果只是读扇区,可以用GENERIC_READ<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp;//循环次数<br> &nbsp; &nbsp;P:=allocmem(SectorCount*BytesPerSector+1); //申请新的内存空间<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^,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^,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+1);<br><br><br><br> &nbsp; &nbsp;//剩余的字节数不到一个标准缓冲时候<br> &nbsp; &nbsp;repeatcount:=(EndSector-StartSector) mod SectorCount;<br> &nbsp; &nbsp;if repeatcount&lt;&gt;0 then &nbsp; &nbsp; //如果剩余的扇区数不足SectorCount个扇区时,复制剩余扇区<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;P:=allocmem(repeatcount*BytesPerSector+1); //申请新的内存空间<br> &nbsp; &nbsp; &nbsp;FileSeek(hDevicehandle,SectorStart*BytesPerSector,0); //查找制定扇区数据<br> &nbsp; &nbsp; &nbsp;if FileRead(hDevicehandle,p^,repeatcount*BytesPerSector)&lt;&gt;repeatcount*BytesPerSector then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Self.Terminate;<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;FileSeek(tDevicehandle,SectorStart*BytesPerSector,0);<br> &nbsp; &nbsp; if FileWrite(tDevicehandle,p^,repeatcount*BytesPerSector)&lt;&gt;repeatcount*BytesPerSector then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Self.Terminate;<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;freemem(p,repeatcount*BytesPerSector+1);<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;
 
高手帮忙啊!
 
再顶一下子^_^
 
我也想知道~~帮你顶了`~~~
 
后退
顶部