Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Broadcast a message to multiple instances of an application

0.00/5 (No votes)
30 Jun 2004 1  
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);
    }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here