Reputation: 15973
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!
Upvotes: 3
Views: 12363
Reputation: 10306
The following code works very well,
Server side.
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);
}
}
Client Side
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);
}
}
Upvotes: 3
Reputation: 62005
There exists no such thing as code with a problem. There only exists code that does not do what you expected it to do. If you want us to tell you what the problem is with the above code, you should tell us what you expected it to do, (okay, we can guess that,) but most importantly, what it did instead. Did you get an error? Did it just silently fail to work? That's an important part of a question, you know!
You are making use of methods like RegisterWindowMessage() and SendNotifyMessage() the definitions of which you are not showing to us. How are we supposed to tell whether the problem is with SendNotifyMessage() when we do not know how SendNotifyMessage() has been declared?
Your Application 2 has a WndProc in which you expect to receive messages for a window. Has it been properly registered? Are you sure that it works? Does it receive other window messages? Does it receive "MYMESSAGE" if you send it from within Application 2?
HWND_BROADCAST only sends messages to top-level windows. Are you sure your window is a top-level window?
You are not checking whether the call to RegisterWindowMessage() in Application 2 was successful or not. How about checking that first of all?
"MYMESSAGE" is not a very good name for a message. How about using something more unique, like your first name plus last name, or creating a guid and using its string representation as a name for your message?
Upvotes: 4
Reputation: 21979
HWND_BROADCAST
is 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+).
When a message is blocked by UIPI the last error, retrieved with GetLastError, is set to 5 (access denied).
Upvotes: 3