UIAutomation GetTopLevelWindowByName - c++

Trying to follow the example found here:
https://learn.microsoft.com/en-us/windows/win32/winauto/uiauto-howto-find-ui-elements
WCHAR window_name[250] = L"tools";
IUIAutomationElement *window = GetTopLevelWindowByName(window_name);
IUIAutomationElement* GetTopLevelWindowByName(LPWSTR windowName)
{
if (windowName == NULL)
return NULL;
CComPtr<IUIAutomation> g_pAutomation;
VARIANT varProp;
varProp.vt = VT_BSTR;
varProp.bstrVal = SysAllocString(windowName);
if (varProp.bstrVal == NULL)
return NULL;
IUIAutomationElement* pRoot = NULL;
IUIAutomationElement* pFound = NULL;
IUIAutomationCondition* pCondition = NULL;
// Get the desktop element.
HRESULT hr = g_pAutomation->GetRootElement(&pRoot);
if (FAILED(hr) || pRoot == NULL)
goto cleanup;
// Get a top-level element by name, such as "Program Manager"
hr = g_pAutomation->CreatePropertyCondition(UIA_NamePropertyId, varProp, &pCondition);
if (FAILED(hr))
goto cleanup;
pRoot->FindFirst(TreeScope_Children, pCondition, &pFound);
cleanup:
if (pRoot != NULL)
pRoot->Release();
if (pCondition != NULL)
pCondition->Release();
VariantClear(&varProp);
return pFound;
}
But the application is crashing at the line HRESULT hr = g_pAutomation->GetRootElement(&pRoot);
With the error:
File: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29333\atlmfc\include\atlcomcli.h
Line: 204
Expression: p!=0
What I'm trying is, learn how to iterate through the elements like name, description, etc of the given window.

You must create g_pAutomation, it cannot be null, for example like this:
CoCreateInstance(
__uuidof(CUIAutomation),
NULL,
CLSCTX_ALL,
_uuidof(IUIAutomation),
(void**)&g_pAutomation);

Related

Error WBEM_E_INVALID_METHOD_PARAMETERS from ExecMethod

I am attempting to use SoftwareLicensingService::InstallProductKey to install a product key on Windows 7 through WMI/C++ in a Service. However every time I attempt to call the method via IWbemServices::ExecMethod I get 0x8004102f which is WBEM_E_INVALID_METHOD_PARAMETERS. I thought this was something to do with the product key I am passing, but I have since then tried similar code for Win32_WindowsProductActivation::ActivateOnline [which is a no parameter method available on XP] with the same error. Does anyone know what is suspect in my code fragment below (I have skipped some cleanup code to be brief) ? The same sequence of code successfully invokes other WMI methods however.
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = S_OK;
IWbemLocator *pLoc = NULL;
IWbemServices *pServices = NULL;
IWbemClassObject *pInputParamsClass = NULL;
IWbemClassObject *pInputParams = NULL;
IWbemClassObject *pOutputParams = NULL;
IWbemClassObject *pLicensingClsObj = NULL;
VARIANT vtProductKey = {0};
VARIANT vtPath = {0};
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if(FAILED(hr))
goto cleanup;
hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
_ASSERT(SUCCEEDED(hr));
if(FAILED(hr))
goto cleanup;
hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *)&pLoc);
_ASSERT(SUCCEEDED(hr) && (NULL != pLoc));
if(FAILED(hr) || (NULL == pLoc))
goto cleanup;
hr = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL,
0, 0, &pServices);
_ASSERT(SUCCEEDED(hr) && (NULL != pServices));
if(FAILED(hr) || (NULL == pServices))
goto cleanup;
hr = CoSetProxyBlanket(pServices, RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE);
_ASSERT(SUCCEEDED(hr));
if(FAILED(hr))
goto cleanup;
hr = pServices->GetObject(_bstr_t(L"SoftwareLicensingService"),
0, NULL, &pLicensingClsObj, NULL);
_ASSERT(SUCCEEDED(hr) && (NULL != pLicensingClsObj));
if(FAILED(hr) || (NULL == pLicensingClsObj))
goto cleanup;
hr = pLicensingClsObj->Get(L"__Path", 0, &vtPath, 0, 0);
_ASSERT(SUCCEEDED(hr));
if(FAILED(hr))
goto cleanup;
hr = pLicensingClsObj->GetMethod(L"InstallProductKey", 0,
&pInputParamsClass, NULL);
_ASSERT(SUCCEEDED(hr) && (NULL != pInputParamsClass));
if(FAILED(hr) || (NULL == pInputParamsClass))
goto cleanup;
hr = pInputParamsClass->SpawnInstance(0, &pInputParams);
_ASSERT(SUCCEEDED(hr) && (NULL != pInputParams));
if(FAILED(hr) || (NULL == pInputParams))
goto cleanup;
vtProductKey.vt = VT_BSTR;
vtProductKey.bstrVal = SysAllocString(L"XXXXXXXXXXXXXXXXXXXXXXXXX");
hr = pInputParams->Put(L"ProductKey", 0, &vtProductKey, 0);
_ASSERT(SUCCEEDED(hr));
if(FAILED(hr))
goto cleanup;
hr = pServices->ExecMethod(vtPath.bstrVal,
_bstr_t(L"InstallProductKey"),
0, NULL, pInputParams,
&pOutputParams, NULL);
_ASSERT(SUCCEEDED(hr) && (NULL != pOutputParams));
if(FAILED(hr) || (NULL == pOutputParams))
goto cleanup;
hr = S_OK;//all success
cleanup:
if(NULL != pLoc)
{
pLoc->Release();
pLoc = NULL;
}
if(NULL != pServices)
{
pServices->Release();
pServices = NULL;
}
(VOID)CoUninitialize();
return hr;
}
I have since figured what the issue was. The method call was being made on the class not the instance.
This code will work properly:
IEnumWbemClassObject * enum_obj;
hres = pSvc>CreateInstanceEnum(_bstr_t(L"SoftwareLicensingService"),WBEM_FLAG_RETURN_IMMEDIATELY , NULL ,&enum_obj);
IWbemClassObject * spInstance;
ULONG uNumOfInstances = 0;
hres = enum_obj->Next(10000, 1,&spInstance,&uNumOfInstances);
VARIANT path;
hres = spInstance->Get(_bstr_t("__Path"), 0,&path, 0, 0);
IWbemClassObject *results = NULL;
hres = pSvc->ExecMethod( path.bstrVal, _bstr_t( L"InstallProductKey" ), 0,
NULL,NULL,&results, NULL );
This code is not only for this class and method.Generally for any class you can get the objectPath(1st parameter of ExecMethod()) and use it.This code mentioned is for Calling a method without parameters.
The code for method calling with parameters is available here.In some cases, calling ExecMethod with input parameters for a method returns WBEM_E_INVALID_METHOD_PARAMETERS error. In that case you can get the object path first and then call GetObejct and GetMethod functions.

