Solved

CListCtrl and Unicode(?) Problem

Posted on 2001-06-27
4
436 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 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

17 Experts available now in Live!

Get 1:1 Help Now