【API求教】C#根据flash句柄获取网页内flash的对象
根据句柄获取网页内flash的对象
首先我已经获取了flash的句柄,通过webbrowser插件
结构如下:webHandle:1900946
Handle:2031998|windowsname:|classname:Shell Embedding
Handle:2097774|windowsname:|classname:Shell DocObject View
Handle:3408466|windowsname:|classname:Internet Explorer_Server
Handle:1245802|windowsname:|classname:MacromediaFlashPlayerActiveX
网上有人实现了 不过是VB的代码 我调用api后老是错误。http://hi.baidu.com/%C4%BE%D2%D7%D8%A3/blog/item/9e12eb61cbc090c18db10dc8.html
AccessibleObjectFromWindow(phwnd, (uint)OBJID.CLIENT, ref guid, ref flash_main);获取的flash对象为空的、、
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Accessibility;
namespace webgame
{
public partial class mian : Form
{
public mian()
{
InitializeComponent();
}
//①dll导入
//FindWindow
[DllImport("user32.dll")]
public static extern int FindWindow(string className, string title);
//FindWindowEx
[DllImport("user32.dll")]
public static extern int FindWindowEx(int parent, int next, string ClassName, string title);
//SetWindowText
[DllImport("user32")]
public static extern int SetWindowText(int handle, string title);
//GetWindowText
[DllImport("user32.dll")]
private static extern int GetWindowText(int hWnd, StringBuilder title, int size);
//GetClassName
[DllImport("user32.dll")]
private static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount);
//SendMessage
[DllImport("User32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
//EnumWindows 需回调
[DllImport("user32.dll")]
//public static extern int EnumWindows(EnumWindowsProc lpfn, int lParam);
public static extern int EnumWindows(CallBack lpfn, int lParam);
//EnumChildWindows 需回调
[DllImport("user32.dll")]
public static extern int EnumChildWindows(int Parenthandle, CallBack lpfn, int lParam);
//获取窗口标题
[DllImport("user32", SetLastError = true)]
public static extern int GetWindowText(
IntPtr hWnd, //窗口句柄
StringBuilder lpString, //标题
int nMaxCount //最大值
);
//根据坐标获取窗口句柄
[DllImport("user32")]
private static extern IntPtr WindowFromPoint(
Point Point //坐标
);
//获取pid根据handle
[DllImport("user32", EntryPoint = "GetWindowThreadProcessId")]
private static extern int GetWindowThreadProcessId(int hwnd, out int pid);
//根据handle获取对象
[DllImport("atl.dll")]
private static extern int AtlAxGetControl(int hwnd,ref Object obj);
//获取对象
[DllImport("Oleacc.dll")]
public static extern int AccessibleObjectFromWindow(
IntPtr hwnd,
uint dwObjectID,
ref Guid refID,
[In, Out, MarshalAs(UnmanagedType.IUnknown)] ref object ppvObject);
[DllImport("Oleacc.dll")]
public static extern int WindowFromAccessibleObject(
Object pacc,
out IntPtr phwnd);
[DllImport("Oleacc.dll")]
public static extern int AccessibleChildren(
Object paccContainer,
int iChildStart,
int cChildren,
[Out] object[] rgvarChildren,
out int pcObtained);
internal enum OBJID : uint
{
WINDOW = 0x00000000,
SYSMENU = 0xFFFFFFFF,
TITLEBAR = 0xFFFFFFFE,
MENU = 0xFFFFFFFD,
CLIENT = 0xFFFFFFFC,
VSCROLL = 0xFFFFFFFB,
HSCROLL = 0xFFFFFFFA,
SIZEGRIP = 0xFFFFFFF9,
CARET = 0xFFFFFFF8,
CURSOR = 0xFFFFFFF7,
ALERT = 0xFFFFFFF6,
SOUND = 0xFFFFFFF5,
}
//②定义委托
public delegate bool CallBack(int hwnd, int lParam);
//③回调函数
public static bool EnumChildWindowsCallBack(int hwnd, int lParam)
{
try
{
StringBuilder className = new StringBuilder(100);
StringBuilder windowsName = new StringBuilder(100);
int a=GetClassName(hwnd, className, className.Capacity);
int b=GetWindowText(hwnd, windowsName, windowsName.Capacity);
note += "Handle:" + hwnd + "|windowsname:" + windowsName + "|classname:" + className + "\r\n";
}
catch (Exception e)
{ MessageBox.Show(e.ToString()); }
return true;
}
//全局变量
static String note = null;
public const int WM_SETTEXT = 0x000C;
public const int WM_CLICK = 0x00F5;
public const int CHILDID_SELF = 0;
public const int CHILDID_1 = 1;
public const int OBJID_CLIENT = -4;
//载入游戏
private void btn_start_Click(object sender, EventArgs e)
{
try
{
String url = tbox_url.Text;
//web1.Navigate(new Uri(url));
this.web1.Url = new System.Uri(url, System.UriKind.Absolute);
}
catch (System.UriFormatException)
{
return;
}
}
private void button1_Click(object sender, EventArgs e)
{
note = null;
//③调用实例
CallBack enumCldWndCallback = new CallBack(EnumChildWindowsCallBack);//callback
int hwnd = web1.Handle.ToInt32();
int a = EnumChildWindows(hwnd, enumCldWndCallback, 0);
note = "webHandle:"+hwnd.ToString()+"\r\n"+note;
tbox_show.Text = note;
//MessageBox.Show(a.ToString());
}
private void btn_hdlinfo_Click(object sender, EventArgs e)
{
note = null;
tbox_show.Text = null;
int hwnd = Convert.ToInt32(tbox_handle.Text);
CallBack enumCldWndCallback = new CallBack(EnumChildWindowsCallBack);//callback
int a = EnumChildWindows(hwnd, enumCldWndCallback, 0);
note = "parentHandle:" + hwnd.ToString() + "\r\n" + note;
tbox_show.Text = note;
MessageBox.Show(a.ToString());
}
private void btn_childobject_Click(object sender, EventArgs e)
{
int hwnd = Convert.ToInt32(tbox_handle.Text);
object flash_main = null;
Guid guid = new Guid("{618736E0-3C3D-11CF-810C-00AA00389B71}");
IntPtr phwnd = new IntPtr(hwnd);
int a = AccessibleObjectFromWindow(phwnd, (uint)OBJID.CLIENT, ref guid, ref flash_main);
IAccessible flash = (IAccessible)flash_main;
int childCount = flash.accChildCount;
object[] windowChildren = new object[childCount];
int pcObtained;
int a1= AccessibleChildren(flash_main, 0, childCount, windowChildren, out pcObtained);
string accName;
int accRole;
string accValue;
IAccessible cld = (IAccessible)windowChildren[0];
int aa = cld.accChildCount;
}
private object[] GetAccessibleChildren(IAccessible paccContainer)
{
object[] rgvarChildren = new object[paccContainer.accChildCount];
int pcObtained;
AccessibleChildren(paccContainer, 0, paccContainer.accChildCount, rgvarChildren, out pcObtained);
return rgvarChildren;
}
}
}