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. std::string strange error

std::string strange error

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++jsonquestionlearning
3 Posts 2 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
    Scozturk
    wrote on last edited by
    #1

    Hi! I am using c++ win32 api. I declared a string variable and I can append some values to it by the append function. Everything goes fine if I don't enter a value longer than 15 chars but at 16 it craches... Can anyone help me? the code is here: ... string mycode; char values; ... mycode.append("Hello the value you append is: "); int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_VALUE)); if(len > 0) { GetDlgItemText(hwnd, IDC_VALUE, &values, len + 1); } mycode.append(&values); mycode.append(" is it right?"); // SetDlgItemText(hwnd,IDC_CODE,mycode.data()); Thank you very much in advance for your answers! Well... I am a beginner ...

    S 1 Reply Last reply
    0
    • S Scozturk

      Hi! I am using c++ win32 api. I declared a string variable and I can append some values to it by the append function. Everything goes fine if I don't enter a value longer than 15 chars but at 16 it craches... Can anyone help me? the code is here: ... string mycode; char values; ... mycode.append("Hello the value you append is: "); int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_VALUE)); if(len > 0) { GetDlgItemText(hwnd, IDC_VALUE, &values, len + 1); } mycode.append(&values); mycode.append(" is it right?"); // SetDlgItemText(hwnd,IDC_CODE,mycode.data()); Thank you very much in advance for your answers! Well... I am a beginner ...

      S Offline
      S Offline
      Scozturk
      wrote on last edited by
      #2

      ...OK I found my error... The error was in the way I was getting the value from the edit box. I should have done this: ... string mycode; char values[MAX_PATH]; ... mycode.append("Hello the value you append is: "); int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_VALUE)); if(len > 0) { GetDlgItemText(hwnd, IDC_VALUE, values, len + 1); } mycode.append(values); mycode.append(" is it right?"); // SetDlgItemText(hwnd,IDC_CODE,mycode.data()); ...I told I was a beginner... :-D Well... I am a beginner ...

      J 1 Reply Last reply
      0
      • S Scozturk

        ...OK I found my error... The error was in the way I was getting the value from the edit box. I should have done this: ... string mycode; char values[MAX_PATH]; ... mycode.append("Hello the value you append is: "); int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_VALUE)); if(len > 0) { GetDlgItemText(hwnd, IDC_VALUE, values, len + 1); } mycode.append(values); mycode.append(" is it right?"); // SetDlgItemText(hwnd,IDC_CODE,mycode.data()); ...I told I was a beginner... :-D Well... I am a beginner ...

        J Offline
        J Offline
        James R Twine
        wrote on last edited by
        #3

        >    if(len > 0) >    { >      GetDlgItemText(hwnd, IDC_VALUE, values, len + 1); >    }    Be sure that len does not exceed the capacity of the buffer you are copying into; in this case, values, which is of MAX_PATH length.    I would change the code to something like this:       GetDlgItemText( hwnd, IDC_VALUE, values, **MAX_PATH** );    It is OK to ask for "too much text" in this case, the function will copy only what is available in the window or how much your buffer can handle, which ever is less.    Peace! -=- James Tip for SUV winter driving survival: "Professional Driver on Closed Course" does not mean "your Dumb Ass on a Public Road"!
        Articles -- Products: Delete FXP Files & Check Favorites

        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