4

I need to append text to win32 edit control i have working function to do this , but the text that printed in the edit control is gibrish why ? the sample code taken from microsoft example from here

void settext(HWND hDlg,std::string s)
{
    //std::wstring ws; 
    //ws.assign( s.begin(), s.end() );
    //LPWSTR pwst = &ws[0];
    //// get temporary LPCWSTR (pretty safe)
    //LPCWSTR pcwstr = ws.c_str();
    //SetDlgItemText(hWndEdit, IDC_EDIT1,pcwstr);
    HWND hWndEdit = GetDlgItem (hDlg, IDC_EDIT1);
    LPSTR pst = &s[0];
    int ndx = GetWindowTextLength (hWndEdit);
    SetFocus (hWndEdit);
    #ifdef WIN32
      SendMessage (hWndEdit, EM_SETSEL, (WPARAM)ndx, (LPARAM)ndx);
    #else
      SendMessage (hWndEdit, EM_SETSEL, 0, MAKELONG (ndx, ndx));
    #endif
      SendMessage (hWndEdit, EM_REPLACESEL,0,(LPARAM)pst);
    
}

and from the DlgProc im calling :

std::string  ss("wwwwww");
settext(hwnd,ss);

update
even if i do as suggested here :

 SendMessage (hWndEdit, EM_REPLACESEL,0,(LPARAM)s.c_str()); 

that pass compilation but still the characters printed are gibrish
and if i do :

LPSTR pst = s.c_str()

it doesn't pass compilation the error:
error C2440: 'initializing' : cannot convert from 'const char *' to 'LPSTR'

Community
  • 1
  • 1
user63898
  • 29,839
  • 85
  • 272
  • 514

4 Answers4

4

My guess is that your app is compiled for Unicode and so the window is interpreting your ANSI C string as a Unicode C string, hence the characters from another language.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • the application compiled in unicode , i type in english . mybe its the problem so how can i over come this? – user63898 Jun 09 '11 at 21:30
  • Pass a LPWSTR to SendMessage. Use a `wstring`. For sure it is your problem! – David Heffernan Jun 09 '11 at 21:51
  • 1
    If the app is compiled as unicode (be sure that both UNICODE and _UNICODE are defined), then you'll need to pass a unicode string; use wstring instead. To be sure, try calling SendMessageW explicitly, and use LPCWSTR pstr = ws.c_str(), passing pstr as the pointer to SendMessageW. – BrendanMcK Jun 09 '11 at 21:55
  • BrendanMcK you are right this is the answer , also i have to send the string with L – user63898 Jun 10 '11 at 06:01
  • L prefix is indeed how you denote a wide string literal – David Heffernan Jun 10 '11 at 06:04
2

The problem is that

LPSTR pst = &s[0];

is not null terminated. You need to use

LPCSTR pst = s.c_str();
Coincoin
  • 27,880
  • 7
  • 55
  • 76
grayDad
  • 324
  • 1
  • 7
1

There's no guarantee that &s[0] is null terminated so you're probably just seeing whatever random memory is there until a null appears after the end of your string. Probably in some compilers/libraries it works out to be null terminated some/all of the time and thus hasn't surfaced until now.

You'll want to use s.c_str() instead.

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • 1
    Use LPCSTR. The C stands for `const` - you're not allowed to modify the string pointed to by the pointer from `c_str()`. – bdonlan Jun 09 '11 at 21:39
0

You can convert your LPSTR to an LPWSTR with ATL so that the Unicode APIs can digest it.

#include <windows.h>
#include <atlbase.h>

void settext(HWND hDlg,std::string s)
{
    USES_CONVERSION;
    LPSTR pst = A2T(s.c_str());

A2T will convert a narrow string to a string compatible with the current compilation settings (Unicode/multi-byte). You could also use A2W which explicitly converts ANSI to WIDE, but A2T will always do the right thing regardless of whether you're compiling in Unicode mode or not.

A2T actually allocates enough space on the stack for the resulting string, so you don't need to worry about freeing it.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299