OVERLAPPED結構與GetOverlappedResult函數

異步I/O調用時,我們會用到OVERLAPPED結構和函數GetOverlappedResult。以前一直對GetOverlappedResult比較困惑,這兩天看書和代碼才知道這個函數的主要作用不過是將Overlapped返回的結果進行一次簡單的分析而已。

下面是OVERLAPPED的結構定義:
typedef struct _OVERLAPPED {
    DWORD  Internal;
    DWORD  InternalHigh;
    DWORD  Offset;
    DWORD  OffsetHigh;
    HANDLE hEvent;
} OVERLAPPED;
這個結構中Internal和InternalHigh是兩個返回值。寫過驅動程序的人知道這兩個值對應着irp的IO_STATUS_BLOCK結構:
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將這個兩個值就是內部使用的。
而普通調用者如何知道其含義呢?
1.當調用返回時(用ReadFile舉例):
  若Internal=0時表明返回STATUS_SUCCESS,於是ReadFile返回TRUE,即成功返回;InternalHigh的值保存在lpNumberOfBytesTransferred中。
  若Internal!=0表示出現錯誤或PENDING,於是ReadFile返回FALSE, GetLastError值就是Internal值。

2.當1中返回ERROR_IO_PENDING時:
這個時候就需要用到GetOverlappedResult了。
  若Internal=0時表明返回STATUS_SUCCESS,於是GetOverlappedResult返回TRUE,即成功返回;InternalHigh的值保存在lpNumberOfBytesTransferred中。
  若Internal!=0表示出現錯誤,於是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.
        //這個地址就是當初調用ReadFile是傳遞的參數的值,一定記住不能錯。

    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.

--*/

{
    DWORD WaitReturn;

    //
    // Did caller specify an event to the original operation or was the
    // default (file handle) used?
    //

    if (lpOverlapped->Internal == (DWORD)STATUS_PENDING ) {
        if ( bWait ) {
            //
            //現在還是PENDING,且還需要等待,則無限期等待。
            //很多人會自己調用WaitForSingleObject後再調用GetOverlappedResult,其實看起來
            //沒多少必要。
            //
            WaitReturn = WaitForSingleObject(
                            ( lpOverlapped->hEvent != NULL ) ?
                                lpOverlapped->hEvent : hFile,
                            INFINITE
                            );
            }
        else {
            WaitReturn = WAIT_TIMEOUT;
            }

        if ( WaitReturn == WAIT_TIMEOUT ) {
            //  !bWait and event in not signalled state
            SetLastError( ERROR_IO_INCOMPLETE );
            return FALSE;
            }

        if ( WaitReturn != 0 ) {
             return FALSE;    // WaitForSingleObject calls BaseSetLastError
             }
        }

    *lpNumberOfBytesTransferred = (DWORD)lpOverlapped->InternalHigh;

    if ( NT_SUCCESS((NTSTATUS)lpOverlapped->Internal) ){
        return TRUE;
        }
    else {
        BaseSetLastNTError( (NTSTATUS)lpOverlapped->Internal );
        return FALSE;
        }
}


補充:(2009-10-8)

《windows核心編程》(5th版),p293.

---------------

Internal成員:這個成員用來保存已處理的I/O請求的錯誤碼.

InternalHigh成員:當異步I/O請求完成的時候,這個成員用來保存已傳輸的字節數。

在當初設計OVERLAPPED結構的時候,Microsoft決定不公開Internal和InternalHigh成員(名副其實)。隨着時間的推移,Microsoft認識到這些成員包含的信息會對開發人員有用,因此把它們公開了。但是,Microsoft沒有改變這些成員的名字,這是因爲操作系統的源代碼頻繁地用到它們,而Microsoft並不想爲此修改源代碼。

-------

由於Microsoft公開了這些成員,所以我們看到並不一定需要GetOverLappedResult了。:)

轉自:http://hi.baidu.com/calmlier/blog/item/35359dc17add0649b219a86a.html


發佈了20 篇原創文章 · 獲贊 11 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章