How to distinguish between items returned by EnumObjects of IShellFolder

I am trying to get the computers in my workgroup using Shell
HRESULT hr = S_OK;
ULONG celtFetched;
LPITEMIDLIST pidlItems = NULL;
LPITEMIDLIST netItemIdLst = NULL;
IShellFolder* pFolder = NULL;
IEnumIDList* pEnumIds = NULL;
IShellFolder* pDesktopFolder = NULL;
LPITEMIDLIST full_pid;
hr = SHGetMalloc(&pMalloc);
hr = SHGetDesktopFolder(&pDesktopFolder);
hr = SHGetSpecialFolderLocation(NULL, CSIDL_COMPUTERSNEARME, &netItemIdLst);
hr = pDesktopFolder->BindToObject(netItemIdLst, NULL,
IID_IShellFolder, (void **)&pFolder);
if(SUCCEEDED(hr))
{
hr = pFolder->EnumObjects(NULL, SHCONTF_NONFOLDERS, &pEnumIds);
if(SUCCEEDED(hr))
{
STRRET strDisplayName;
while((hr = pEnumIds->Next(1, &pidlItems, &celtFetched)) == S_OK && celtFetched > 0)
{
hr = pFolder->GetDisplayNameOf(pidlItems, SHGDN_NORMAL, &strDisplayName);
if(SUCCEEDED(hr))
{
wprintf(L"%s\n", strDisplayName.pOleStr);
}
}
}
}
But this returns the Computers, Media Devices, and Streaming Devices (is that it name?).
Like say, a computer is called OSOFEM on my workgroup... STRRET::pOleStr returns strings OSOFEM (which is the computer), OSOFEM (I think streaming device), and OSOFEM:example#email.com: (which is the Windows Media Player).
My question is how can I distinguish which is the Computer? Of course, there names can't be used because two exactly the same name will always be returned...

Not able to get UniqueId of CPU using WMI classes c++

