Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <iostream>
- #include <cassert>
- // stupid crude auto close handle ;^)
- struct ct_win_hclose
- {
- HANDLE m_h;
- ct_win_hclose(HANDLE h) : m_h(h) {
- if (! m_h) throw;
- }
- ~ct_win_hclose() {
- if (! CloseHandle(m_h))
- {
- throw; // this is not good!
- }
- }
- // quick and dirty ;^o
- operator HANDLE() { assert(m_h); return m_h; }
- };
- // Simple IOCP write.
- void test()
- {
- // Create the iocp
- ct_win_hclose iocp(CreateIoCompletionPort(
- INVALID_HANDLE_VALUE,
- NULL,
- 0,
- 0
- ));
- std::cout << "iocp: " << iocp << "\n";
- // Create testfile.bin
- ct_win_hclose file(CreateFile(
- TEXT("testfile.bin"),
- GENERIC_READ | GENERIC_WRITE,
- 0,
- NULL,
- CREATE_ALWAYS,
- FILE_FLAG_OVERLAPPED,
- NULL
- ));
- std::cout << "file: " << file << "\n";
- // link file to iocp
- if (CreateIoCompletionPort(
- file,
- iocp,
- 0,
- 0) != iocp)
- {
- throw;
- }
- // Perform the initial write.
- OVERLAPPED ol_origin;
- ZeroMemory(&ol_origin, sizeof(ol_origin));
- char buf[] = "Hello World";
- if (! WriteFile(
- file,
- buf,
- sizeof(buf) - 1,
- NULL,
- &ol_origin))
- {
- DWORD lerr = GetLastError();
- if (lerr != ERROR_IO_PENDING) throw;
- }
- // Dequeue the write...
- LPOVERLAPPED ol_dequeue = NULL;
- ULONG_PTR key = 0;
- DWORD bytes = 0;
- if (! GetQueuedCompletionStatus(
- iocp,
- &bytes,
- &key,
- &ol_dequeue,
- INFINITE))
- {
- throw;
- }
- assert(key == 0);
- std::cout << "write complete:" << bytes << " bytes\n";
- }
- int main()
- {
- test();
- std::cout << "\n\nComplete, hit <ENTER> to exit!\n";
- std::fflush(stdout);
- std::cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment