-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from exvito/get-ov-result
GetOverlappedResult implementation
- Loading branch information
Showing
5 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Overlapped | ||
---------- | ||
A module containing Windows functions for working with OVERLAPPED objects. | ||
""" | ||
|
||
from pywincffi.core import dist | ||
from pywincffi.core.checks import NON_ZERO, input_check, error_check | ||
from pywincffi.wintypes import HANDLE, OVERLAPPED, wintype_to_cdata | ||
|
||
|
||
def GetOverlappedResult(hFile, lpOverlapped, bWait): | ||
""" | ||
Retrieves the results of an overlapped operation on the specified file, | ||
named pipe, or communications device. To specify a timeout interval or | ||
wait on an alertable thread, use GetOverlappedResultEx. | ||
.. seealso:: | ||
https://msdn.microsoft.com/en-us/library/ms683209 | ||
:param pywincffi.wintypes.HANDLE hFile: | ||
A handle to the file, named pipe, or communications device. | ||
This is the same handle that was specified when the overlapped | ||
operation was started by a call to the ReadFile, WriteFile, | ||
ConnectNamedPipe, TransactNamedPipe, DeviceIoControl, or WaitCommEvent | ||
function. | ||
:param pywincffi.wintypes.OVERLAPPED lpOverlapped: | ||
The an OVERLAPPED object that was specified when the overlapped | ||
operation was started | ||
:param bool bWait: | ||
If this parameter is TRUE, and the Internal member of the lpOverlapped | ||
structure is STATUS_PENDING, the function does not return until the | ||
operation has been completed. If this parameter is FALSE and the | ||
operation is still pending, the function returns FALSE and the | ||
GetLastError function returns ERROR_IO_INCOMPLETE | ||
:returns: | ||
The number of bytes that were actually transferred by a read or write | ||
operation. For a TransactNamedPipe operation, this is the number of | ||
bytes that were read from the pipe. For a DeviceIoControl operation, | ||
this is the number of bytes of output data returned by the device | ||
driver. For a ConnectNamedPipe or WaitCommEvent operation, this value | ||
is undefined. | ||
""" | ||
input_check("hFile", hFile, HANDLE) | ||
input_check("lpOverlapped", lpOverlapped, OVERLAPPED) | ||
input_check("bWait", bWait, allowed_values=(True, False)) | ||
|
||
ffi, library = dist.load() | ||
|
||
lpNumberOfBytesTransferred = ffi.new("DWORD[1]") | ||
|
||
result = library.GetOverlappedResult( | ||
wintype_to_cdata(hFile), | ||
wintype_to_cdata(lpOverlapped), | ||
lpNumberOfBytesTransferred, | ||
ffi.cast("BOOL", bWait), | ||
) | ||
|
||
error_check("GetOverlappedResult", result, NON_ZERO) | ||
|
||
return int(lpNumberOfBytesTransferred[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import os | ||
import shutil | ||
import tempfile | ||
|
||
from six import text_type | ||
|
||
from pywincffi.dev.testutil import TestCase | ||
|
||
from pywincffi.core import dist | ||
|
||
from pywincffi.kernel32 import ( | ||
CreateFile, WriteFile, CloseHandle, CreateEvent, GetOverlappedResult) | ||
from pywincffi.wintypes import OVERLAPPED | ||
|
||
|
||
class TestOverlappedWriteFile(TestCase): | ||
""" | ||
Tests for :func:`pywincffi.kernel32.GetOverlappedResult` | ||
""" | ||
def test_overlapped_write_file(self): | ||
# Test outline: | ||
# - Create a temp dir. | ||
# - CreateFile for writing with FILE_FLAG_OVERLAPPED. | ||
# - WriteFile in overlapped mode. | ||
# - Use GetOverlappedResult to wait for IO completion. | ||
|
||
temp_dir = tempfile.mkdtemp(prefix="pywincffi-test-ovr-") | ||
self.addCleanup(shutil.rmtree, temp_dir, ignore_errors=True) | ||
|
||
filename = text_type(os.path.join(temp_dir, "overlapped-write-file")) | ||
file_contents = b"hello overlapped world" | ||
|
||
_, lib = dist.load() | ||
handle = CreateFile( | ||
lpFileName=filename, | ||
dwDesiredAccess=lib.GENERIC_WRITE, | ||
dwCreationDisposition=lib.CREATE_NEW, | ||
dwFlagsAndAttributes=lib.FILE_FLAG_OVERLAPPED, | ||
) | ||
|
||
# Prepare overlapped write | ||
ovr = OVERLAPPED() | ||
ovr.hEvent = CreateEvent(bManualReset=True, bInitialState=False) | ||
|
||
# Go for overlapped WriteFile. Should result in: | ||
# - num_bytes_written == 0 | ||
# - GetLastError() == ERROR_IO_PENDING | ||
|
||
# HOWEVER, https://msdn.microsoft.com/en-us/library/aa365683 states: | ||
# "Further, the WriteFile function will sometimes return TRUE with a | ||
# GetLastError value of ERROR_SUCCESS, even though it is using an | ||
# asynchronous handle (which can also return FALSE with | ||
# ERROR_IO_PENDING). | ||
# Test strategy: | ||
# - Disregard WriteFile return result. | ||
# - Assert GetLastError is either ERROR_IO_PENDING or ERROR_SUCCESS. | ||
# - Later validate that the correct number of bytes was written. | ||
|
||
_ = WriteFile(handle, file_contents, lpOverlapped=ovr) | ||
self.maybe_assert_last_error(lib.ERROR_IO_PENDING) | ||
|
||
# Block until async write is completed. | ||
num_bytes_written = GetOverlappedResult(handle, ovr, bWait=True) | ||
self.assertEqual(num_bytes_written, len(file_contents)) | ||
|
||
CloseHandle(handle) |