[Qt-interest] USB Device Notifications (PDEV_BROADCAST_PORT)
Wm. G. Urquhart
wgu at wurquhart.co.uk
Sun May 2 21:08:28 CEST 2010
On 02/05/2010 17:21, David Ching wrote:
> "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) ;
Hello again,
Well I had tried this before, for obvious reasons and just like before
it's a non-starter with the same result as before.
I've also double checked the MOC compiler settings for the project and
again there are no references to unicode and the character set for the
project is 'Not Set'.
In case it's relevant I'm using 4.6.0 of Qt, Visual Studio 2008 and the
Intel v11.54 compiler.
So I am again back at square one.
--
William
More information about the Qt-interest-old
mailing list