C++ IUIAutomation获取微信消息

C++ IUIAutomation获取微信消息

前言

直接上代码

代码如下(示例):

#include 
#include 
#include 
#include 
#include 
#include 
#include   
#include 
#pragma comment(lib, "comsuppw.lib")
using namespace std;


HWND g_hwd=NULL;
int _tmain(int argc, _TCHAR* argv[])
{
     
	CoInitialize(NULL);
	while(1)
	{
     
		g_hwd = FindWindow(_T("WeChatMainWndForPC"),_T("微信"));		//获取微信窗口的句柄
		if (g_hwd == NULL)
		{
     
			return 0;
		}
		else
		{
     
			WeChatInfo::UIAWeChat();
		}
		Sleep(1000);
	}
	CoUninitialize();

	return 0;
}

WeChatInfo::WeChatInfo()
{
     


}

WeChatInfo::~WeChatInfo()
{
     

}

SAFEARRAY* WeChatInfo::runtimeId = NULL;

void WeChatInfo::UIAWeChat()
{
     
	IUIAutomation *_automation;
	HRESULT hr = S_OK;
	if (FAILED(hr))
	{
     
		cout << "11" << endl;
		return;
	}
	else
	{
     
		hr = CoCreateInstance(CLSID_CUIAutomation, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&_automation));
		if (FAILED(hr))
		{
     
			cout << "22" << endl;
			return;
		}
		else
		{
     
			IUIAutomationElement *element = NULL;

			hr = _automation->ElementFromHandle(g_hwd, &element);
			if (FAILED(hr))
			{
     
				cout << "44" << endl;
				return;
			}

			if (SUCCEEDED(hr) && element != NULL)
			{
     
				IUIAutomationCondition* textPatternCondition;

				WCHAR wcPaneName[]=L"列表项目";  
				VARIANT trueVar;
				trueVar.vt=VT_BSTR;
				trueVar.bstrVal=SysAllocString(wcPaneName);

				//hr = _automation->CreateTrueCondition(&textPatternCondition);
				hr = _automation->CreatePropertyCondition(UIA_LocalizedControlTypePropertyId, trueVar, &textPatternCondition);  //设置条件接口
				if (FAILED(hr))
				{
     
					cout << "55" << endl;
					return;
				}
				else
				{
     
					IUIAutomationElementArray *textElement;
					hr = element->FindAll(TreeScope_Descendants, textPatternCondition, &textElement);
					if (FAILED(hr))
					{
     
						cout << "66" << endl;
						return;
					}
					else if (textElement == NULL)
					{
     
						cout << "77" << endl;
						return;
					}
					else
					{
      
						//获取控件内的文本
						int i_length;
						BSTR name;
						string plpszText;
						hr = textElement->get_Length(&i_length);
						if (FAILED(hr))
						{
      
							return;
						}
						
						IUIAutomationElement* pElement;
						hr = textElement->GetElement(i_length-1, &pElement);  //微信消息列表最上面一条消息
						if (FAILED(hr))
						{
      
							return;
						}

						WCHAR PaneName[]=L"按钮";  //消息对象名称
						VARIANT Var;
						Var.vt=VT_BSTR;
						Var.bstrVal=SysAllocString(PaneName);

						hr = _automation->CreatePropertyCondition(UIA_LocalizedControlTypePropertyId, Var, &textPatternCondition);
						if (FAILED(hr))
						{
      
							return;
						}
						IUIAutomationElementArray *ptextElement;
						pElement->FindAll(TreeScope_Subtree,textPatternCondition,&ptextElement);

						hr = ptextElement->get_Length(&i_length);
						if (FAILED(hr))
						{
      
							return;
						}

						IUIAutomationElement* p_Element;
						hr = ptextElement->GetElement(i_length-1, &p_Element);
						if (FAILED(hr))
						{
      
							return;
						}

						
						hr = p_Element->get_CurrentName(&name);
						if (FAILED(hr))
						{
     
							cout << "99" << endl;
							return;
						}

						plpszText = _com_util::ConvertBSTRToString(name);  
						SysFreeString(name); 

						if (FAILED(hr))
						{
     
							cout << "88" << endl;
							return;
						}
						

						
						hr = pElement->get_CurrentName(&name);
						if (FAILED(hr))
						{
     
							cout << "99" << endl;
							return;
						}

						plpszText += ":";
						plpszText +=  _com_util::ConvertBSTRToString(name);  
						SysFreeString(name); 

						SAFEARRAY *pruntimeId;
						BOOL trv;
						hr= pElement->GetRuntimeId(&pruntimeId);

						_automation->CompareRuntimeIds(pruntimeId,runtimeId,&trv);
						if(trv == TRUE)
						{
     
							return;
						}
						else
						{
     
							runtimeId = pruntimeId;
							printf("%s\n",plpszText.c_str());
						}
					}
					textPatternCondition->Release();
				}
			}
			_automation->Release();
		}
	}
	
	cout << "okok" << endl;
	return ;
}

你可能感兴趣的:(c++)