By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,863 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,863 IT Pros & Developers. It's quick & easy.

FindWindow, RegisterWindowMessage, SendMessage

P: n/a
I need to send a message to a window in another application. The name of the
window is known at design time and set in the constant App2_MONITOR_CAPTION.
The message is defined as X_GenerateEvent.
The following VB6 code works. I assume this is possible in C#. How do you do
it?

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
As Long

Private Declare Function RegisterWindowMessage Lib "user32" Alias
"RegisterWindowMessageA" (ByVal lpString As String) As Long

Private Const App2_MONITOR_CAPTION = "App2 Custom Monitor"
Private App2_GenerateEvent As Long

Private Sub Command1_Click()
Dim handle As Long
handle = FindWindow(vbNullString, App2_MONITOR_CAPTION)
If handle = 0 Then Exit Sub
App2_GenerateEvent = RegisterWindowMessage("X_GenerateEvent")
SendMessage handle, App2_GenerateEvent, 0, 0
End Sub

Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know how to send message to
a certain window in C# using APIs. If there is any misunderstanding, please
feel free to let me know.

We can use platform invoke to achieve this. Here is an example:

1. Use DllImport to declare APIs in the class.

[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, UIntPtr
wParam,IntPtr lParam);

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

2. Use the following code to send message.

IntPtr handle = FindWindow(string.Empty, App2_MONITOR_CAPTION);
if(handle != IntPtr.Zero)
{
uint App2_GenerateEvent = RegisterWindowMessage("X_GenerateEvent");
SendMessage(handle, App2_GenerateEvent, UIntPtr.Zero, IntPtr.Zero);
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #2

P: n/a
Your understanding is correct, but to clarify ... the window is owned by
another process.

I received a build error until I added
using System.Runtime.InteropServices;

Now, the FindWindow() is returning 0, so
if(handle != IntPtr.Zero)
is always false.
"Kevin Yu [MSFT]" wrote:
Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know how to send message to
a certain window in C# using APIs. If there is any misunderstanding, please
feel free to let me know.

We can use platform invoke to achieve this. Here is an example:

1. Use DllImport to declare APIs in the class.

[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, UIntPtr
wParam,IntPtr lParam);

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

2. Use the following code to send message.

IntPtr handle = FindWindow(string.Empty, App2_MONITOR_CAPTION);
if(handle != IntPtr.Zero)
{
uint App2_GenerateEvent = RegisterWindowMessage("X_GenerateEvent");
SendMessage(handle, App2_GenerateEvent, UIntPtr.Zero, IntPtr.Zero);
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #3

P: n/a
>Now, the FindWindow() is returning 0, so
if(handle != IntPtr.Zero)
is always false.


Try passing null insteand of String.Empty as the first argument to
FindWindow.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #4

P: n/a
Kevin and Mattias, thanks to you both.

It is working now.

"Mattias Sjögren" wrote:
Now, the FindWindow() is returning 0, so
if(handle != IntPtr.Zero)
is always false.


Try passing null insteand of String.Empty as the first argument to
FindWindow.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 17 '05 #5

P: n/a
You're welcome. Thanks for sharing your experience with all the people
here. If you have any questions, please feel free to post them in the
community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.