views:

107

answers:

2

Hi, I created a simple window with a multiline Edit Control:

Edit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), NULL,
                    WS_CHILD | WS_VISIBLE | ES_MULTILINE,
                    20, 200, 200, 200,
                    hWnd, (HMENU)EDIT, GetModuleHandle(NULL), NULL);

If I set text using a WM_SETTEXT message, I don't get errorrs, but if I use EM_REPLACESEL I get Error 5 (ERROR_ACCESS_DENIED):

SendMessage(GetDlgItem(hWnd, EDIT), EM_REPLACESEL, 0, (LPARAM)TEXT("\r\nSome text"));
if (GetLastError()) {
    /* Error 5 ERROR_ACCESS_DENIED */
}

Same problem with EM_SETSEL:

SendMessage(GetDlgItem(hWnd, EDIT), EM_SETSEL, (WPARAM)(0),(LPARAM)(-1));
SendMessage(GetDlgItem(hWnd, EDIT), EM_REPLACESEL, 0, (LPARAM)TEXT("\r\nSome text"));
if (GetLastError()) {
    /* Error 5 ERROR_ACCESS_DENIED */
}

I noticed that if I send a WM_SETFOCUS message before the EM_REPLACESEL there are no error:

SendMessage(GetDlgItem(hWnd, EDIT), WM_SETFOCUS, (WPARAM)GetDlgItem(hWnd, EDIT), 0);
SendMessage(GetDlgItem(hWnd, EDIT), EM_REPLACESEL, 0, (LPARAM)TEXT("\r\nSome text"));
if (GetLastError()) {
    /* NO ERRORS */
}

How can I resolve this problem? Do I have to send a WM_SETFOCUS message before the EM_REPLACESEL one every time I want to append some text to my Editbox?

Thanks for help!

A: 

You can simply use a EM_SETSEL first and then do your EM_REPLACESEL.

Example:

SendMessage(hwnd, EM_SETSEL, WPARAM(0), LPARAM(-1) );
SendMessage(hwnd, EM_REPLACESEL, WPARAM(TRUE), LPARAM(str) );
Brian R. Bondy
I have the same problem using EM_SETSEL, and I get error 5
Mario
@Mario, if you get error 5 (access denied), it means that you don't have the proper rights.
Abel
A: 

You're most likely getting access denied because of UIPI.

Is the Edit box created by the same application that's attempting to send messages? If not, the application doing the SendMessage probably has a lower UIPI level than the application that owns the Edit.

If you do, in fact, own both the application that created the Edit control and the application sending the messages, you can allow specific messages in by using ChangeWindowMessageFilterEx.

ChangeWindowMessageFilterEx(hwndOfWindowReceivingMessage, EM_REPLACESEL, 
    MSGFLT_ALLOW, NULL);
dauphic
Yes the Edit box is created by the same application.I also tryed your code before the EM_REPLACESEL:ChangeWindowMessageFilterEx(GetDlgItem(hWnd, EDIT), EM_REPLACESEL, MSGFLT_ALLOW, NULL);but I get the same error.
Mario
Could you check if the ChangeWindowMessageFilterEx succeeded? If not, what is the error code?
dauphic
ChangeWindowMessageFilterEx succeeded,but, since the Edit control is created by the same application do I have to call ChangeWindowMessageFilterEx in any case?
Mario
You shouldn't. But as far as I know, only UIPI issues can give you access denied errors on SendMessage. But if everything is contained in the same application, it shouldn't be an issue. I'm not sure what the problem could be.
dauphic