Created
February 22, 2014 15:47
-
-
Save kingsamchen/9156884 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
void CALLBACK OverlappedCompletionRoutine(PTP_CALLBACK_INSTANCE instance, | |
PVOID context, | |
PVOID overlapped, | |
ULONG io_result, | |
ULONG_PTR bytes_transferred, | |
PTP_IO io) | |
{ | |
auto ov = static_cast<OVERLAPPED*>(overlapped); | |
std::cout << "read " << bytes_transferred << std::endl; | |
std::cout << static_cast<char*>(context); | |
} | |
int main() | |
{ | |
HANDLE file = CreateFile(L"messed.txt", | |
GENERIC_READ, | |
FILE_SHARE_READ, | |
nullptr, | |
OPEN_EXISTING, | |
FILE_FLAG_OVERLAPPED, | |
nullptr); | |
if (file == INVALID_HANDLE_VALUE) { | |
std::cerr << "failed to open file messed.txt" << std::endl; | |
return EXIT_FAILURE; | |
} | |
ON_SCOPE_EXIT([&]{ CloseHandle(file); }); | |
char buf[MAX_PATH] = {0}; | |
PTP_IO async_io = CreateThreadpoolIo(file, OverlappedCompletionRoutine, | |
buf, nullptr); | |
ON_SCOPE_EXIT([&]{ CloseThreadpoolIo(async_io); }); | |
StartThreadpoolIo(async_io); | |
std::cout << "bind completed" << std::endl; | |
OVERLAPPED overlapped = {0}; | |
ReadFile(file, buf, 20, nullptr, &overlapped); | |
_getch(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment