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

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
F
Full Disclosure
A
About on SuperTechFans
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
Know Your Adversary
Know Your Adversary
K
Kaspersky official blog
L
LINUX DO - 热门话题
Recorded Future
Recorded Future
C
Cisco Blogs
M
MIT News - Artificial intelligence
T
Tenable Blog
G
GRAHAM CLULEY
月光博客
月光博客
Recent Announcements
Recent Announcements
V
Visual Studio Blog
IT之家
IT之家
T
The Exploit Database - CXSecurity.com
The GitHub Blog
The GitHub Blog
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
P
Privacy International News Feed
P
Proofpoint News Feed
I
Intezer
博客园 - 叶小钗
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
小众软件
小众软件
Hacker News - Newest:
Hacker News - Newest: "LLM"
O
OpenAI News
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
The Cloudflare Blog
Spread Privacy
Spread Privacy
酷 壳 – CoolShell
酷 壳 – CoolShell
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
B
Blog RSS Feed

博客园 - 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!