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

推荐订阅源

D
DataBreaches.Net
V
Vulnerabilities – Threatpost
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
Y
Y Combinator Blog
T
Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
MyScale Blog
MyScale Blog
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
量子位
I
Intezer
Simon Willison's Weblog
Simon Willison's Weblog
C
Cybersecurity and Infrastructure Security Agency CISA
L
Lohrmann on Cybersecurity
L
LINUX DO - 最新话题
The Register - Security
The Register - Security
T
Tailwind CSS Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
T
Troy Hunt's Blog
Stack Overflow Blog
Stack Overflow Blog
Cloudbric
Cloudbric
S
Secure Thoughts
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
L
LangChain Blog
Recorded Future
Recorded Future
小众软件
小众软件
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
人人都是产品经理
人人都是产品经理
F
Full Disclosure
O
OpenAI News
Webroot Blog
Webroot Blog
A
Arctic Wolf
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
H
Heimdal Security Blog
B
Blog RSS Feed
Vercel News
Vercel News

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