65.9K
CodeProject is changing. Read more.
Home

Broadcast a message to multiple instances of an application

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.19/5 (8 votes)

Jul 1, 2004

viewsIcon

46803

downloadIcon

1405

Broadcast a message to multiple instances of an application

Introduction

This application describes how to broadcast a message to multiple instance of an application using SendMessageNotify API.

Details

Follow the following steps to broadcast message :-

  • Register the message function and get its id using RegisterWindowMessage API. This function will return a unique id for the message if the registration is success UINT WM_MYMESSAGE = RegisterWindowMessage("OnTestMyMessage");
  • Add the message function in the message map using ON_REGISTERED_MESSAGE instead of ON_MESSAGE
    BEGIN_MESSAGE_MAP(CTestFindWindowDlg, CDialog)
     // Use ON_REGISTERED_MESSAGE macro instead of ON_MESSAGE
     ON_REGISTERED_MESSAGE(WM_MYMESSAGE,OnTestMyMessage)
    END_MESSAGE_MAP()
  • Broadcast the message using ::SendNotifyMessage API
    BOOL a = ::SendNotifyMessage(HWND_BROADCAST, WM_MYMESSAGE,0,1000);
  • Function declaration and definition
    // .h
    afx_msg void OnTestMyMessage(WPARAM wParam,LPARAM lParam);
    
    //.cpp
    void CTestFindWindowDlg::OnTestMyMessage(WPARAM wParam,LPARAM lParam)
    {
       int a  =(int)lParam;
       CString strValue;
       strValue.Format("%d",a);
       AfxMessageBox("Broadcast :"+strValue);
    }