Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is GetItemText limited to 256 bytes for CString ?

Is GetItemText limited to 256 bytes for CString ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wormhole5230
    wrote on last edited by
    #1

    There are two types of function to extract a text in the list item in CListCtrl. One is CString GetItemText( int nItem, int nSubItem ), and the other is int GetItemText( int nItem, int nSubItem, LPTSTR lpszText, int nLen ). In case of using the first function, it looks like it can only extract 256 bytes, even the text is over 256 bytes. It is OK for second function. I don't know why the length is limited for CString. Any idea to resolve this problem with CString? Should I use second function? Thanks.

    L D 2 Replies Last reply
    0
    • W Wormhole5230

      There are two types of function to extract a text in the list item in CListCtrl. One is CString GetItemText( int nItem, int nSubItem ), and the other is int GetItemText( int nItem, int nSubItem, LPTSTR lpszText, int nLen ). In case of using the first function, it looks like it can only extract 256 bytes, even the text is over 256 bytes. It is OK for second function. I don't know why the length is limited for CString. Any idea to resolve this problem with CString? Should I use second function? Thanks.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Perhaps the first function uses the second with a length value of 256 ? Probably easiest to write your own function which is equivalent to the first but with a larger length limit. Elaine :rose: The tigress is here :-D

      W 1 Reply Last reply
      0
      • L Lost User

        Perhaps the first function uses the second with a length value of 256 ? Probably easiest to write your own function which is equivalent to the first but with a larger length limit. Elaine :rose: The tigress is here :-D

        W Offline
        W Offline
        Wormhole5230
        wrote on last edited by
        #3

        OK, that's no problem but what happened to CString, is it bug or not? Thanks.

        L 1 Reply Last reply
        0
        • W Wormhole5230

          OK, that's no problem but what happened to CString, is it bug or not? Thanks.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Its not a bug in CString, its the function that returns CString. They have to call the original function and in the course of that add the length parameter. And thinking that 256 chars would have been enough.... If you look at the library code there will be a GetBuffer(256) statement with the length also set to 256. Make sure these match in your code or you could get an overflow. Elaine :rose: The tigress is here :-D

          1 Reply Last reply
          0
          • W Wormhole5230

            There are two types of function to extract a text in the list item in CListCtrl. One is CString GetItemText( int nItem, int nSubItem ), and the other is int GetItemText( int nItem, int nSubItem, LPTSTR lpszText, int nLen ). In case of using the first function, it looks like it can only extract 256 bytes, even the text is over 256 bytes. It is OK for second function. I don't know why the length is limited for CString. Any idea to resolve this problem with CString? Should I use second function? Thanks.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            What are you using to verify the length of the CString object? Have you stepped into CListCtrl::GetItemText() to watch how it is behaving?

            W 1 Reply Last reply
            0
            • D David Crow

              What are you using to verify the length of the CString object? Have you stepped into CListCtrl::GetItemText() to watch how it is behaving?

              W Offline
              W Offline
              Wormhole5230
              wrote on last edited by
              #6

              I just used GetLength() to check the length of the returned CString object. The return value is 256. The length of the text in the CListCtrl is surely over 256 bytes. The second function can extract whole text when the last parameter nLen is large enought to hold whole text. Thanks.

              D 1 Reply Last reply
              0
              • W Wormhole5230

                I just used GetLength() to check the length of the returned CString object. The return value is 256. The length of the text in the CListCtrl is surely over 256 bytes. The second function can extract whole text when the last parameter nLen is large enought to hold whole text. Thanks.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Ok, but did you step through the MFC code?

                W 1 Reply Last reply
                0
                • D David Crow

                  Ok, but did you step through the MFC code?

                  W Offline
                  W Offline
                  Wormhole5230
                  wrote on last edited by
                  #8

                  With your advice, I looked at the MFC code. The function was defined as follows. CString CListCtrl::GetItemText(int nItem, int nSubItem) const { ASSERT(::IsWindow(m_hWnd)); LVITEM lvi; memset(&lvi, 0, sizeof(LVITEM)); lvi.iSubItem = nSubItem; CString str; int nLen = 128; int nRes; do { nLen *= 2; lvi.cchTextMax = nLen; lvi.pszText = str.GetBufferSetLength(nLen); nRes = (int)::SendMessage(m_hWnd, LVM_GETITEMTEXT, (WPARAM)nItem, (LPARAM)&lvi); } while (nRes == nLen-1); str.ReleaseBuffer(); return str; } At first look at, it looks like to extract whole text using do-while statement. But in my code, nRes is 256, so do-while is applied only one time.

                  D 1 Reply Last reply
                  0
                  • W Wormhole5230

                    With your advice, I looked at the MFC code. The function was defined as follows. CString CListCtrl::GetItemText(int nItem, int nSubItem) const { ASSERT(::IsWindow(m_hWnd)); LVITEM lvi; memset(&lvi, 0, sizeof(LVITEM)); lvi.iSubItem = nSubItem; CString str; int nLen = 128; int nRes; do { nLen *= 2; lvi.cchTextMax = nLen; lvi.pszText = str.GetBufferSetLength(nLen); nRes = (int)::SendMessage(m_hWnd, LVM_GETITEMTEXT, (WPARAM)nItem, (LPARAM)&lvi); } while (nRes == nLen-1); str.ReleaseBuffer(); return str; } At first look at, it looks like to extract whole text using do-while statement. But in my code, nRes is 256, so do-while is applied only one time.

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    I used the following code snippet and both TRACE() calls output 300: CString str; int len; str = "12345678901234567890123456789012345678901234567890"; str += "12345678901234567890123456789012345678901234567890"; str += "12345678901234567890123456789012345678901234567890"; str += "12345678901234567890123456789012345678901234567890"; str += "12345678901234567890123456789012345678901234567890"; str += "12345678901234567890123456789012345678901234567890"; len = str.GetLength(); TRACE("len = %d\n", len); ctrlList.InsertItem(0, str); str.Empty(); str = ctrlList.GetItemText(0, 0); len = str.GetLength(); TRACE("len = %d\n", len);

                    W 1 Reply Last reply
                    0
                    • D David Crow

                      I used the following code snippet and both TRACE() calls output 300: CString str; int len; str = "12345678901234567890123456789012345678901234567890"; str += "12345678901234567890123456789012345678901234567890"; str += "12345678901234567890123456789012345678901234567890"; str += "12345678901234567890123456789012345678901234567890"; str += "12345678901234567890123456789012345678901234567890"; str += "12345678901234567890123456789012345678901234567890"; len = str.GetLength(); TRACE("len = %d\n", len); ctrlList.InsertItem(0, str); str.Empty(); str = ctrlList.GetItemText(0, 0); len = str.GetLength(); TRACE("len = %d\n", len);

                      W Offline
                      W Offline
                      Wormhole5230
                      wrote on last edited by
                      #10

                      You are right. I found it worked well in alphanumeric text. I'm a Korean. When the string is Korean, it fails. Thanks.

                      D 1 Reply Last reply
                      0
                      • W Wormhole5230

                        You are right. I found it worked well in alphanumeric text. I'm a Korean. When the string is Korean, it fails. Thanks.

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        Makes me wonder if you aren't having a Unicode problem.

                        W 1 Reply Last reply
                        0
                        • D David Crow

                          Makes me wonder if you aren't having a Unicode problem.

                          W Offline
                          W Offline
                          Wormhole5230
                          wrote on last edited by
                          #12

                          OK, it looks like it definitely related to data-type mapping. I will check it later because of the lack of time. Thank you for your consideration. :)

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups