Question

Can anyone help me how I update a HWND value through an edit control or any other kind of text box? I tried the following

void ChwndtestDlg::OnBnClickedButton1()
{
    TCHAR thebuffer[100]; 
    HWND thetext = (HWND)thebuffer; 
    GetDlgItemText(IDC_EDITWINDOW, thebuffer, 100);
    ::SendMessage(thetext,WM_SETTEXT,0,(LPARAM)L"hello");
}

But, that does not work! I'm new to all of this, and I'll be grateful for any help. Please bear in mind that I already know about enumwindows, and I have already successfully changed text in another program from my program, but I want to actually update the HWND in my edit control. It is supposed to work like so...

program 2's current hwnd = 0x00000: open my program -> open program #2 -> input 0x00000 into textbox in my program -> click button in my program to update the hwnd value -> input text in my programs text editor -> text goes to program 2's text editor. I'm aware that there are other ways of doing this, but I would really like to do it the way I have described. Thanks in advance. I found a similar question, but the answer was to use enumwindows, but I don't want to use that.

Was it helpful?

Solution

You are typecasting the TCHAR[] itself to an HWND. That will never work. You need to extract the TCHAR[] text from the edit control, then use _stscanf() or similar parsing function to convert that text value to an integer which can be typecasted to an HWND, then assign your other text to that HWND as needed.

For example:

void ChwndtestDlg::OnBnClickedButton1()
{
    CString sWnd;
    if (GetDlgItemText(IDC_HWNDEDIT, sWnd) > 0)
    { 
        HWND hWnd;
        if (_stscanf((LPTSTR)sWnd, _T("%p"), &hWnd) == 1)
        {
            CString sText;
            GetDlgItemText(IDC_TEXTEDIT, sText);
            ::SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)(LPTSTR)sText);
        }
    } 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top