473,382 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

FindWindow, RegisterWindowMessage, SendMessage

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
5 27201
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
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
>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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: James Burrow | last post by:
Hi, I am trying to write a 2 .net programs that can communicate via windows messages. I have achieved this but only just passing ints. When i try and pass a string, i get junk out. I guess this...
0
by: TonyG | last post by:
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....
4
by: LamNgo | last post by:
Hi group, I have two application that need to communicate with each other, one written in C++ and the other in VB.NET. To communicate, I use "SendMessage" API to send message from C++ app to...
18
by: Lars Netzel | last post by:
Hello! Thanx to this newgroup I have finally, with the help of you guys, gotten this to work halfway.. but the final action is still not working, clicking the "Button2" thru SendMessage(). ...
4
by: paraidy | last post by:
Hi all, reading some examples in this group i'm trying to send a text to notepad, but it doesn't work, can someone to correct my code? Thx Private Declare Function FindWindow Lib "user32.dll"...
22
by: SQACSharp | last post by:
I'm trying to get the control name of an editbox in another window. The following code set the value "MyPassword" in the password EditBox but it fail to return the control name of the EditBox. ...
2
by: Alex | last post by:
Dear coleagues, I am trying to call FindWindow function but it seems not to be working. It returns a loooooong value even if the Window "SoftMaxPro GxP" is not started. I can't even find a...
1
by: Necromis | last post by:
Ok, I have gotten my head around things better regarding SendMessage and FindWindow functions. However, I am running into an issue with my code still. The program I am working with is EXTRA! by...
1
by: stevus06 | last post by:
I have two projects in Visual Studio 2008: One a C# app, the other a c++ dll. I have figured out to get my dll started from the c# app and can confirm it is running along with the C# GUI. The...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.