I am trying to perform an asynchronous call to DeviceIOControl with the intent that once the device has completed the request, an associated handler will be invoked.
I have a working code using winapi which works as expected. Yet the equivalent code that uses boost::asio does not work the same. The code does the following:
- Initialize an IO Completion Port (CreateIoCompletionPort / boost::asio::io_context)
- Create a device HANDLE with the OVERLAPPED_FLAG
- Associate a HANDLE to a device with the IO Completion Port (CreateIoCompletionPort / boost::asio::detail::io_context_impl::register_handle)
- Perform a single DeviceIOControl with an OVERLAPPED structure (boost::asio::windows::overlapped_ptr)
- Wait for the call to complete (GetQueuedCompletionStatus / boost::asio::io_context::run)
This is the WinAPI code (works)
auto iocp_handle = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 0);
assert(iocp_handle);
auto str = GetDeviceInterfaceInstancesList(&GUID_DEVINTERFACE_cvbkmd);
HANDLE dev_handle = CreateFileW(
str.c_str(),
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
nullptr
);
uint64_t completion_key = 3082;
assert(::CreateIoCompletionPort(dev_handle, iocp_handle, completion_key, 0));
std::string writeString = "test write\n";
std::string readString = "test read\n";
unsigned long bytes_returned;
OVERLAPPED overlapped{};
BOOL ok = DeviceIoControl(
dev_handle,
123,
writeString.data(),
writeString.size(),
readString.data(),
readString.size(),
&bytes_returned,
&overlapped
);
if (ok)
{
std::cout << "done\n";
}
else
{
auto err = GetLastError();
assert(err == ERROR_IO_PENDING);
OVERLAPPED* returned_overlapped{ nullptr };
uint64_t returned_completion_key{};
unsigned long bytes_transferred;
BOOL ok = ::GetQueuedCompletionStatus(iocp_handle,
&bytes_transferred, &returned_completion_key, &returned_overlapped,
INFINITE);
assert(ok);
assert(returned_completion_key = completion_key);
assert(returned_overlapped = &overlapped);
}
This is the boost::asio code (io_context::run hangs forever, completion handler never invoked)
boost::asio::io_context context;
auto str = GetDeviceInterfaceInstancesList(&GUID_DEVINTERFACE_cvbkmd);
HANDLE dev_handle = CreateFileW(
str.c_str(),
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
nullptr
);
auto& impl = boost::asio::use_service<boost::asio::detail::io_context_impl>(context);
boost::system::error_code ec;
impl.register_handle(dev_handle, ec);
assert(!ec);
boost::asio::windows::overlapped_ptr overlapped(
context,
[&](const boost::system::error_code& ec, std::size_t count)
{
std::cout << "done"; // never called
}
);
std::string writeString = "test write\n";
std::string readString = "test read\n";
unsigned long bytes_returned;
BOOL ok = DeviceIoControl(
dev_handle,
123,
writeString.data(),
writeString.size(),
readString.data(),
readString.size(),
&bytes_returned,
overlapped.get()
);
if (ok)
{
std::cout << "done\n";
}
else
{
auto err = GetLastError();
assert(err == ERROR_IO_PENDING);
auto completions = context.run(); // hangs forever
assert(completions == 1);
}
Questions
- Why do the 2 code segments don't act the same?
- Why does boost::asio::io_context::run hangs and the completion handler associated with the overlapped_ptr never invoked?