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