468,103 Members | 1,307 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,103 developers. It's quick & easy.

whats wrong with this?

Hello everyone, Ive been trying to get my application to "click" on a
button in another application using SendMessage, Ive gotten this far
but Im not sure whats wrong with this code, here is the whole
application (its small for testing purposes) and it seems that window
wraps the text, at least when I preview this post:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace test
{
public partial class Form1 : Form
{
[DllImport("user32", EntryPoint = "GetNextWindow")]
public static extern int
GetWindow(IntPtr hwnd, int wFlag);
public Form1()
{
InitializeComponent();
}
public class Win32
{
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
public const int BM_CLICK = 0xF5;
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClass, string
lpWindow);

[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg,
int wParam, int lParam);
}
private void button1_Click(object sender, EventArgs e)
{

int hWnd;
//Thread.Sleep(5000);
if ((txtWindowCaption.Text == "") && (txtClassName.Text ==
""))
{
hWnd = Win32API.FindWindowAny(0, 0);
}
else if ((txtWindowCaption.Text == "") &&
(txtClassName.Text != ""))
{
hWnd =
Win32API.FindWindowNullWindowCaption(txtClassName. Text, 0);

}
else if ((txtWindowCaption.Text != "") &&
(txtClassName.Text == ""))
{
hWnd = Win32API.FindWindowNullClassName(0,
txtWindowCaption.Text);
}
else
{
hWnd = Win32API.FindWindow(txtClassName.Text,
txtWindowCaption.Text);
}
if (hWnd == 0)
{
MessageBox.Show("Specified window is not running.",
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
// Set the window foreground.
Win32API.SetForegroundWindow(hWnd);
// if window is minimized, simply restore, otherwise
show it. Notice the
// declaration of Win32API.IsIconic defines the return
value bool
// allowing .NET to marshall the integer value to a
bool.

if (Win32API.IsIconic(hWnd))
{
Win32API.ShowWindow(hWnd, Win32API.SW_RESTORE);
}
else
{
Win32API.ShowWindow(hWnd, Win32API.SW_SHOW);
}
int a = Win32.FindWindow(txtClassName2.Text,
Title.Text);
int b = Win32.SendMessage(a, Win32.BM_CLICK, 0, 0);
}
}
}
}

Feb 6 '06 #1
4 3179
blah,

The sample you posted is not compilable there is bunch of methods that the
code use, but are not posted. The missing class is Win32API.
--

Stoitcho Goutsev (100)

"blah" <so********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hello everyone, Ive been trying to get my application to "click" on a
button in another application using SendMessage, Ive gotten this far
but Im not sure whats wrong with this code, here is the whole
application (its small for testing purposes) and it seems that window
wraps the text, at least when I preview this post:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace test
{
public partial class Form1 : Form
{
[DllImport("user32", EntryPoint = "GetNextWindow")]
public static extern int
GetWindow(IntPtr hwnd, int wFlag);
public Form1()
{
InitializeComponent();
}
public class Win32
{
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
public const int BM_CLICK = 0xF5;
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClass, string
lpWindow);

[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg,
int wParam, int lParam);
}
private void button1_Click(object sender, EventArgs e)
{

int hWnd;
//Thread.Sleep(5000);
if ((txtWindowCaption.Text == "") && (txtClassName.Text ==
""))
{
hWnd = Win32API.FindWindowAny(0, 0);
}
else if ((txtWindowCaption.Text == "") &&
(txtClassName.Text != ""))
{
hWnd =
Win32API.FindWindowNullWindowCaption(txtClassName. Text, 0);

}
else if ((txtWindowCaption.Text != "") &&
(txtClassName.Text == ""))
{
hWnd = Win32API.FindWindowNullClassName(0,
txtWindowCaption.Text);
}
else
{
hWnd = Win32API.FindWindow(txtClassName.Text,
txtWindowCaption.Text);
}
if (hWnd == 0)
{
MessageBox.Show("Specified window is not running.",
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
// Set the window foreground.
Win32API.SetForegroundWindow(hWnd);
// if window is minimized, simply restore, otherwise
show it. Notice the
// declaration of Win32API.IsIconic defines the return
value bool
// allowing .NET to marshall the integer value to a
bool.

if (Win32API.IsIconic(hWnd))
{
Win32API.ShowWindow(hWnd, Win32API.SW_RESTORE);
}
else
{
Win32API.ShowWindow(hWnd, Win32API.SW_SHOW);
}
int a = Win32.FindWindow(txtClassName2.Text,
Title.Text);
int b = Win32.SendMessage(a, Win32.BM_CLICK, 0, 0);
}
}
}
}

Feb 6 '06 #2
Stoitcho Goutsev (100) wrote:
The sample you posted is not compilable there is bunch of methods that the
code use, but are not posted. The missing class is Win32API.


.... and the rest of Form1. (It's a partial class, and only the
non-designer part has been posted.)

Jon

Feb 6 '06 #3
The part with Win32API works, I use Win32 because when I try to use
SendMessage with Win32API it gives me an error saying that it does not
contain a definition for SendMessage. how do I define it?

Feb 7 '06 #4

"blah" <so********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
| The part with Win32API works, I use Win32 because when I try to use
| SendMessage with Win32API it gives me an error saying that it does not
| contain a definition for SendMessage. how do I define it?
|

It is defined (be it not completely correct) by this:

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

but if you care about portability with 64bit versions of windows you should
definitely change int hWnd to IntPtr hWnd type, the same is true for the
return value, WPARAM and LPARAM types!
So your declaration should look like:
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg,
IntPtr wParam, IntPtr lParam);

Not sure why you get this compile error though, are you sure this is the
offending line ?

int b = Win32.SendMessage(a, Win32.BM_CLICK, 0, 0);
}
or do you call SendMessage from other classes in you project?
I would suggest you make Win32 a separate class instead of a nested one.
Willy.

Feb 7 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Chris Geerdink | last post: by
4 posts views Thread by asdf | last post: by
5 posts views Thread by Alexandre Martins | last post: by
1 post views Thread by aa | last post: by
3 posts views Thread by mahsa | last post: by
5 posts views Thread by islayer | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.