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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Strncmp and GetwindowText problem

Strncmp and GetwindowText problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondata-structures
6 Posts 4 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.
  • K Offline
    K Offline
    keret
    wrote on last edited by
    #1

    I enumerate all open windows with Enumwindows. In the processor function I try to check if the title of the window contains a certain string. If yes, put the handle in a list of handles(m_wndList).

    if(hwnd)
    {
    CWindow win(hwnd);
    CHAR str[100];
    win.GetWindowText((LPTSTR)(str),100);
    if (strncmp(str,"searchstring", 13) == 0)
    dlg->m_wndList.push_back(hwnd);
    }

    It compiles, but I get the following error messages: Run-time Check Failure #2: Stack around the variable str is corrupted and/or Run-time Check Failure #2: Stack around the variable win is corrupted What is the problem? How should I change my code?

    S C D 3 Replies Last reply
    0
    • K keret

      I enumerate all open windows with Enumwindows. In the processor function I try to check if the title of the window contains a certain string. If yes, put the handle in a list of handles(m_wndList).

      if(hwnd)
      {
      CWindow win(hwnd);
      CHAR str[100];
      win.GetWindowText((LPTSTR)(str),100);
      if (strncmp(str,"searchstring", 13) == 0)
      dlg->m_wndList.push_back(hwnd);
      }

      It compiles, but I get the following error messages: Run-time Check Failure #2: Stack around the variable str is corrupted and/or Run-time Check Failure #2: Stack around the variable win is corrupted What is the problem? How should I change my code?

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      There's some problems with your code. What's with the (LPTSTR) nonsense? My guess is that you're building a Unicode application and casting away the compiler warnings (the (LPTSTR) cast I mentioned before) which were informing you of the real problem: that CHAR str[100]; is not a Unicode string. Try this:

      if(hwnd)
      {
      CWindow win(hwnd);
      TCHAR str[100];
      win.GetWindowText(str, 100);
      if (_tcsncmp(str, _T("searchstring"), 13) == 0)
      dlg->m_wndList.push_back(hwnd);
      }

      Don't cast unless you're sure what the cast actually means, and don't try to cast away error messages.

      Steve

      1 Reply Last reply
      0
      • K keret

        I enumerate all open windows with Enumwindows. In the processor function I try to check if the title of the window contains a certain string. If yes, put the handle in a list of handles(m_wndList).

        if(hwnd)
        {
        CWindow win(hwnd);
        CHAR str[100];
        win.GetWindowText((LPTSTR)(str),100);
        if (strncmp(str,"searchstring", 13) == 0)
        dlg->m_wndList.push_back(hwnd);
        }

        It compiles, but I get the following error messages: Run-time Check Failure #2: Stack around the variable str is corrupted and/or Run-time Check Failure #2: Stack around the variable win is corrupted What is the problem? How should I change my code?

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        To complement the answer from Stephen, I suggest you read this excellent article[^]. You'll be able to really understand why your code is wrong.

        K 1 Reply Last reply
        0
        • C Cedric Moonen

          To complement the answer from Stephen, I suggest you read this excellent article[^]. You'll be able to really understand why your code is wrong.

          K Offline
          K Offline
          keret
          wrote on last edited by
          #4

          Thanks guys, you both helped me a lot. At the moment I just cut & paste any code I find, but this will change I hope.

          1 Reply Last reply
          0
          • K keret

            I enumerate all open windows with Enumwindows. In the processor function I try to check if the title of the window contains a certain string. If yes, put the handle in a list of handles(m_wndList).

            if(hwnd)
            {
            CWindow win(hwnd);
            CHAR str[100];
            win.GetWindowText((LPTSTR)(str),100);
            if (strncmp(str,"searchstring", 13) == 0)
            dlg->m_wndList.push_back(hwnd);
            }

            It compiles, but I get the following error messages: Run-time Check Failure #2: Stack around the variable str is corrupted and/or Run-time Check Failure #2: Stack around the variable win is corrupted What is the problem? How should I change my code?

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

            keret wrote:

            if (strncmp(str,"searchstring", 13) == 0)

            Why 13?

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

            K 1 Reply Last reply
            0
            • D David Crow

              keret wrote:

              if (strncmp(str,"searchstring", 13) == 0)

              Why 13?

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

              K Offline
              K Offline
              keret
              wrote on last edited by
              #6

              I had an other searchstring originally. :)

              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