Despite reading lots of MSDN articles, I can't seem to wrap my head around MFC's message maps and the SendMessage() function. Right now I have an IP control called IDC_IPADDRESS_MYADDRESS that I want to set the value of. I know that IPM_SETADDRESS is the correct message type, but I don't know how to successfully send the message and update the ip control's value.

What do I need to add to my message map,

    ON_BN_CLICKED(IDC_BUTTON1, &myDlg::OnBnClickedButton1)

And how do I correctly use that map entry to update the value of the ip address control? Below is my attempt at updating it with a SendMessage() call in the dialog init method.

// myDlgmessage handlers

BOOL myDlg::OnInitDialog()

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);         // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon

    // TODO: Add extra initialization here

    //set default IP address    
    DWORD IP = MAKEIPADDRESS(192, 168, 0, 254);
    SendMessage(IPM_SETADDRESS, 0, IP); 

    return TRUE;  // return TRUE  unless you set the focus to a control
  • 1
    Please edit your question and add the dialog definition from the .rc file. But anyway SendMessage is probably wrong here anyway. – Jabberwocky Apr 15 at 15:50
  • @Jabberwocky I'm not sure what the .rc file is – Snail Cadet Apr 16 at 17:41
SendMessage(IPM_SETADDRESS, 0, IP);

IPM_SETADDRESS is the correct message but it is being sent to the main dialog. The dialog is not looking for this message and ignores it.

You want to send the message to the IP control instead. That means you need the window handle for the ip address control:

CWnd *ptr_ip_address = GetDlgItem(IDC_IPADDRESS_MYADDRESS);
if (ptr_ip_address)
    ptr_ip_address->SendMessage(IPM_SETADDRESS, 0, IP);

In MFC you can use CIPAddressCtrl class instead. You have to declare m_ip_address and subclass it with DoDataExchange. This class also has SetAddress method.

class CMyDialog : public CDialogEx
    CIPAddressCtrl m_ip_address;
    void DoDataExchange(CDataExchange* pDX);

void CMyDialog::DoDataExchange(CDataExchange* pDX)
    DDX_Control(pDX, IDC_IPADDRESS_MYADDRESS , m_ip_address);

BOOL myDlg::OnInitDialog()
    m_ip_address.SetAddress(192, 168, 0, 254);

MFC message map is not directly related to your question. The message map is used to respond to windows messages. For example you want to respond to ON_BN_CLICKED. But here you are sending a message to the control, not receiving a message.

You can read more about this in books for WinAPI programming. In plain windows programming there is a "message loop" and a "windows procedure" where you can respond to messages. MFC uses message map to simplify this procedure.

BOOL myDlg::OnInitDialog()
    myDlg::OnInitDialog(); <- recursive

By the way, putting myDlg::OnInitDialog in myDlg::OnInitDialog causes stack overflow. Call the base class instead, CDialogEx::OnInitDialog();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.