In an application, it needs to retrieve, a system specific UniqueId. SO that I have tried to retrive the UniqueId of the CPU. and I am using WMI class property to retrieve that.
But on trying to retrieve the 'UniqueId' property of win32_processor class, it returns VT_NULL in the variant in which the output is expected. But in the same time it is providing valid outputs for other properties like, deciveId, processorId etc. but those are not unique and can't fulfil my purpose.
Any one know why this happens? plz help me....
below is the code that i used... please have a look, and say whether there is any problem in that....
what modification can i do to make it work...
if(CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
0
) != S_OK)
return;
IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices = NULL;
IEnumWbemClassObject * pEnumObject = NULL;
BSTR bstrNamespace = (L"root\\cimv2");
if(CoCreateInstance (
CLSID_WbemAdministrativeLocator,
NULL ,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER ,
IID_IUnknown ,
( void ** ) & pIWbemLocator
) != S_OK)
return;
if(pIWbemLocator->ConnectServer(
bstrNamespace, // Namespace
NULL, // Userid
NULL, // PW
NULL, // Locale
0, // flags
NULL, // Authority
NULL, // Context
&pWbemServices
) != S_OK)
return;
HRESULT hRes;
BSTR strQuery = (L"Select * from Win32_Processor");
BSTR strQL = (L"WQL");
hRes = pWbemServices->ExecQuery(strQL, strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject);
if(hRes != S_OK)
{
MessageBox("Could not execute Query");
return;
}
ULONG uCount = 1, uReturned;
IWbemClassObject * pClassObject = NULL;
hRes = pEnumObject->Reset();
if(hRes != S_OK)
{
MessageBox("Could not Enumerate");
return;
}
hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned);
if(hRes != S_OK)
{
MessageBox("Could not Enumerate");
return;
}
VARIANT v;
BSTR strClassProp = SysAllocString(L"UniqueId");
hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0);// here the v is VT_NULL but works if the vlaue of strClassProp is processerId, deviceId
if(hRes != S_OK)
{
MessageBox("Could not Get Value");
return;
}
SysFreeString(strClassProp);
_bstr_t bstrPath = &v; //it causes exception if the v is VT_NULL in other cases its working
char* strPath = new char [1024];
_stprintf(strPath, ("%s"),(char*)bstrPath);
if (SUCCEEDED(hRes))
MessageBox(strPath);
Expects expert advice and help....
Thanks in Advance....

DirectShow BindToObject returning invalid handle

EDIT
Just tested playcap.cpp and I'm also getting the same error so I know it's a not a fault with my code.
--
EDIT 2
Edited my code to preserve the goodMoniker pointer. Same error, however.
+if(pMoniker != goodMoniker)
+{
pMoniker->Release();
+}
--
Having a problem with getting my webcam to work with DirectShow. This line:
hr = goodMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)(&pCap));
returns the following error from MSVC++ 2010 EE:
First-chance exception at 0x777ff9d2 in WebcamControlTest.exe: 0xC0000008: An invalid handle was specified.
Full code here (ripped almost completely from MSDN):
#include <DShow.h>
#include <iostream>
int main(void)
{
IGraphBuilder* pGraph = NULL;
ICaptureGraphBuilder2* pBuild = NULL;
HRESULT hr;
//Initialize pGraph
hr = CoInitialize(NULL);
hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void**) &pBuild);
if(FAILED(hr))
{
printf("ERROR - Could not initialize COM library");
return 1;
}
//Initialize pBuild
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **) &pGraph);
if(FAILED(hr))
{
printf("ERROR - Could not create the Filter Graph Manager.");
return 2;
}
pBuild->SetFiltergraph(pGraph);
//Initialize pCap
ICreateDevEnum* pDevEnum = NULL;
IEnumMoniker* pEnum = NULL;
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, reinterpret_cast<void**>(&pDevEnum));
if(SUCCEEDED(hr))
{
hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnum, 0);
}
IMoniker* goodMoniker = NULL;
HWND hList;
IMoniker *pMoniker = NULL;
while(pEnum->Next(1, &pMoniker, NULL) == S_OK)
{
IPropertyBag* pPropBag;
hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)(&pPropBag));
if(FAILED(hr))
{
pMoniker->Release();
continue;
}
VARIANT varName;
VariantInit(&varName);
hr = pPropBag->Read(L"Description", &varName, 0);
if(FAILED(hr))
{
hr = pPropBag->Read(L"FriendlyName", &varName, 0);
}
if(SUCCEEDED(hr))
{
for(int i=0;i<8;i++)
{
std::cout<<(char)*(varName.bstrVal + i);
}
char yn;
std::cin>>yn;
if(yn=='Y')
{
std::cout<<"SUCCESSFUL"<<std::endl;
goodMoniker = pMoniker;
VariantClear(&varName);
}
}
pPropBag->Release();
if(pMoniker != goodMoniker)
{
pMoniker->Release();
}
}
IBaseFilter* pCap;
hr = goodMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)(&pCap));
if(SUCCEEDED(hr))
{
hr = pGraph->AddFilter(pCap, L"Capture Filter");
}
hr = pBuild->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pCap, NULL, NULL);
while(SUCCEEDED(hr));
pGraph->Release();
pBuild->Release();
pCap->Release();
}
It might be a driver problem as there is one device that works (a virtual driver for screencapping and not actual webcam input) but I've updated, uninstalled and reinstalled to no luck. Any ideas?
After saving a pointer in goodMoniker you release the object a couple of lines below (pMoniker->Release() ). Now goodMoniker points to a released object. You should have increased its reference count.

