HOWTO: Use SendMessage() As Opposed to SendDlgItemMessage()

ID: Q12273


The information in this article applies to:


SUMMARY

The following information describes under what circumstances it is appropriate to use either the SendMessage() or SendDlgItemMessage() function.

Both SendMessage() and SendDlgItemMessage() can be used to add strings to a list box. SendMessage() is used to send a message to a specific window using the handle to the list box. SendDlgItemMessage() is used to send a message to the child window of a given window using the list box resource ID. SendDlgItemMessage() is most often used in dialog box functions that have a handle to the dialog box and not to the child window control.

The SendDlgItemMessage() call


   SendDlgItemMessage (hwnd, id, msg, wParam, lParam) 

is equivalent to the following SendMessage() call:

   hwnd2 = GetDlgItem (hwnd, id);
   SendMessage (hwnd2, msg, wParam, lParam); 

Please note that PostMessage() should never be used to talk to the child windows of dialog boxes for the following reasons:
  1. PostMessage() will only return an error if the message was not posted to the control's message queue. Since many messages are sent to control return information, PostMessage() will not work, since it does not return the information to the caller.


  2. 16-bit only: Messages such as the WM_SETTEXT message that include a far pointer to a string can potentially cause problems if posted using the PostMessage() function. The far pointer may point into a buffer that is inside the DS (data segment). Because PostMessage() does not process the message immediately, the DS might get moved. If the DS is moved before the message is processed, the far pointer to the buffer will be invalid.



Keywords          : kbNTOS kbGrpUser kbWinOS kbWndw kbWndwMsg 
Version           : 
Platform          : 
Issue type        : kbhowto 

Last Reviewed: March 4, 1999