471,348 Members | 1,228 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

beginners question API


Hello everyone, Ive been trying to get this code to work for very long
now, but I havent been able to catch the problem. Im trying to use the
SendMessage method but when I execute the program nothing happens. Thats
the best response Ive gotten from it so far, other times it mdoesnt
compile. Its a short code, Im trying simply to get SendMessage to click
a button in another specified app when I click my button. Im using c#
express with framework 2.0, the example Ive found for this were all for
older framwork versions and didnt work. Can anyone show me where Im
wrong?
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
{
public Form1()
{
InitializeComponent();
}
public class Win32
{
[DllImport("user32", EntryPoint = "GetNextWindow")]
public static extern int
GetWindow(IntPtr hwnd, int wFlag);
public const int BM_CLICK = 0x00F5;
[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;
hWnd = Win32API.FindWindow(Class1.Text, caption.Text);
Win32API.SetForegroundWindow(hWnd);
Win32.SendMessage(hWnd, Win32.BM_CLICK, 0, 0);
}
}
}
*** Sent via Developersdex http://www.developersdex.com ***
Feb 11 '06 #1
3 1463
[DllImport("user32", EntryPoint = "GetNextWindow")]
public static extern int
GetWindow(IntPtr hwnd, int wFlag);
There's no function called GetNextWindow exported from user32.dll.
GetNextWindow is simply a macro calling GetWindow, so it will probably
work better if you just remove the EntryPoint.

hWnd = Win32API.FindWindow(Class1.Text, caption.Text);
Win32API.SetForegroundWindow(hWnd);
Win32.SendMessage(hWnd, Win32.BM_CLICK, 0, 0);


FindWindow only searches top level windows. Do you really have a
button floating around on the desktop without a parent?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 11 '06 #2
I understand your first point about the GetNextWindow, but about the
second part with FindWindow, that is the only way I knew how to do it.
What can I use instead? Thanks for your help by the way!

*** Sent via Developersdex http://www.developersdex.com ***
Feb 11 '06 #3
>I understand your first point about the GetNextWindow, but about the
second part with FindWindow, that is the only way I knew how to do it.
What can I use instead? Thanks for your help by the way!


If the button is a control in some other window, you first have to
find the top level window with FindWindow, then walk your way down the
control tree with functions like FindWindowEx, GetWindow or
GetDlgItem.
Mattias

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by blah | last post: by
4 posts views Thread by Eggnog | last post: by
6 posts views Thread by William Foster | last post: by
19 posts views Thread by yltkhuu | last post: by
1 post views Thread by Ronak mishra | last post: by

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.