How to properly use CComPtr in function calls?

I'm new to COM and smartpointers, I'm trying to convert a project from raw pointers to CComPtr to avoid the hassle with memory management. I'm looking for some advice on how to properly use CComPointers when it comes to functions and scope in general. A sample of my code.
int DisplayDeviceInformation(IEnumMoniker * pEnum, IMoniker * pMoniker)
{
CComPtr<IPropertyBag> pPropBag = NULL;
while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
{
HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
if (FAILED(hr))
{
}
VARIANT var;
VariantInit(&var);
// Get description or friendly name.
hr = pPropBag->Read(L"Description", &var, 0);
if (FAILED(hr))
{
hr = pPropBag->Read(L"FriendlyName", &var, 0);
}
if (SUCCEEDED(hr))
{
printf("%S\n", var.bstrVal);
VariantClear(&var);
}
hr = pPropBag->Write(L"FriendlyName", &var);
// WaveInID applies only to audio capture devices.
hr = pPropBag->Read(L"WaveInID", &var, 0);
if (SUCCEEDED(hr))
{
printf("WaveIn ID: %d\n", var.lVal);
VariantClear(&var);
}
hr = pPropBag->Read(L"DevicePath", &var, 0);
if (SUCCEEDED(hr))
{
// The device path is not intended for display.
printf("Device path: %S\n", var.bstrVal);
VariantClear(&var);
}
}
return 0;
}
CComPtr<IMoniker> pMoniker = NULL;
CComPtr<IMoniker> pMoniker2 = NULL;
CComPtr<IEnumMoniker> pEnum = NULL;
hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum);
//pEnum->Next(1, &pMoniker,&cFetched);
if (SUCCEEDED(hr))
{
DisplayDeviceInformation(pEnum, pMoniker);
}
pEnum = NULL;
hr = EnumerateDevices(CLSID_AudioInputDeviceCategory, &pEnum);
//pEnum->Next(1, &pMoniker2,&cFetched);
if (SUCCEEDED(hr))
{
DisplayDeviceInformation(pEnum, pMoniker);
}
Basiclly, the first DisplayDeviceInformation(pEnum, pMoniker); gives a p==0 error. If I however uncomment the pEnum->Next(1, &pMoniker,&cFetched); it works. With raw pointers I don't have to do that since the code just skips to the next device. Any advice or help would make me outmost grateful, thanks in advance!
Where CComPtr gives you an assert failure, you are likely to have an issue with raw pointers as well. You just are not pre-warned and the problem comes up later, e.g. as a reference leak.
You don't seem to need IMoniker in global scope
You need to clear IMoniker pointer before supplying it for the enumerator
See below:
int DisplayDeviceInformation(IEnumMoniker* pEnum, IMoniker** ppSelectedMoniker)
{
CComPtr<IMoniker> pMoniker;
while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
{
CComPtr<IPropertyBag> pPropBag; // You need it clear to start from for every moniker
HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
// ...
if(we should stop enumeration and we are good with current moniker)
{
//ATLASSERT(ppSelectedMoniker != NULL);
*ppSelectedMoniker = pMoniker.Detach();
return ...
}
// ...
pMoniker.Release(); // You have to do this, so that next Next would accept empty CComPtr as an argument
}
return 0;
}
CComPtr<IEnumMoniker> pEnumVideoMoniker;
CComPtr<IMoniker> pSelectedVideoMoniker;
hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnumVideoMoniker);
if (SUCCEEDED(hr))
DisplayDeviceInformation(pEnumVideoMoniker, &pSelectedVideoMoniker);
CComPtr<IEnumMoniker> pEnumAudioMoniker;
CComPtr<IMoniker> pSelectedAudioMoniker;
hr = EnumerateDevices(CLSID_AudioInputDeviceCategory, &pEnumAudioMoniker);
if (SUCCEEDED(hr))
DisplayDeviceInformation(pEnumAudioMoniker, &pSelectedAudioMoniker);