Solved

CListCtrl and Unicode(?) Problem

Posted on 2001-06-27
4
440 Views
Last Modified: 2013-11-20
This little bug's got me stumped. I've got a listctrl that I'm dynamically creating in a formview. It is a report style listctrl, which allows the editing of the items.  

I'm handling the EndLabelEdit message with the message maps, in the formview:
ON_NOTIFY(LVN_ENDLABELEDITA,IDC_REPOSITORY, OnEndlabeleditRepository)
     ON_NOTIFY(LVN_ENDLABELEDITW,IDC_REPOSITORY, OnEndlabeleditRepository)

I ended up using these, as a solution to a previous question I asked, where handling LVN_ENDLABELEDIT was not enough to catch the message.

Up to this point, things work fine, and I can handle and validate the edited text.  The problem is here:

void CMyView::OnEndlabeleditRepository(NMHDR* pNMHDR, LRESULT* pResult)
{
 CMyView::OnEndlabeleditRepository(pNMHDR, pResult);
 LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;    
 CString strText = pDispInfo->item.pszText;
 //do something with the text...
 *pResult = TRUE;
}

If I was to enter "Test" then
pDispInfo->item.pszText = T\0e\0s\0t\0
so strText ends up equalling "T"

I think this is a UNICODE issue, and I don't currently have the preprocessor _UNICODE defined.  But it's my understanding that a CString should take this and convert it correctly, right?

This works correctly on Win98, and I get this problem on WinNT.

Any ideas on how to remedy this?

Thanks,
Marcus
0
Comment
Question by:marcus78
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
migel earned 50 total points
ID: 6234003
Hi!
no CString doesn`t convert chars to the UNICODE and vice versa.
you must perform such conversion manually:
for example unicode string to char
{
          short nSize = wcslen(pDispInfo->item.pszText);
          char* pStr = strText.GetBufferSetLength(nSize+1);
          if(!WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)(pDispInfo->item.pszText), -1, pStr,
                                             nSize,          // size of buffer
                                             NULL, NULL))
               {
               DWORD dwErrorCodes = GetLastError();
               }
          strCaption.ReleaseBuffer();
}
strCaption now contain 'normal' string
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6234259
Windows NT uses UNICODE internally (all API calls with ANSI parameters get converted to Unicode).

I guess the solution is something around

CString str;
if (::IsWindowUnicode(lisctrl.m_hWnd)) {
  // migel's code here
}
else
  str = pDispInfo->item.pszText;

Peter
0
 
LVL 12

Expert Comment

by:migel
ID: 6234399
also you can separate notifications handler for
LVN_ENDLABELEDITA and
LVN_ENDLABELEDITW
0
 

Author Comment

by:marcus78
ID: 6234999
Migel,

This is what I'm looking for. Thanks!

Marcus
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Macro to save highlighted rows as a separate file 5 41
if loop in java 3 130
only14 challenge 19 69
How to convert MFC::CString to UTF8 wchar_t* 10 148
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now