Windows 10 CreateFile on COMx port do not returns

Giganews Newsgroups
Subject: Windows 10 CreateFile on COMx port do not returns
Posted by:  CortexA57 (CortexA57@ch.invalid)
Date: Mon, 26 Nov 2018

Hi! I've a code that list all serial ports available using the
CreateFile function.
The code is working on XP, Win7 systems and on many Win10 .
Only on some Win10 systems the application will hung up over non
existing port:
ie in a systema with COM1,COM2, COM3,COM4,COM10,COM11 ports (all USB
deviced except the COM1, COM2 ports), when the CreateFile will try to
open COM5, the application hung-up and never returns from CreateFile....
any idea?
Thanks

Code looks like this one:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int CUart::CreateComList(comList* cList, int size)
{
    HANDLE            hPort;
    unsigned int        i;
    wchar_t            buf[64];
    wchar_t            buff1[64];
    unsigned int    cnt;

  for(int i=1; i<256 && cnt<size; i++)
  {
    swprintf(buf,64,L"COM%d",i);
    swprintf(buff1,64,L"\\.\COM%d",i);

    //Try to open the port(FILE_SHARE_READ|FILE_SHARE_WRITE)
    BOOL bSuccess = FALSE;

    hPort = CreateFile(buff1,
            GENERIC_READ|GENERIC_WRITE, 0,
            NULL,
            OPEN_EXISTING, 0, NULL
        );

    //// NEVER RETURNS IF PORT IS NON-EXISTENT ON WIN10 PRO 64bit ..
    //// WHY???

    if (hPort == INVALID_HANDLE_VALUE)
    {
      DWORD dwError = GetLastError();

      //Check to see if the error was because some other app had the
      //port open or a general failure
      if (dwError == ERROR_ACCESS_DENIED ||
        dwError == ERROR_GEN_FAILURE ||
        dwError == ERROR_SHARING_VIOLATION ||
        dwError == ERROR_SEM_TIMEOUT)
            bSuccess = TRUE;
    }
    else
    {
      //The port was opened successfully
      bSuccess = TRUE;
      //Don't forget to close the port, since we are going to do
      // nothing with it anyway
      CloseHandle(hPort);
    }
    if (bSuccess)
    {
      /* Add item to combo box */
      lstrcpyn(cList[cnt].comName,buff1,sizeof(cList[cnt].comName));
lstrcpyn(cList[cnt].comShowName,buf,sizeof(cList[cnt].comShowName));
    cnt++;
    }
  }
  return(cnt); // Number of com ports
}

Replies