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. Self-registered edit control class crashes because of wrong GetWindowTextLength return value

Self-registered edit control class crashes because of wrong GetWindowTextLength return value

Scheduled Pinned Locked Moved C / C++ / MFC
cssperformancehelpquestion
5 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.
  • S Offline
    S Offline
    Skarrin
    wrote on last edited by
    #1

    Hello, long title, short story: a rather old third-party library uses the following code to register its own edit control class: static const TCHAR BASED_CODE szEdit[] = _T("GXEDIT"); WNDCLASS wcls; // check to see if class already registered if (!::GetClassInfo(hResource, szEdit, &wcls)) { // Use standard "edit" control as a template. VERIFY(::GetClassInfo(NULL, _T("edit"), &wcls)); // set new values wcls.style |= GX_GLOBALCLASS; wcls.hInstance = hResource; wcls.lpszClassName = szEdit; AfxRegisterClass(&wcls); This works fine unless XP themes are enabled. If they are and I use GetWindowText(CString-variable) with this edit control, the CString destructor causes a heap corruption because ::GetWindowTextLength reports less bytes than ::GetWindowText actually receives, and thus CString::GetBufferSetLength allocates not enough memory. Is there a simple solution to change the above code so that this GXEDIT works with XP themes? I already tried rewriting it using WNDCLASSEX instead, but that didn't help. adTHANKSvance, Jens

    CPalliniC L 2 Replies Last reply
    0
    • S Skarrin

      Hello, long title, short story: a rather old third-party library uses the following code to register its own edit control class: static const TCHAR BASED_CODE szEdit[] = _T("GXEDIT"); WNDCLASS wcls; // check to see if class already registered if (!::GetClassInfo(hResource, szEdit, &wcls)) { // Use standard "edit" control as a template. VERIFY(::GetClassInfo(NULL, _T("edit"), &wcls)); // set new values wcls.style |= GX_GLOBALCLASS; wcls.hInstance = hResource; wcls.lpszClassName = szEdit; AfxRegisterClass(&wcls); This works fine unless XP themes are enabled. If they are and I use GetWindowText(CString-variable) with this edit control, the CString destructor causes a heap corruption because ::GetWindowTextLength reports less bytes than ::GetWindowText actually receives, and thus CString::GetBufferSetLength allocates not enough memory. Is there a simple solution to change the above code so that this GXEDIT works with XP themes? I already tried rewriting it using WNDCLASSEX instead, but that didn't help. adTHANKSvance, Jens

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Are you sure there is such a bug affecting GetWindowTextLength? I cannot find related info.

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      In testa che avete, signor di Ceprano?

      S 1 Reply Last reply
      0
      • S Skarrin

        Hello, long title, short story: a rather old third-party library uses the following code to register its own edit control class: static const TCHAR BASED_CODE szEdit[] = _T("GXEDIT"); WNDCLASS wcls; // check to see if class already registered if (!::GetClassInfo(hResource, szEdit, &wcls)) { // Use standard "edit" control as a template. VERIFY(::GetClassInfo(NULL, _T("edit"), &wcls)); // set new values wcls.style |= GX_GLOBALCLASS; wcls.hInstance = hResource; wcls.lpszClassName = szEdit; AfxRegisterClass(&wcls); This works fine unless XP themes are enabled. If they are and I use GetWindowText(CString-variable) with this edit control, the CString destructor causes a heap corruption because ::GetWindowTextLength reports less bytes than ::GetWindowText actually receives, and thus CString::GetBufferSetLength allocates not enough memory. Is there a simple solution to change the above code so that this GXEDIT works with XP themes? I already tried rewriting it using WNDCLASSEX instead, but that didn't help. adTHANKSvance, Jens

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

        Maybe its a Unicode/ANSI issue. The third party control may be using Unicode. Windows Unicode (UTF-16) uses 2 bytes to represent each character. Try changing your GetWindowText to GetWindowTextW and see what happens, perhaps you need a CStringW. Best Wishes, -Randor (David Delaune)

        S 1 Reply Last reply
        0
        • CPalliniC CPallini

          Are you sure there is such a bug affecting GetWindowTextLength? I cannot find related info.

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          S Offline
          S Offline
          Skarrin
          wrote on last edited by
          #4

          Part of this third-party library is a grid control, and the problems occurs when a GXEDIT control is created in-place to edit the contents of a cell. The cell contains a 4-digit number, and the value returned by GetWindowTextLength is 12. But only if I discard this value and reserve 25 bytes or more in CString::GetBufferSetLength, the heap doesn't get corrupted. It's strange, but I think this is only a side effect of some error in the class registration. If I don't use the XP visual styles for this app (no manifest file), everything works fine. Other edit controls that are of the systems standard "edit" class type, work ok with or without styles enabled... Regards, Jens

          1 Reply Last reply
          0
          • L Lost User

            Maybe its a Unicode/ANSI issue. The third party control may be using Unicode. Windows Unicode (UTF-16) uses 2 bytes to represent each character. Try changing your GetWindowText to GetWindowTextW and see what happens, perhaps you need a CStringW. Best Wishes, -Randor (David Delaune)

            S Offline
            S Offline
            Skarrin
            wrote on last edited by
            #5

            The control does not use unicode, I have checked that. As I wrote, the problem only occurs if I enable XP visual styles and themes for the application by including a manifest file in the executables directory. I already tried to write replacements for Get/SetWindowText: void CGXEditControl::GetWindowText(CString& s) { if (IsThemeActive()) { const int nLen = ::GetWindowTextLengthW(m_hWnd); CStringW sW; ::GetWindowTextW(m_hWnd, sW.GetBufferSetLength(nLen), nLen+1); sW.ReleaseBuffer(); s = sW; } else { //CGXEditControl is derived from CEdit, CGXStatic CEdit::GetWindowText(s); } } void CGXEditControl::SetWindowText(const CString& s) { if (IsThemeActive()) { CStringW sW(s); ::SetWindowTextW(GetSafeHwnd(), sW); } else { CEdit::SetWindowText(s); } } This prevents the heap corruption, but then the edit control contains a lot of unreadable stuff like arabic letters :omg: even though the debugger shows that sW seems to contain a correct string like "1120". The problem seems to be the registration process of the class GXEDIT. In CGXEditControl::CreateControl, if I replace the class name "GXEDIT" with the standard "edit" class in the call to CEdit::Create, everything works! But I don't know what side effects this will have for the rest of the library :confused:, so I want to avoid this workaround if possible. BTW, this library is a very antique version of Stingray's Objective Grid, dated 1997 :-O . Regards, Jens

            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