如何使用ReadFile()Windows API(C#)读取特定偏移位置的文件?

发布于 2025-02-13 20:20:31 字数 456 浏览 2 评论 0原文

以下C#摘要执行二进制文件中页面的顺序读取。

由于某些精致的原因 - 使用readfile()Windows System API是必须的。

for (iReadCounter = 0; iReadCounter < iReadCountLimit; iReadCounter++)
{
    readsize = DefineConstants.READ_BUF_SIZE;
    bool bResult = ReadFile(fhnd, readbuffer, (uint)readsize, out readresult, ref _overlapped);
    .
    .
}

是否有一种方法可以将 readfile()指向以特定的文件偏移/选择位置阅读?

谢谢。

/h

Following C# snippet executes sequential reads of pages in a binary files.

For certain delicate reasons - using ReadFile() Windows System API is a must.

for (iReadCounter = 0; iReadCounter < iReadCountLimit; iReadCounter++)
{
    readsize = DefineConstants.READ_BUF_SIZE;
    bool bResult = ReadFile(fhnd, readbuffer, (uint)readsize, out readresult, ref _overlapped);
    .
    .
}

Is there a way by which ReadFile() can be directed to read at a specific file offset/position of choice?

Thanks.

/H

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怕倦 2025-02-20 20:20:31

您将lpoverlapped readfile()的参数设置为_Overlapped变量的引用。因此,您需要使用_OVERLAPED.OFFSET_Overlapped.offsethigh字段来指定所需的文件偏移以从中读取。

per 代码> 文档:

[IN,OUT,可选] lpoverlapped

如果使用hfile参数打开的file_flag_overplapped,则需要重叠结构,否则可以为null。

如果hfilefile_flag_overlapped打开,则lpoverlapped参数必须指向有效且唯一的重叠>重叠结构,否则该功能可能会错误地报告读取操作已完成。

对于hfile支持字节偏移,如果您使用此参数,则必须指定一个字节偏移,以开始从文件或设备读取。通过设置offsetOffsethigh 重叠结构的成员。 >不支持字节偏移,offsetOffsethigh被忽略。

有关lpoverlappedfile_flag_overlapped的不同组合的更多信息,请参见“备注”部分和同步和文件位置部分。


“同步和文件位置”部分说:

如果hfile使用file_flag_overlapped打开,则是一个异步文件句柄;否则它是同步的。如前所述,使用重叠结构的规则略有不同。

...

使用异步文件的考虑:

  • ...
  • lpoverlapper参数不得无效,应考虑以下事实:
    • 尽管已在系统中设置并自动设置了重叠结构中指定的事件,但 在重叠结构中指定的偏移量不会自动更新
    • ...
    • 因为读取操作始于重叠结构readfile可能返回的读取操作。操作已完成(读取),在申请发出信号之前,均不得修改,释放或重复结构的偏移或其他部分(即,读取完成)。
    • ...



使用同步文件的考虑:

  • ...
  • 如果lpoverlapped不是null,则读取操作从重叠结构和readfile dik中指定的偏移开始直到读取操作完成后才返回。系统更新重叠偏移量之前readfile返回。
  • ...

您是否一直在设置lpoverlapped参数为null而是您必须使用 a>或 a>指定所需偏移:

使用同步文件的考虑:

  • 如果lpoverlapped为null,则读取操作在当前文件位置readfile 在操作完成之前返回,并且系统在readfile返回之前更新文件指针。
  • ...

You are setting the lpOverlapped parameter of ReadFile() to a reference to an _overlapped variable. So you need to use the _overlapped.Offset and _overlapped.OffsetHigh fields to specify the desired file offset to read from.

Per the ReadFile() documentation:

[in, out, optional] lpOverlapped

A pointer to an OVERLAPPED structure is required if the hFile parameter was opened with FILE_FLAG_OVERLAPPED, otherwise it can be NULL.

If hFile is opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must point to a valid and unique OVERLAPPED structure, otherwise the function can incorrectly report that the read operation is complete.

For an hFile that supports byte offsets, if you use this parameter you must specify a byte offset at which to start reading from the file or device. This offset is specified by setting the Offset and OffsetHigh members of the OVERLAPPED structure. For an hFile that does not support byte offsets, Offset and OffsetHigh are ignored.

For more information about different combinations of lpOverlapped and FILE_FLAG_OVERLAPPED, see the Remarks section and the Synchronization and File Position section.

And the "Synchronization and File Position" section says:

If hFile is opened with FILE_FLAG_OVERLAPPED, it is an asynchronous file handle; otherwise it is synchronous. The rules for using the OVERLAPPED structure are slightly different for each, as previously noted.

...

Considerations for working with asynchronous file handles:

  • ...
  • The lpOverlapped parameter must not be NULL and should be used with the following facts in mind:
    • Although the event specified in the OVERLAPPED structure is set and reset automatically by the system, the offset that is specified in the OVERLAPPED structure is not automatically updated.
    • ...
    • Because the read operation starts at the offset that is specified in the OVERLAPPED structure, and ReadFile may return before the system-level read operation is complete (read pending), neither the offset nor any other part of the structure should be modified, freed, or reused by the application until the event is signaled (that is, the read completes).
    • ...

Considerations for working with synchronous file handles:

  • ...
  • If lpOverlapped is not NULL, the read operation starts at the offset that is specified in the OVERLAPPED structure and ReadFile does not return until the read operation is complete. The system updates the OVERLAPPED offset before ReadFile returns.
  • ...

Had you been setting the lpOverlapped parameter to null instead, then you would have to use SetFilePointer() or SetFilePointerEx() to specify the desired offset:

Considerations for working with synchronous file handles:

  • If lpOverlapped is NULL, the read operation starts at the current file position and ReadFile does not return until the operation is complete, and the system updates the file pointer before ReadFile returns.
  • ...
零度℉ 2025-02-20 20:20:31

如果该文件实际上支持在特定位置(不是给定)的读数,则您应该能够使用重叠参数的偏移和偏高成员来指定读取位置。

即使未打开对重叠的I/O打开文件,这也应该有效。

If the file actually supports reading at a specific position (not a given), you should be able to use the Offset and OffsetHigh members of the OVERLAPPED parameter to specify the read position.

This should work even if the file was not opened for overlapped I/O.

可是我不能没有你 2025-02-20 20:20:31

我相信您应该能够使用setFilePointersetfilepointerex进行此操作。它允许您设置文件句柄指向的位置。

https://learn.microsoft.com/en-us/windows/wind32/api/fileapi/nf-fileapi-setfilepointer

I believe you should be able to do this with SetFilePointer or SetFilePointerEx. It allows you to set where the file handle is pointing at.

https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfilepointer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文