Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1. I want to give parameter to ListCtrl::SetItemText as follows

m_List_1100.SetItemText(i, 1, SP.mac);

2. But because SP.mac is a char type variable, it causes compile error.

3. I want to it to convert to CString type another variable.

In addtion to, I want to get the conversion rules about CString

What I have tried:

I have failed to code samples and make same errors.
Posted
Updated 15-Mar-16 22:24pm

1 solution

The lpszText argument of SetItemText is of type LPCTSTR string. When having an ANSI string (LPCSTR) it must be converted to wide char with Unicode builds.

While this can be converted using MultiByteToWideChar function (Windows)[^], using the CString class is simpler because that provides such conversions with constructors and assignment operators.

So simply create a CString from the ANSI string and pass that:
CString strMac(SP.mac);
m_List_1100.SetItemText(i, 1, strMac.GetString());


To know about the conversion rules see the CStringT Class[^].

It uses some predefined types to handle parameters of different types (see CStringT Predefined Types in the above link). The interesting types for conversion are those with Y (YCHAR, PYSTR, PCYSTR). They indicate support for characters not matching the build setting (char, LPSTR, LPCSTR for Unicode builds). So when a member function supports one of these types, you can pass an ANSI char or string with Unicode builds. See for example the CStringT::operator =[^] and the CStringT::CStringT[^] constructor.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900