title: Determining Serial port usage
body: <p>I want to programmaticaly check for the available serial ports which are not connected with any device.</p>
  
  <p>i tried the following code and able to get the available com ports but dont know whether it is used. how to determine that?</p>
  
  <pre><code>TCHAR szComPort[8];  
  HANDLE hCom = NULL;  
  
  for (int i = 1; i &lt;= 10; ++i)  
  {  
      if (i &lt; 10)
        wsprintf(szComPort, _T("COM%d"), i);
      else
        wsprintf(szComPort, _T("\\\\.\\COM%d"), i); 
  
      hCom = CreateFile(szComPort,  
          GENERIC_READ|GENERIC_WRITE, // desired access should be read&amp;write  
          0,                          // COM port must be opened in non-sharing mode  
          NULL,                       // don't care about the security  
          OPEN_EXISTING,              // IMPORTANT: must use OPEN_EXISTING for a COM port  
          0,                          // usually overlapped but non-overlapped for existance test  
          NULL);                      // always NULL for a general purpose COM port  
  
     }  
  </code></pre>