'CreateFile() hangs on VMWare with a disk disconnected. How do I safely abort a CreateFile attempt in such a scenario?

I am calling CreateFile to open a file for reading and writing on an external Windows volume running over VMWare. However, if the disk is disconnected for any reason (after having been previously connected), CreateFile will hang indefinitely. How can I terminate the CreateFile attempt safely?

I've tried starting a thread to do the CreateFile and then using CancelSynchronousIO, but this call also seems to block indefinitely. As far as I can tell, the only way to abort the CreateFile attempt is with TerminateThread, which I know is a messy option that is never recommended. Is TerminateThread safe to do in this instance?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source