[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
...全文
904 点赞 收藏 13
写回复
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("成功获取接口指针");
回复 点赞
发动态
发帖子
VC/MFC
创建于2007-09-28

7889

社区成员

42.1w+

社区内容

VC/MFC相关问题讨论
社区公告
暂无公告