惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
Y
Y Combinator Blog
I
InfoQ
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
IT之家
IT之家
H
Help Net Security
月光博客
月光博客
S
SegmentFault 最新的问题
B
Blog
aimingoo的专栏
aimingoo的专栏
GbyAI
GbyAI
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
Vercel News
Vercel News
博客园 - 叶小钗
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
Jina AI
Jina AI
T
The Blog of Author Tim Ferriss

博客园 - gyhanonline

Function Point in Vbscript Window API in QTP Static Constructor A Go Set program A Go Set program About Inherit Code for Inter-process communicate Integrity Level Test for publish blog by word 2007 托管为什么安全 The usage of intellisense in Vs .net 2005 我的面试(七) 关于singlton的一些问题 我的面试(六) 我的面试(五) 我的面试(四)补充1 我的面试(四) 我的面试(三) 我的面试(二)
Simulate click event using widows API.
gyhanonline · 2010-05-01 · via 博客园 - gyhanonline

This article illuminates how to simulate click event using widows API. It is easily to add a click event in our WinForm/webForm application. But, if I want an external program to involve their click event, it’s a little bit tricky. It’s known that to protect an application we can’t access the application directly. If we have to control external app we could use Window API send message to tell the app what we want. I’ll use IE download dialog as a sample to explain How to click “OK” button automatically.

First of all, we need create a winForm project and drag a button onto the form. Then add click event.

Then we can add namespace “ System.Runtime.InteropServices; to import windows API.

using System.Runtime.InteropServices;

Following is the Windows API what we needed.

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern int PostMessage(IntPtr hwnd, int msg, uint wparam, uint lparam);

[DllImport("user32.dll", CharSet = CharSet.Auto)]

private extern static IntPtr SetActiveWindow(IntPtr hWnd);

[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

public static extern bool SetForegroundWindow(IntPtr hwnd);

FindWindowEx and FindWindow are used to get the handle of a dialog. FindWindow use to get the top level window. If you wanna find the child element, FindWindowEx supply a good practice.

 Before send click message, it’s necessary that active the button’s owner dialog. This is very important. If the “OK” button work at background, whatever we send the click message it would not respond. On the other hand, we have to make sure the Download Dialog has the focus before it is clicked.

So Let’s use function SetForegroundWindow to top the Download Dialog then active it by function SetActiveWindow.

From now all the prepare work has finished. And we can send the message to “OK” button thought by method PostMessage.

I’d like you using BM_Click as the click message. You can find all windows messages form MSDN site. Furthermore, spy++ is the good tool to find the value of a specified message.

Declare a message like this:

const int BM_CLICK = 0x00F5;

Completed! Using above approach, our program can click external application like a hackerJ

Sounds good, isn’t it! Following is the whole code segment:

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern int PostMessage(IntPtr hwnd, int msg, uint wparam, uint lparam);

[DllImport("user32.dll", CharSet = CharSet.Auto)]

private extern static IntPtr SetActiveWindow(IntPtr hWnd);

const int WM_CLICK = 0x00F5;

private void button2_Click(object sender, EventArgs e)

{

        IntPtr dialog= FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "");

        IntPtr child = FindWindowEx(dialog, IntPtr.Zero, "Button", "OK");

        SetForegroundWindow(dialog);

        SetActiveWindow(dialog);

        PostMessage(child, WM_CLICK, 0, 0);

}

Open a browser,Try!