OVERLAPPED结构与GetOverlappedResult函数
異步I/O調(diào)用時,我們會用到OVERLAPPED結(jié)構(gòu)和函數(shù)GetOverlappedResult。以前一直對GetOverlappedResult比較困惑,這兩天看書和代碼才知道這個函數(shù)的主要作用不過是將Overlapped返回的結(jié)果進行一次簡單的分析而已。
下面是OVERLAPPED的結(jié)構(gòu)定義:
typedef struct _OVERLAPPED {?
??? DWORD? Internal;?
??? DWORD? InternalHigh;?
??? DWORD? Offset;?
??? DWORD? OffsetHigh;?
??? HANDLE hEvent;?
} OVERLAPPED;?
這個結(jié)構(gòu)中Internal和InternalHigh是兩個返回值。寫過驅(qū)動程序的人知道這兩個值對應(yīng)著irp的IO_STATUS_BLOCK結(jié)構(gòu):
typedef struct _IO_STATUS_BLOCK {
??? union {
??????? NTSTATUS Status;
??????? PVOID Pointer;
??? };
??? ULONG_PTR Information;
} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK;
其中,Internal就是Status的值;InternalHigh就是Information的值。“Internal”這個單詞表明當初MS將這個兩個值就是內(nèi)部使用的。
而普通調(diào)用者如何知道其含義呢?
1.當調(diào)用返回時(用ReadFile舉例):
? 若Internal=0時表明返回STATUS_SUCCESS,于是ReadFile返回TRUE,即成功返回;InternalHigh的值保存在lpNumberOfBytesTransferred中。
? 若Internal!=0表示出現(xiàn)錯誤或PENDING,于是ReadFile返回FALSE, GetLastError值就是Internal值。
2.當1中返回ERROR_IO_PENDING時:
這個時候就需要用到GetOverlappedResult了。
? 若Internal=0時表明返回STATUS_SUCCESS,于是GetOverlappedResult返回TRUE,即成功返回;InternalHigh的值保存在lpNumberOfBytesTransferred中。
? 若Internal!=0表示出現(xiàn)錯誤,于是GetOverlappedResult返回FALSE, GetLastError值就是Internal值。
附源碼:
WINDOWS_2000_SOURCE_CODE\WIN2K\PRIVATE\windows\base\client\error.c
BOOL
WINAPI
GetOverlappedResult(
??? HANDLE hFile,
??? LPOVERLAPPED lpOverlapped,
??? LPDWORD lpNumberOfBytesTransferred,
??? BOOL bWait
??? )
/*++
Routine Description:
??? The?GetOverlappedResult?function returns the result of the last
??? operation that used lpOverlapped and returned ERROR_IO_PENDING.
Arguments:
??? hFile - Supplies the open handle to the file that the overlapped
??????? structure lpOverlapped was supplied to ReadFile, WriteFile,
??????? ConnectNamedPipe, WaitNamedPipe or TransactNamedPipe.
??? lpOverlapped - Points to an OVERLAPPED structure previously supplied to
??????? ReadFile, WriteFile, ConnectNamedPipe, WaitNamedPipe or
??????? TransactNamedPipe.
??????? //這個地址就是當初調(diào)用ReadFile是傳遞的參數(shù)的值,一定記住不能錯。
??? lpNumberOfBytesTransferred - Returns the number of bytes transferred
??????? by the operation.
??? bWait -? A boolean value that affects the behavior when the operation
??????? is still in progress. If TRUE and the operation is still in progress,
????????GetOverlappedResult?will wait for the operation to complete before
??????? returning. If FALSE and the operation is incomplete,
????????GetOverlappedResult?will return FALSE. In this case the extended
??????? error information available from the GetLastError function will be
??????? set to ERROR_IO_INCOMPLETE.
??????? //若當前還是ERROR_IO_PENDING則判斷是否需要無限期的等待。
Return Value:
??? TRUE -- The operation was successful, the pipe is in the
??????? connected state.
??? FALSE -- The operation failed. Extended error status is available using
??????? GetLastError.
--*/
補充:(2009-10-8)
《windows核心編程》(5th版),p293.
---------------
Internal成員:這個成員用來保存已處理的I/O請求的錯誤碼.
InternalHigh成員:當異步I/O請求完成的時候,這個成員用來保存已傳輸?shù)淖止?jié)數(shù)。
在當初設(shè)計OVERLAPPED結(jié)構(gòu)的時候,Microsoft決定不公開Internal和InternalHigh成員(名副其實)。隨著時間的推移,Microsoft認識到這些成員包含的信息會對開發(fā)人員有用,因此把它們公開了。但是,Microsoft沒有改變這些成員的名字,這是因為操作系統(tǒng)的源代碼頻繁地用到它們,而Microsoft并不想為此修改源代碼。
-------
由于Microsoft公開了這些成員,所以我們看到并不一定需要GetOverLappedResult了。:)
轉(zhuǎn)載于:https://www.cnblogs.com/Little-Star/p/7413199.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的OVERLAPPED结构与GetOverlappedResult函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NET CLR via c# 第4版笔记
- 下一篇: ajax实现highchart与数据库数