1
我試圖重新使用代碼(http://msdn.microsoft.com/en-us/library/windows/desktop/ff625913(v=vs.85).aspx#FindByName),但我想把它包裝在一個簡單的類。當我運行這個代碼時,它落在:UI自動化CreatePropertyCondition 0xc0000005
hr = g_pAutomation->CreatePropertyCondition(UIA_ClassNamePropertyId, varProp, &pCondition);
與0xc0000005。
我知道,這是因爲空指針或斷指針。但是,當我運行這個代碼沒有任何類(從main()
),它的作品完美。
我應該閱讀哪些內容以及從何處瞭解其發生的原因?
#pragma once
#include <UIAutomation.h>
class Automator
{
protected:
IUIAutomation* g_pAutomation;
IUIAutomationElement* pRoot;
IUIAutomationElementArray* pArrFound;
IUIAutomationElement* pFound;
public:
Automator(void);
~Automator(void);
void ClearResources(void);
void FindAllWindows(void);
};
#include "Automator.h"
Automator::Automator(void)
{
pRoot = NULL;
pArrFound = NULL;
pFound = NULL;
g_pAutomation = NULL;
CoInitialize(NULL);
HRESULT hr;
hr = CoCreateInstance(__uuidof(CUIAutomation), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IUIAutomation), (void**)&g_pAutomation);
if(FAILED(hr))
ClearResources();
else
{
hr = g_pAutomation->GetRootElement(&pRoot);
if (FAILED(hr) || pRoot == NULL)
ClearResources();
}
}
Automator::~Automator(void)
{
ClearResources();
}
//
//Doesn't work
//
void Automator::FindAllWindows(void)
{
VARIANT varProp;
varProp.vt = VT_BSTR;
varProp.bstrVal = L"";
IUIAutomationCondition* pCondition;
HRESULT hr = NULL;
if (g_pAutomation != NULL)
{
hr = g_pAutomation->CreatePropertyCondition(UIA_ClassNamePropertyId, varProp, &pCondition);
if(FAILED(hr))
{
if (pCondition != NULL)
pCondition->Release();
ClearResources();
}
else
{
pRoot->FindAll(TreeScope_Subtree, pCondition, &pArrFound);
}
}
if(pCondition != NULL)
pCondition->Release();
}
void Automator::ClearResources(void)
{
if (pRoot != NULL)
pRoot->Release();
if (pArrFound != NULL)
pArrFound->Release();
if (pFound != NULL)
pFound->Release();
if (g_pAutomation != NULL)
g_pAutomation->Release();
CoUninitialize();
}