本文介绍了Delphi Pascal-使用SetFilePointerEx和GetFileSizeEx,以文件形式读取时获取物理介质的确切大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用RTL中没有的任何API.我一直在使用SetFilePointer和GetFileSize将物理磁盘读入缓冲区并将其转储到文件中,类似这样的事情在循环中完成了2GB以下的闪存卡的工作:

I do not know how to use any API that is not in the RTL. I have been using SetFilePointer and GetFileSize to read a Physical Disk into a buffer and dump it to a file, something like this in a loop does the job for flash memory cards under 2GB:

SetFilePointer(PD,0,nil,FILE_BEGIN);
SetLength(Buffer,512);
ReadFile(PD,Buffer[0],512,BytesReturned,nil);

但是GetFileSize的限制为2GB,SetFilePointer也是如此.我绝对不知道如何删除外部API,我查看了RTL,并在Google上搜索了许多示例,但没有找到正确的答案.

However GetFileSize has a limit at 2GB and so does SetFilePointer. I have absolutley no idea how to delcare an external API, I have looked at the RTL and googled for many examples and have found no correct answer.

我尝试过

function GetFileSizeEx(hFile: THandle; lpFileSizeHigh: Pointer): DWORD; 
    external 'kernel32';

并按照建议

function GetFileSizeEx(hFile: THandle; var FileSize: Int64): DWORD;
    stdcall; external 'kernel32';

但是,即使我使用的是有效的磁盘句柄,该函数也会返回0,而我已经确认并使用旧的API转储了数据.

But the function returns a 0 even though I am using a valid disk handle which I have confirmed and dumped data from using the older API's.

我正在使用SetFilePointer来跳跃每512个字节,并使用ReadFile写入缓冲区,反之,当我使用WriteFile将初始程序加载程序代码或其他内容写入磁盘时,可以使用它进行设置.我需要能够将文件指针设置为2gb以上.

I am using SetFilePointer to jump every 512 bytes and ReadFile to write into a buffer, in reverse I can use it to set when I am using WriteFile to write Initial Program Loader Code or something else to the disk. I need to be able to set the file pointer beyond 2gb well beyond.

有人可以帮我做一些外部声明以及对GetFileSizeEx和SetFilePointerEx的调用都起作用,以便我可以修改较旧的代码以使用4到32gb闪存卡.

Can someone help me make the external declarations and a call to both GetFileSizeEx and SetFilePointerEx that work so I can modify my older code to work with say 4 to 32gb flash cards.

推荐答案

我建议您看看这个和他的 GpHugeFile 单元,应该为您提供足够的指导获取文件大小.

I suggest that you take a look at this Primoz Gabrijelcic blog article and his GpHugeFile unit which should give you enough pointers to get the file size.

编辑1 ,根据问题的编辑,这看起来似乎是一个愚蠢的答案.

Edit 1 This looks rather a daft answer now in light of the edit to the question.

编辑2 既然已经接受了这个答案,并且对jachguate的答案进行了大量评论,所以我认为有必要总结所学到的知识.

Edit 2 Now that this answer has been accepted, following a long threads of comments to jachguate's answer, I feel it incumbent to summarise what has been learnt.

  • GetFileSize andSetFilePointer have no 2GBlimitation, they can be used on filesof essentially arbitrary size.

GetFileSizeEx SetFilePointerEx 很多易于使用,因为它们可以工作直接以64位的数量具有更简单的错误条件信号.

GetFileSizeEx andSetFilePointerEx are mucheasier to use because they workdirectly with 64 bit quantities andhave far simpler error conditionsignals.

OP实际上不需要计算他的磁盘大小.自从OP正在阅读整个磁盘内容的大小不是需要.所要做的就是顺序阅读内容,直到一无所有.

The OP did not in fact need tocalculate the size of his disk. Sincethe OP was reading the entirecontents of the disk the size was notneeded. All that was required was toread the contents sequentially untilthere was nothing left.

实际上 GetFileSize /不支持设备的句柄(例如物理磁盘或卷)为是OP要求的.更重要的是, SetFilePointer /不能寻找到这种设备的尽头句柄.

In factGetFileSize/GetFileSizeExdo not support handles to devices(e.g. a physical disk or volume) aswas requested by the OP. What's more,SetFilePointer/SetFilePointerExcannot seek to the end of such devicehandles.

为了获得一个的大小磁盘,卷或分区,一个应该通过 IOCTL_DISK_GET_LENGTH_INFO 控制代码 DeviceIoControl .

In order to obtain the size of adisk, volume or partition, one shouldpass the theIOCTL_DISK_GET_LENGTH_INFOcontrol code toDeviceIoControl.

最后,您是否需要使用 GetFileSizeEx SetFilePointerEx ,则可以将它们声明如下:

Finally, should you need to use GetFileSizeEx and SetFilePointerEx then they can be declared as follows:

function GetFileSizeEx(hFile: THandle; var lpFileSize: Int64): BOOL;
    stdcall; external 'kernel32.dll';
function SetFilePointerEx(hFile: THandle; liDistanceToMove: Int64;
    lpNewFilePointer: PInt64; dwMoveMethod: DWORD): BOOL;
    stdcall; external 'kernel32.dll';

获取这些API的一种简单方法是通过出色的 JEDI API库.

One easy way to obtain these API imports them is through the excellent JEDI API Library.

这篇关于Delphi Pascal-使用SetFilePointerEx和GetFileSizeEx,以文件形式读取时获取物理介质的确切大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 15:36