Windows Messaging HWND_BROADCAST

I work in an application in this application, I will wait for any event in application 1, and when this event occurs, I will send a message to application 2, which will do something.

Announcement of the first API

private const int HWND_BROADCAST = 0xffff;

 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int RegisterWindowMessage(string lpString);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool SendNotifyMessage(int hWnd, int Msg, int wParam, int lParam);

Appendix 1 Code

private string msgstr = "MYMESSAGE";              
public int  msg = RegisterWindowMessage(msgstr);
                if (msg == 0)
                {
                    MessageBox.Show(Marshal.GetLastWin32Error().ToString());
                }

                //SendNotifyMessage(HWND_BROADCAST, msg, 4848484, 8484865);

                SendNotifyMessage(HWND_BROADCAST, msg, 0, 0);

                MessageBox.Show(Marshal.GetLastWin32Error().ToString());

Appendix 2 Code

 static readonly int msg = RegisterWindowMessage("MYMESSAGE");
     protected override void WndProc(ref Message m)
        {
            if (m.Msg == msg)
            {
                MessageBox.Show(m.Msg.ToString() + " = from wndproc");
            }
            base.WndProc(ref m);
         }

Will anyone point out what is the problem with this code. I suspect the problem is in SendNotifyMessage

lparam and wparam options

Someone will offer me another alternative to achieve this behavior!

+3
source share
3 answers

HWND_BROADCAST quite dangerous .. I know this is very unlikely, but what if another application also processed your message?

, , http://msdn.microsoft.com/en-us/library/ms644953.aspx

( WINAPI) - GetLastError. , , , , ( , ). , , ​​ , UAC, (Vista +).

UIPI, , GetLastError, 5 ( ).

+3
  • , . , , . , , , , , (, , ), , , . ? ? , !

  • , RegisterWindowMessage() SendNotifyMessage(), . , SendNotifyMessage(), , SendNotifyMessage()?

  • 2 WndProc, . ? , ? ? "MYMESSAGE", 2?

  • HWND_BROADCAST . , ?

  • , RegisterWindowMessage() 2 . , ?

  • "MYMESSAGE" - . , - , , , ?

+5

,

.

public partial class Server : Form
    {
        private UInt32 msg;
        public Server()
        {
            InitializeComponent();

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            msg = RegisterWindowMessage("THIS_IS_MY_UNIQUE_MESSAGE");
        }

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern uint RegisterWindowMessage(string lpString);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool SendNotifyMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

        private void SendMessage(object sender, EventArgs e)
        {
            var retval = SendNotifyMessage(new IntPtr(-1), msg, 0, 0);
        }


    }

public partial class Client : Form
    {
        public Client()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern uint RegisterWindowMessage(string lpString);


        private static UInt32 GetMessage()
        {
            return RegisterWindowMessage("THIS_IS_MY_UNIQUE_MESSAGE");
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == (int)GetMessage())
            {
                MessageBox.Show(@"Hello, from server");
            }
            base.WndProc(ref m);
        }
    }
+2

All Articles