[Help]AccessibleObjectFromWindow获取窗口IAccessible接口指针问题。

sos_2010123 2010-12-09 05:02:38
//HTML相关头文件
#include <windows.h>
#include <atlbase.h>
CComModule _Module;
#include <mshtml.h>
#include <atlcom.h>
#include <oleacc.h>
#include <comdef.h>

//系统API相关头文件
#include <string.h>
#include <fstream.h>
#include <stdlib.h>
#include <Tlhelp32.h>
#include "Shlwapi.h"
#include <Vector>
#pragma comment(lib,"shlwapi.lib")

//全局变量
HWND hWndMainWindow;
IAccessible *paccMainWindow = NULL;
HRESULT hr;

//通过窗口句柄得到窗口的IAccessible接口指针
void CURLChekcDlg::OnButton2()
{
//得到标题为"运行"的窗口的句柄
if(!(hWndMainWindow=(HWND)FindWindow(NULL, "运行")))
{
AfxMessageBox("没有发现窗口");
}

else
{
//通过窗口句柄得到窗口的IAccessible接口指针
//编译时提示第二个参数未定义
//把第二个参数设置为0,返回结果“不是S_OK”
if(S_OK == (hr = AccessibleObjectFromWindow(hWndMainWindow,OBJID_WINDOW, IID_IAccessible,(void**)&paccMainWindow)))
{
AfxMessageBox("成功获取接口指针");

//……我们可以通过这个指针paccMainWindow进行操作。
paccMainWindow->Release();
}
}
}


参考的相关资料:
MS Active Accessibility 接口技术编程尝试
http://www.vckbase.com/document/viewdoc/?id=883

IAccessible ( 二 )
http://www.cnblogs.com/GnagWang/archive/2010/04/20/1716449.html

如何获取网页密码框中的密码
http://www.vckbase.com/document/viewdoc/?id=288
...全文
1932 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
sos_2010123 2010-12-10
  • 打赏
  • 举报
回复
原创:取得火狐地址栏网址!(Active Accessibility的应用)
http://hi.baidu.com/hidetray/blog/item/f60a58a695055c92d1435809.html

模块:       
引用oleacc.dll
Option Explicit
'BY 小江
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function AccessibleObjectFromWindow Lib "oleacc" (ByVal hwnd As Long, ByVal dwId As Long, riid As Type_GUID, ppvObject As Object) As Long
Private Declare Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As IAccessible, ByVal iChildStart As Long, ByVal cChildren As Long, rgvarChildren As Variant, pcObtained As Long) As Long

Private Const CHILDID_SELF As Long = &H0&
Private Const OBJID_CLIENT As Long = &HFFFFFFFC
Private Type Type_GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type

'*************************************************************************
'**函 数 名:GetFirefoxURL
'**输 入:sVersion(String) -火狐的版本,目前我只试过“1.6a1、3.0.5”。
'**输 出:(String) -网址
'*************************************************************************
Public Function GetFirefoxURL(sVersion As String) As String
Dim hFirefoxWnd As Long
Dim hClildWnd As Long
Dim udtIA As IAccessible
Dim udtNewIA As IAccessible
Dim tmpLoad As IAccessible
Dim tg As Type_GUID
Dim lRet As Long
Dim lStart As Long
Dim udtChildIA() As Variant
Dim lCount As Long
Dim lNewCount As Long
Dim i As Long

hFirefoxWnd = FindWindow("MozillaUIWindowClass", vbNullString)
If hFirefoxWnd = 0 Then
MsgBox "没有打开火狐!", vbCritical, "警告"
Exit Function
End If
hClildWnd = FindWindowEx(hFirefoxWnd, 0, "MozillaWindowClass", vbNullString)
Debug.Print Hex$(hClildWnd)
'以下部分代码参考了《Advanced Microsoft Visual Basic 6.0 Second Edition》第16章Microsoft Active Accessibility部分
'定义IAccessible对象的GUID{618736E0-3C3D-11CF-810C-00AA00389B71}
With tg
.Data1 = &H618736E0
.Data2 = &H3C3D
.Data3 = &H11CF
.Data4(0) = &H81
.Data4(1) = &HC
.Data4(2) = &H0
.Data4(3) = &HAA
.Data4(4) = &H0
.Data4(5) = &H38
.Data4(6) = &H9B
.Data4(7) = &H71
End With
lRet = AccessibleObjectFromWindow(hFirefoxWnd, OBJID_CLIENT, tg, udtIA)

lCount = udtIA.accChildCount
Debug.Print lCount
ReDim udtChildIA(lCount - 1) As Variant
lRet = AccessibleChildren(udtIA, lStart, lCount - 1, udtChildIA(0), lNewCount)

If sVersion = "1.6" Then
For i = 0 To lNewCount - 1
If IsObject(udtChildIA(i)) = True Then
Set tmpLoad = udtChildIA(i)

If tmpLoad.accRole(CHILDID_SELF) = 22 And tmpLoad.accChildCount = 3 Then
Set udtNewIA = tmpLoad
lCount = tmpLoad.accChildCount
Exit For
End If
End If
Next i
ReDim udtChildIA(lCount - 1) As Variant
lRet = AccessibleChildren(udtNewIA, lStart, lCount - 1, udtChildIA(0), lNewCount)
End If

