FindWindow, RegisterWindowMessage, SendMessage

G

Guest

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
 
K

Kevin Yu [MSFT]

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."
 
G

Guest

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.
 
M

Mattias Sjögren

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
 
K

Kevin Yu [MSFT]

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."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top