Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe the problem is with the driver that serves the COM port. Hence - there'll be no API to "actually close" the COM port.</p> <p>BTW, after you close the file handle, there's no need to wait for all oustanding I/Os to complete with errors. At the time <code>CloseHandle</code> returns all outstanding I/Os are <strong>already</strong> completed/cancelled, you just receive the callbacks asynchronously (be it via completion port or APC queue, no matter).</p> <p>Specifically FTDI drivers (those that emulate COM->USB) are known to be very glitchy.</p> <p>I may only recommend to try flushing the data before closing the handle. You may wait for all I/Os to complete <strong>before</strong> closing the COM port (if this is applicable for your case). Alternatively you may call <code>SetCommMask</code> and <code>WaitCommEvent</code> to ensure there's no send data pending. Hopefully this may help.</p> <p><strong>EDIT:</strong></p> <p>Does <code>CloseHandle</code> immediately (before it returns) cancel all the pending I/Os on the file handle?</p> <p>Strictly speaking - <strong>no</strong>.</p> <p>There may be other references left to the file object. For example, a user-mode code may call <code>DuplicateHandle</code>, or a kernel-mode driver may call <code>ObReferenceObjectByXXXX</code>. In such a case the object the handle refers to is not necessarily released.</p> <p>When the last handle is closed the driver's <code>DispatchCleanup</code> is called. It must cancel all the outstanding I/Os according to <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff543242%28v=vs.85%29.aspx" rel="nofollow">this</a>.</p> <p>However while one thread is within a <code>CloseHandle</code> - you may theoretically issue another I/O from another thread (if you're lucky - the handle will still be valid). While you're in the call to an I/O function (such as <code>WriteFile</code> or etc.) the OS temporarily increases the reference counter to the object. This in turn may start another I/O, which won't be cancelled directly by the <code>CloseHandle</code> invocation.</p> <p>However in this case the handle will be closed by the O/S immediately after the new I/O is issued, because the reference count to the object reached 0 again.</p> <p>So that in such a perverted scenario there may happen a situation where immediately after a call to <code>CloseHandle</code> you're unable to reopen the file again.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload