[Qt-interest] USB Device Notifications (PDEV_BRAODCAST_PORT)

David Ching dc at remove-this.dcsoft.com
Sun May 2 18:21:07 CEST 2010


"Wm. G. Urquhart" <wgu at wurquhart.co.uk> wrote in message 
news:4BDD37F4.80005 at wurquhart.co.uk...
> But I have just tried this only to get the same result!
>
> ui.statusBar->showMessage(tr("Device Name : 
> %1").arg(QString::fromAscii(name)), 0) ;
>
> The 'weird' thing is that when I later enumerate the ports using 
> QueryDosDevice(...) new COMM port 'COM5' appears in my list.
>
> Still confused.
>

Hmm, I think I was wrong.  In your code:

> char name[256] = {0} ;
...
> PDEV_BROADCAST_PORT pPort = (PDEV_BROADCAST_PORT) pDevHdr ;
> strncpy_s(name, 255, pPort->dbcp_name, 255) ;
> ui.statusBar->showMessage(tr("Device Name : %1").arg(name), 0) ;
> }


Now I think pPort->dbcp_name is a Unicode string.  The reason is if the 
characters in the string are simple ASCII, each ASCII character appears in 
the first Unicode byte, followed by a '\0'.  So name would have bytes:

  'C' '\0' 'O' '\0' 'M' \0'

And when QString::arg() thinks this is an ASCII string, it sees the first 
byte 'C' and interprets the next '\0' as a NULL terminator.  That's why it 
shows only "C" for the name.

BTW, you can see this is true by the declaration:

  typedef struct _DEV_BROADCAST_PORT {
    DWORD dbcp_size;
    DWORD dbcp_devicetype;
    DWORD dbcp_reserved;
    TCHAR dbcp_name[1];
  } DEV_BROADCAST_PORT,

which says dbcp_name is a TCHAR, which is a Unicode char (wchar_t) if your 
app is built as UNICODE.

The proper solution then is to make 'name' match the type of what you are 
copying into it:
   TCHAR name[255] = {0};
   strncpy_s(name, _countof(name),  pPort->dbcp_name, 255) ;
  ui.statusBar->showMessage(tr("Device Name : %1").arg(name), 0) ;


-- David 




More information about the Qt-interest-old mailing list