title: Can't open COM1 from application launched at startup
body: <p>I'm using <a href="http://winlirc.sourceforge.net/" rel="nofollow noreferrer">WinLIRC</a> with IR receiver connected to serial port COM1 on Windows 7 x64. <code>WinLIRC</code> is added to Startup folder (Start->All applications->Startup) so it starts every time I log in. Very often (but not all the time) I see initialization error messages from <code>WinLIRC</code>, which continue for some time (couple of minutes) if I retry initialization, and after some retries it initializes correctly and works fine. If I remove it from Startup and start manually at any other moment it starts without errors.</p>
  
  <p>I've downloaded <code>WinLIRC</code> sources and added <code>MessageBox</code> calls here and there so I can see what happens during initialization and found out that <code>CreateFile</code> call fails:</p>
  
  <pre><code>if((hPort=CreateFile(
      settings.port,GENERIC_READ | GENERIC_WRITE,
      0,0,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0))==INVALID_HANDLE_VALUE)
  {
      char buffer[256];
      sprintf_s(buffer, "CreateFile(%s) failed with %d", settings.port, GetLastError());
      MessageBox(NULL, buffer, "debug", MB_OK);
      hPort=NULL;
      return false;
  }
  </code></pre>
  
  <p>I see message box saying "CreateFile(COM1) failed with 5", and 5 is an error code for "Access denied" error according to <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx" rel="nofollow noreferrer">this link</a>.</p>
  
  <p>So the question is why opening COM-port can fail with such error right after booting Windows and proceed normally few seconds or minutes later?</p>
  
  <p><strong>Update</strong>: the COM-port is the real one.</p>
  
  <p><strong>Update2</strong>: regarding the other application that opens serial port before <code>WinLIRC</code>. I did the following: I put <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653" rel="nofollow noreferrer">Process Explorer</a> to the Startup folder so it start on log in also, then rebooted. As soon as process explorer started I ran "Find Handle or Dll" dialog, put "Serial0" to the input and hit "Search". By that moment WinLIRC had already shown message box saying "CreateFile(COM1) failed with 5". Then I waited till the process explorer search ends, seen that it found nothing, then tried to reinitialize WinLIRC and it failed again. So I suggest that it is not the case of serial port being opened by other application. If anyone can suggest a better way to check it, I'll happily recheck.</p>
  
  <p>When I search for "Serial0" in process explorer while WinLIRC is running it finds the <code>winlirc.exe</code> process, so it looks like it is correct term to search.</p>
  
  <p><strong>Update3</strong>: regarding the serial mouse driver. It is not listed in device manager, so I wasn't able to disable it there, however I have found <a href="http://answers.microsoft.com/en-us/windows/forum/windows_7-hardware/windows-7-recognized-usb-gps-as-a-serial-mouse-in/0c3f0d94-6181-4a43-9e90-bcea8a21415d" rel="nofollow noreferrer">this</a> instructions on how to disable <code>sermouse</code> service and it didn't help.</p>
  
  <p><strong>Update4</strong>: one more thing I forgot to mention. It happens only if I log in soon after booting PC. If I leave windows on log in screen for several minutes and log in later, then WinLIRC initializes without any problems always.</p>
  
  <p><strong>Update5</strong>: Unfortunately, I don't have access to the computer that had this problem reproducing,  so I can't experiment anymore.</p>