title: Is it possible to change HANDLE that has been opened for synchronous I/O to be opened for asynchronous I/O during its lifetime?
body: <p>Most of my daily programming work in Windows is nowadays around I/O operations of all kind (pipes, consoles, files, sockets, ...). I am well aware of different methods of reading and writing from/to different kinds of handles (Synchronous, asynchronous waiting for completion on events, waiting on file HANDLEs, I/O completion ports, and alertable I/O). We use many of those.</p>
  
  <p>For some of our applications it would be very useful to have only one way to treat all handles. I mean, the program may not know, what kind of handle it has received and we would like to use, let's say, I/O completion ports for all.</p>
  
  <p>So first I would ask:</p>
  
  <p>Let's assume I have a handle:</p>
  
  <pre><code>HANDLE h;
  </code></pre>
  
  <p>which has been received by my process for I/O from somewhere. Is there any easy and reliable way to find out what flags it has been created with? The main flag in question is <code>FILE_FLAG_OVERLAPPED</code>.</p>
  
  <p>The only way known to me so far, is to try to register such handle into I/O completion port (using <code>CreateIoCompletionPort()</code>). If that succeeds the handle has been created with FILE_FLAG_OVERLAPPED. But then only I/O completion port must be used, as the handle can not be unregistered from it without closing the <code>HANDLE</code> <code>h</code> itself.</p>
  
  <p>Providing there is an easy a way to determine presence of <code>FILE_FLAG_OVERLAPPED</code>, there would come my second question:</p>
  
  <p>Is there any way how to add such flag to already existing handle? That would make a handle that has been originally open for synchronous operations to be open for asynchronous. Would there be a way how to create opposite (remove the <code>FILE_FLAG_OVERLAPPED</code> to create synchronous handle from asynchronous)?</p>
  
  <p>I have not found any direct way after reading through MSDN and googling a lot. Would there be at least some trick that could do the same? Like re-creating the handle in same way using <code>CreateFile()</code> function or something similar? Something even partly documented or not documented at all?</p>
  
  <p>The main place where I would need this, is to determine the the way (or change the way) process should read/write from handles sent to it by third party applications. We can not control how third party products create their handles.</p>
  
  <p>Dear Windows gurus: help please!</p>
  
  <p>With regards</p>
  
  <p>Martin</p>