Domanda

I am trying to use listbox.Addstring(); in MFC application which will take LPCTSTR. I am passing a variable of char array that's 33 chars long.

ListBox.AddString(Adapter_List->pScanList->network[0].szSsid);

SzSsid is declared as char szSsid[33];

I am facing two problems:

1) if I typecast to LPCTSTR like

ListBox.AddString( (LPCTSTR ) Adapter_List->pScanList->network[0].szSsid );

I am not getting correct output - there are some Chinese characters displaying. I know it's some unicode problem but I am not knowledgeable about unicode.

2) if I dont typecast I get an error

Cannot convert char[33] to LPCTSTR

I am trying to build an MFC application which will display all access points. In szSsid I am able to see access point names.

È stato utile?

Soluzione

LPCTSTR type-casting is just wrong. You may want to use an ATL conversion helper like CA2T to convert from char string to TCHAR (LPCTSTR) string, or CA2W to convert from char string to Unicode UTF-16 wchar_t string; e.g.:

// CA2T - Uses the TCHAR model (obsolete)
ListBox.AddString( CA2T(Adapter_List->pScanList->network[0].szSsid) );

or:

// CA2W - Conversion to Unicode UTF-16 (wchar_t) string
// More modern approach.
ListBox.AddString( CA2W(Adapter_List->pScanList->network[0].szSsid) );

But, more important, what is the encoding used by your char szSSid[] string? You may want to specify that encoding identifier (e.g. CP_UTF8 for UTF-8 strings) to CA2W constructor nCodePage parameter for proper conversion to Unicode UTF-16 string passed to AddString() method.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top