I am working in an application in this application I shall wait for some event in application 1 and when this event happen I shall sendmessage to application 2 which will perform something.
First API declaration
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);
Application 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());
Application 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 somebody point out what is problem with this code.
I suspect there is problem in SendNotifyMessage
lparam and wparam
parameters
Will somebody suggest me any other alternative to achieve this behaviour!
HWND_BROADCASTis pretty dangerous.. I know it’s highly unlikely but what if another application also handled your message??Anyway, that aside, have you taken a read of http://msdn.microsoft.com/en-us/library/ms644953.aspx
The most basic way to debug an issue with your code (as it’s WINAPI based) would be to use
GetLastError. You should always be checking the return value of methods to see if they succeed, so ensure it’s returning zero (which means it worked). If it’s not, and you get an error such as access denied, try running with either UAC Disabled or as an Administrator (Vista+).