For i = 0 To lNewCount - 1
If IsObject(udtChildIA(i)) = True Then
Set tmpLoad = udtChildIA(i)
If tmpLoad.accChildCount = 8 Then
Set udtNewIA = tmpLoad
lCount = tmpLoad.accChildCount
Exit For
End If
End If
Next i

ReDim udtChildIA(lCount - 1) As Variant
lRet = AccessibleChildren(udtNewIA, lStart, lCount, udtChildIA(0), lNewCount)
For i = 0 To lNewCount - 1
If IsObject(udtChildIA(i)) = True Then
Set tmpLoad = udtChildIA(i)
If tmpLoad.accName(CHILDID_SELF) = "地址" Then
Set udtNewIA = tmpLoad
lCount = tmpLoad.accChildCount
GetFirefoxURL = tmpLoad.accValue(CHILDID_SELF)
Exit For
End If
End If
Next i

If sVersion = "3.0.5" Then
ReDim udtChildIA(lCount - 1) As Variant
lRet = AccessibleChildren(udtNewIA, lStart, lCount, udtChildIA(0), lNewCount)
For i = 0 To lNewCount - 1
If IsObject(udtChildIA(i)) = True Then
Set tmpLoad = udtChildIA(i)
If tmpLoad.accName(CHILDID_SELF) = "地址" Then
GetFirefoxURL = tmpLoad.accValue(CHILDID_SELF)
Exit For
End If
End If
Next i
End If

End Function

form1

Option Explicit

Private Sub Command1_Click()
Text1.Text = GetFirefoxURL("3.0.5")
End Sub

Private Sub Form_Load()
Text1.Text = GetFirefoxURL("3.0.5")
End Sub
sos_2010123 2010-12-10
  • 打赏
  • 举报
回复
Microsoft© Active Accessibility 2.0 is a COM-based technology that improves the
way accessibility aids work with applications running on Microsoft Windows?. It
provides dynamic-link libraries that are incorporated into the operating system
as well as a COM interface and application programming elements that provide
reliable methods for exposing information about user interface elements.

是否还缺少相关库文件呢?
sos_2010123 2010-12-10
  • 打赏
  • 举报
回复
VC6环境下没有定义这个变量,还是需要别的头文件呢?

能否自己定义 OBJID_WINDOW 这个变量呢?
yihandrensunyong 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lsq19871207 的回复:]
引用 3 楼 sos_2010123 的回复:
是否需要写代码加载 OLEACC.DLL和OLEACC.LIB ?


不需要

很明显,提示说OBJID_WINDOW为定义说明你少了定义这个东东的头文件,Windows 对象标识符在winuser.h中定义
[/Quote]
顶个再说
yihandrensunyong 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lsq19871207 的回复:]
引用 3 楼 sos_2010123 的回复:
是否需要写代码加载 OLEACC.DLL和OLEACC.LIB ?


不需要

很明显,提示说OBJID_WINDOW为定义说明你少了定义这个东东的头文件,Windows 对象标识符在winuser.h中定义
[/Quote]
顶个再说
sos_2010123 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lsq19871207 的回复:]
引用 3 楼 sos_2010123 的回复:
是否需要写代码加载 OLEACC.DLL和OLEACC.LIB ?


不需要

很明显,提示说OBJID_WINDOW为定义说明你少了定义这个东东的头文件,Windows 对象标识符在winuser.h中定义
[/Quote]
---------------------
编译环境是VC6.0

(1)加了winuser.h这个头文件,还是不可以。。。
//HTML相关头文件
#include <windows.h>
#include <winuser.h> //Add 1209

#include <atlbase.h>
CComModule _Module;
#include <mshtml.h>
#include <atlcom.h>
#include <oleacc.h>
#include <comdef.h>

//系统API相关头文件
#include <string.h>
#include <fstream.h>
#include <stdlib.h>
#include <Tlhelp32.h>
#include "Shlwapi.h"
#include <Vector>
#pragma comment(lib,"shlwapi.lib")
#pragma comment(lib,"oleacc.lib")


(2)自己定义一个宏变量,也是"不返回S_OK"
#define OBJID_WINDOW 0x00000000
信阳毛尖 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sos_2010123 的回复:]
是否需要写代码加载 OLEACC.DLL和OLEACC.LIB ?
[/Quote]

不需要

很明显,提示说OBJID_WINDOW为定义说明你少了定义这个东东的头文件,Windows 对象标识符在winuser.h中定义

信阳毛尖 2010-12-09
  • 打赏
  • 举报
回复
加上头文件winuser.h
sos_2010123 2010-12-09
  • 打赏
  • 举报
回复
是否需要写代码加载 OLEACC.DLL和OLEACC.LIB ?
sos_2010123 2010-12-09
  • 打赏
  • 举报
回复
  //通过窗口句柄得到窗口的IAccessible接口指针
//编译时提示第二个参数未定义
//把第二个参数设置为0,返回结果“不是S_OK”
if(S_OK == (hr = AccessibleObjectFromWindow(hWndMainWindow,OBJID_WINDOW, IID_IAccessible,(void**)&paccMainWindow)))
{
AfxMessageBox("成功获取接口指针");

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