std::string strange error
-
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 ... -
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 ......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 ... -
...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 ...> if(len > 0) > { > GetDlgItemText(hwnd, IDC_VALUE, values, len + 1); > }
Be sure thatlen
does not exceed the capacity of the buffer you are copying into; in this case,values
, which is ofMAX_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