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

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

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