How to display all the tasks in Task scheduler - c++

I want to display all the tasks under Task Scheduler using the COM library.
I tried using the programs on learn.microsoft.com which uses the COM objects.
But instead of displaying all the 60 tasks, it displays only 12 tasks
//Get the pointer to the root task folder.
ITaskFolder *pRootFolder = NULL;
hr = pService->GetFolder(_bstr_t(L"\\"), &pRootFolder);
pService->Release();
if (FAILED(hr))
{
printf("Cannot get Root Folder pointer: %x", hr);
CoUninitialize();
return 1;
}
// -------------------------------------------------------
// Get the registered tasks in the folder.
IRegisteredTaskCollection* pTaskCollection = NULL;
hr = pRootFolder->GetTasks(NULL, &pTaskCollection);
pRootFolder->Release();
if (FAILED(hr))
{
printf("Cannot get the registered tasks.: %x", hr);
CoUninitialize();
return 1;
}
LONG numTasks = 0;
hr = pTaskCollection->get_Count(&numTasks);
TASK_STATE taskState;
for (LONG i = 0; i < numTasks; i++)
{
IRegisteredTask* pRegisteredTask = NULL;
hr = pTaskCollection->get_Item(_variant_t(i + 1), &pRegisteredTask);
if (SUCCEEDED(hr))
{
BSTR taskName = NULL;
hr = pRegisteredTask->get_Name(&taskName);
if (SUCCEEDED(hr))
{
printf("\nTask Name: %S", taskName);
SysFreeString(taskName);
hr = pRegisteredTask->get_State(&taskState);
if (SUCCEEDED(hr))
printf("\n\tState: %d", taskState);
I expected the output to display all the tasks, but it displays only a selected number of tasks.
https://learn.microsoft.com/en-us/windows/desktop/taskschd/displaying-task-names-and-state--c---
This is the link I used for the program

Here is a sample console app code that will display all tasks recursively, but as I said in the comment, the output will depend if you run this as admin or not.
// include task scheduler lib from code
#pragma comment(lib, "taskschd.lib")
// some useful macros
#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define __WFILE__ WIDEN(__FILE__)
#define HRCHECK(__expr) {hr=(__expr);if(FAILED(hr)){wprintf(L"FAILURE 0x%08X (%i)\n\tline: %u file: '%s'\n\texpr: '" WIDEN(#__expr) L"'\n",hr, hr, __LINE__,__WFILE__);goto cleanup;}}
#define RELEASE(__p) {if(__p!=nullptr){__p->Release();__p=nullptr;}}
#define STARTUP HRESULT hr=S_OK;
#define CLEANUP {cleanup:return hr;}
// forward declaration
HRESULT DumpFolder(ITaskFolder *fld);
int main()
{
STARTUP;
CoInitialize(NULL);
{
CComPtr<ITaskService> svc;
CComPtr<ITaskFolder> fld;
HRCHECK(svc.CoCreateInstance(CLSID_TaskScheduler));
HRCHECK(svc->Connect(CComVariant(), CComVariant(), CComVariant(), CComVariant()));
HRCHECK(svc->GetFolder(CComBSTR(L"\\"), &fld));
HRCHECK(DumpFolder(fld));
}
CoUninitialize();
CLEANUP;
}
HRESULT DumpFolder(ITaskFolder *fld)
{
STARTUP;
CComPtr<IRegisteredTaskCollection> tasks;
CComPtr<ITaskFolderCollection> children;
LONG count;
HRCHECK(fld->GetTasks(TASK_ENUM_HIDDEN, &tasks));
HRCHECK(tasks->get_Count(&count));
// dump out tasks
for (LONG i = 1; i < (count + 1); i++)
{
CComPtr<IRegisteredTask> task;
CComBSTR name;
HRCHECK(tasks->get_Item(CComVariant(i), &task));
HRCHECK(task->get_Name(&name));
wprintf(L"name:'%s'\n", name.m_str);
}
// dump out sub folder
HRCHECK(fld->GetFolders(0, &children));
HRCHECK(children->get_Count(&count));
for (LONG i = 1; i < (count + 1); i++)
{
CComPtr<ITaskFolder> child;
HRCHECK(children->get_Item(CComVariant(i), &child));
// go recursive
HRCHECK(DumpFolder(child));
}
CLEANUP;
}

Related

Create instance failed. Code 80040154 issue when trying to create a component using IDispatch

I am new to COM, I am trying to create a simple c++ console Calendar app.
Here is my code:
// CalendarProps.cpp
//
// DClient.cpp - Dispatch client implementation
// This client connects to the component "Êàëåíäàðü"
// through the dispinterface.
//
#include <windows.h>
#include <stdio.h>
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
#pragma region Create the component
//
DWORD clsctx;
clsctx = CLSCTX_INPROC_SERVER;
HRESULT hr = OleInitialize(NULL);
if (FAILED(hr))
{
printf("Failed to initialize Ole. Code %X \n", hr);//+
return 1;
}
// Get the CLSID for the application.
wchar_t progid1[] = L"MSCAL.Calendar.7";//ProgId - component Id
CLSID clsid;
hr = ::CLSIDFromProgID(progid1, &clsid);
if (FAILED(hr))
{
printf("Failed to get CLSID.Code %X \n", hr);//+
return 1;
}
// Create the component.
IDispatch* pIDispatch = NULL;
hr = ::CoCreateInstance(clsid,
NULL,
clsctx,
IID_IDispatch,
(void**)&pIDispatch);
if (FAILED(hr))
{
printf("Create instance failed.Code %X \n", hr);//+
OleUninitialize();
return 1;
}
printf("CoCreateInstance succeeded.\n");//+
#pragma endregion
#pragma region Get Day
printf("===============get Day==================\n");
// First we need to get the IDs for the function names.
printf("Get DispID for property \"Day\".\n");//+
DISPID dispid1;
OLECHAR* name1 = (OLECHAR*)L"Day";//*
hr = pIDispatch->GetIDsOfNames(IID_NULL,
&name1,
1,
GetUserDefaultLCID(),
&dispid1);
if (FAILED(hr))
{
printf("Query GetIDsOfNames failed. Code %X \n", hr);//+
pIDispatch->Release();
return 1;
}
// DISPPARAMS
DISPPARAMS param1;
param1.cArgs = 0; // Number of arguments
param1.rgvarg = NULL; // Arguments
param1.cNamedArgs = 0; // Number of named args
param1.rgdispidNamedArgs = NULL; // Named arguments
// "Day"
VARIANTARG varres1;
::VariantInit(&varres1); // Initialize the VARIANT
varres1.vt = VT_I2; // Type of VARIANT data
varres1.iVal = 0; // Data for the VARIANT
printf("Get property \"Day\".\n");//+
hr = pIDispatch->Invoke(dispid1,
IID_NULL,
GetUserDefaultLCID(),
DISPATCH_PROPERTYGET,
&param1,
&varres1,//*
NULL,
NULL);
if (FAILED(hr))
{
printf("Invoke call failed.Code %X \n", hr);//+
pIDispatch->Release();
return 1;
}
// Display the returned Value//*
if (varres1.vt == VT_I2)//*
{
printf("Day from Calendar: %d", varres1.iVal);//+
}
printf("\n=========================================\n\n");
/////////////////////////////////////////////////////////
#pragma endregion
#pragma region Put Year
//HRESULT hr2 = OleInitialize(NULL);
printf("===============put Year==================\n");
// First we need to get the IDs for the function names.
printf("Get DispID for property \"Year\".\n");//+
DISPID dispid2;
OLECHAR* name2 = (OLECHAR*)L"Year";//*
hr = pIDispatch->GetIDsOfNames(IID_NULL,
&name2,
1,
GetUserDefaultLCID(),
&dispid2);
if (FAILED(hr))
{
printf("Query GetIDsOfNames failed.Code %X \n", hr);//+
pIDispatch->Release();
return 1;
}
// Allocate and initialize a VARIANT argument.
VARIANTARG vargs2;//*
::VariantInit(&vargs2); // Initialize the VARIANT.
vargs2.vt = VT_I2; // Type of VARIANT data
vargs2.iVal = 2023; // Data for the VARIANT
DISPID dispid = DISPID_PROPERTYPUT; // !!!!!!
DISPPARAMS param2;
param2.cArgs = 1; // Number of arguments
param2.rgvarg = &vargs2; // Arguments
param2.cNamedArgs = 1; // Number of named args !!!
param2.rgdispidNamedArgs = &dispid; // Named arguments !!!!
printf("Put the property \"Year\".\n");//+
hr = pIDispatch->Invoke(dispid2,
IID_NULL,
GetUserDefaultLCID(),
DISPATCH_PROPERTYPUT,
&param2,
NULL,
NULL,
NULL);
if (FAILED(hr))
{
printf("Invoke call failed.Code %X \n", hr);//+
pIDispatch->Release();
return 1;
}
#pragma endregion
#pragma region Display the new Year value
// Display the new Year value
// DISPPARAMS Year
param2.cArgs = 0; // Number of arguments
param2.rgvarg = 0; // Arguments
param2.cNamedArgs = 0; // Number of named args
param2.rgdispidNamedArgs = 0; // Named arguments
VARIANTARG varres2;//*
::VariantInit(&varres2); // Initialize the VARIANT.//*
varres2.vt = VT_I2; // Type of VARIANT data//*
varres2.iVal = 0; // Data for the VARIANT//*
printf("Get the property \"Year\"\n");//+
hr = pIDispatch->Invoke(dispid2,
IID_NULL,
GetUserDefaultLCID(),
DISPATCH_PROPERTYGET,
&param2,
&varres2,
NULL,
NULL);
if (FAILED(hr))
{
printf("Invoke call failed.Code %X \n", hr);
pIDispatch->Release();
return 1;
}
// Display the returned Year value
if (varres2.vt == VT_I2)
{
printf("Property Year in component Calendar: %d", vargs2.iVal);
}
printf("\n=========================================\n\n");
// Release the dispatch interface.
#pragma endregion
#pragma region Removing a Component
// Release the dispatch interface.
pIDispatch->Release();
// Uninitialize the OLE library.
OleUninitialize();
#pragma endregion
return 0;
}
I have create a reg.file with the stored ProdId and merged it successfully:
wchar_t progid1[] = L"MSCAL.Calendar.7";//ProgId - component Id
the file has:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\MSCAL.Calendar.7]
#="MSCAL.Calendar.7"
[HKEY_CLASSES_ROOT\MSCAL.Calendar.7\CLSID]
#="{here I generated GUID using VS GUID tool}"
When I try to run my app, I am getting any issue:
issue
Please, help me to find an issue!
I am expecting the COM to be created and my app to be running.
The answer from #Igor Tandetnik was right, I do not need to register any other ProdId, coz mine example is already installed on my machine.
There would also need to be a key HKEY_CLASSES_ROOT\CLSID{Your actual
GUID here}, and under that key, the details of where and how the
component is actually implemented. Taking a step back,
MSCAL.Calendar.7 sounds like some component provided by Microsoft -
why are you making up a GUID for it? If it's installed on your
machine, the necessary registry entries should already be present. If
it's not installed on your machine, then making up random registry
keys won't magically conjure it up.

How to check trigger of a task in Task scheduler using c++?

I want to check the triggers for the tasks in the Task scheduler using c++.
I want to use the function HRESULT get_Type(TASK_TRIGGER_TYPE2 *pType);
to check whether the task is logon or boot triggered.
TASK_STATE taskState;
for (LONG i = 0; i < numTasks; i++)
{
IRegisteredTask* pRegisteredTask = NULL;
hr = pTaskCollection->get_Item(_variant_t(i + 1), &pRegisteredTask);
if (SUCCEEDED(hr))
{
BSTR taskName = NULL;
TASK_TRIGGER_TYPE2 *pType = NULL;
hr = pRegisteredTask->get_Name(&taskName);
if (SUCCEEDED(hr))
{
printf("\nTask Name: %S", taskName);
SysFreeString(taskName);
hr = pRegisteredTask->get_Type(*pType); //Implemented here
if (SUCCEEDED(hr))
printf("\n%s",&pType);
else
printf("\n\tCannot get the registered task state: %x", hr);
}
hr = pRegisteredTask->get_State(&taskState);
if (SUCCEEDED(hr))
printf("\n\tState: %d", taskState);
else
printf("\n\tCannot get the registered task state: %x", hr);
}
else
{
printf("\nCannot get the registered task name: %x", hr);
}
pRegisteredTask->Release();
}
else
{
printf("\nCannot get the registered task item at index=%d: %x", i + 1, hr);
}
}
On compilation, it gives me an error saying "IRegisteredTask has no member get_Type()"
Then I altered the code and added
ITrigger *trig = NULL;
trig->get_Type(&pType);
But that doesnt give me any values either
The following is code piece works fore me you can have a try:
for (LONG i = 0; i < numTasks; i++)
{
IRegisteredTask* pRegisteredTask = NULL;
hr = pTaskCollection->get_Item(_variant_t(i + 1), &pRegisteredTask);
if (SUCCEEDED(hr))
{
BSTR taskName = NULL;
hr = pRegisteredTask->get_Name(&taskName);
if (SUCCEEDED(hr))
{
printf("\nTask Name: %S", taskName);
SysFreeString(taskName);
hr = pRegisteredTask->get_State(&taskState);
if (SUCCEEDED(hr))
printf("\n\tState: %d", taskState);
else
printf("\n\tCannot get the registered task state: %x", hr);
}
else
{
printf("\nCannot get the registered task name: %x", hr);
}
ITaskDefinition* taskDef = NULL;
hr = pRegisteredTask->get_Definition(&taskDef);
if (SUCCEEDED(hr))
{
ITriggerCollection* triggers = NULL;
taskDef->get_Triggers(&triggers);
LONG trigCnt = 0;
triggers->get_Count(&trigCnt);
for (LONG i = 0; i < trigCnt; i++)
{
ITrigger* trig = NULL;
TASK_TRIGGER_TYPE2 pType = TASK_TRIGGER_EVENT;
triggers->get_Item(_variant_t(i + 1), &trig);
trig->get_Type(&pType);
DWORD errCode = GetLastError();
if(pType != NULL)
printf("\nTrigger Type : %d", pType);
}
}
else
{
printf("\nCannot get the registered task definition: %x", hr);
}
pRegisteredTask->Release();
}
else
{
printf("\nCannot get the registered task item at index=%d: %x", i + 1, hr);
}
}
Refer to "Displaying Task Names and States (C++)"
Looking at the documentation, it looks like after you have a IRegisteredTask you need to call get_Definition() to get its ITaskDefinition.
Using the ITaskDefinition, you can get the a ITriggerCollection via get_Triggers()
Then, enumerating over the ITriggerCollection, you can QueryInterface() each ITrigger to see if it supports the ILogonTrigger or IBootTrigger interfaces.

Polling for audio sessions on default endpoint sometimes crashes on Win7

I'm working on an application which polls for the states and peak levels of audio sessions on the default audio rendering endpoint every X milliseconds, and implements some logic based on that.
It seems to run fine on Windows 10, but on Windows 7 I get occasional crashes when I change the default playback device (e.g. when I switch between my USB headset and the PC speaker).
The exact line where the crash occurs changes between runs, but it's usually when I access the IAudioSessionControl or IAudioSessionControl2 pointers to make various WASAPI calls.
What I could glean from the stack traces created by ProcDump and analyzed by WinDbg is that the COM objects representing audio sessions are destroyed when the default playback device has changed
(even though I had obtained and am still holding pointers to those objects' interfaces), and then my polling thread crashes randomly in places where I access them through the interface pointers.
I figured that maybe I'm doing something wrong, so this led me to Matthew van Eerde's
blog
and sample, where he does the same querying (and more), but for all available audio endpoints in the system.
So I modified his sample program to do it every 5 milliseconds and only for the default rendering endpoint, and I get the same occasional crashes on Windows 7.
Here is a stripped-down version of the sample, which sometimes results in crashes when switching between playback devices.
I usually get the crash within ~2 minutes of switching back-and-forth between devices. YMMV.
#include <windows.h>
#include <atlbase.h>
#include <stdio.h>
#include <mmdeviceapi.h>
#include <audiopolicy.h>
#include <endpointvolume.h>
#include <functiondiscoverykeys_devpkey.h>
#define LOG(format, ...) wprintf(format L"\n", __VA_ARGS__)
class CoUninitializeOnExit {
public:
CoUninitializeOnExit() {}
~CoUninitializeOnExit() {
CoUninitialize();
}
};
class PropVariantClearOnExit {
public:
PropVariantClearOnExit(PROPVARIANT *p) : m_p(p) {}
~PropVariantClearOnExit() {
PropVariantClear(m_p);
}
private:
PROPVARIANT *m_p;
};
int _cdecl wmain() {
HRESULT hr = S_OK;
hr = CoInitialize(NULL);
if (FAILED(hr)) {
LOG(L"CoInitialize failed: hr = 0x%08x", hr);
return -__LINE__;
}
CoUninitializeOnExit cuoe;
while (1)
{
Sleep(5);
// get default device
CComPtr<IMMDeviceEnumerator> pMMDeviceEnumerator;
hr = pMMDeviceEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator));
if (FAILED(hr)) {
LOG(L"CoCreateInstance(IMMDeviceEnumerator) failed: hr = 0x%08x", hr);
return -__LINE__;
}
CComPtr<IMMDevice> pMMDevice;
hr = pMMDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pMMDevice);
if (FAILED(hr)) {
LOG(L"IMMDeviceEnumerator::GetDefaultAudioEndpoint failed: hr = 0x%08x", hr);
return -__LINE__;
}
// get the name of the endpoint
CComPtr<IPropertyStore> pPropertyStore;
hr = pMMDevice->OpenPropertyStore(STGM_READ, &pPropertyStore);
if (FAILED(hr)) {
LOG(L"IMMDevice::OpenPropertyStore failed: hr = 0x%08x", hr);
return -__LINE__;
}
PROPVARIANT v; PropVariantInit(&v);
PropVariantClearOnExit pvcoe(&v);
hr = pPropertyStore->GetValue(PKEY_Device_FriendlyName, &v);
if (FAILED(hr)) {
LOG(L"IPropertyStore::GetValue(PKEY_Device_FriendlyName) failed: hr = 0x%08x", hr);
return -__LINE__;
}
if (VT_LPWSTR != v.vt) {
LOG(L"PKEY_Device_FriendlyName has unexpected vartype %u", v.vt);
return -__LINE__;
}
LOG(L"Selected playback device: %s\n", v.pwszVal);
// get a session enumerator
CComPtr<IAudioSessionManager2> pAudioSessionManager2;
hr = pMMDevice->Activate(
__uuidof(IAudioSessionManager2),
CLSCTX_ALL,
nullptr,
reinterpret_cast<void **>(&pAudioSessionManager2)
);
if (FAILED(hr)) {
LOG(L"IMMDevice::Activate(IAudioSessionManager2) failed: hr = 0x%08x", hr);
return -__LINE__;
}
CComPtr<IAudioSessionEnumerator> pAudioSessionEnumerator;
hr = pAudioSessionManager2->GetSessionEnumerator(&pAudioSessionEnumerator);
if (FAILED(hr)) {
LOG(L"IAudioSessionManager2::GetSessionEnumerator() failed: hr = 0x%08x", hr);
return -__LINE__;
}
// iterate over all the sessions
int count = 0;
hr = pAudioSessionEnumerator->GetCount(&count);
if (FAILED(hr)) {
LOG(L"IAudioSessionEnumerator::GetCount() failed: hr = 0x%08x", hr);
return -__LINE__;
}
for (int session = 0; session < count; session++) {
// get the session control
CComPtr<IAudioSessionControl> pAudioSessionControl;
hr = pAudioSessionEnumerator->GetSession(session, &pAudioSessionControl);
if (FAILED(hr)) {
LOG(L"IAudioSessionEnumerator::GetSession() failed: hr = 0x%08x", hr);
return -__LINE__;
}
AudioSessionState state;
hr = pAudioSessionControl->GetState(&state);
if (FAILED(hr)) {
LOG(L"IAudioSessionControl::GetState() failed: hr = 0x%08x", hr);
return -__LINE__;
}
CComPtr<IAudioSessionControl2> pAudioSessionControl2;
hr = pAudioSessionControl->QueryInterface(IID_PPV_ARGS(&pAudioSessionControl2));
if (FAILED(hr)) {
LOG(L"IAudioSessionControl::QueryInterface(IAudioSessionControl2) failed: hr = 0x%08x", hr);
return -__LINE__;
}
DWORD pid = 0;
hr = pAudioSessionControl2->GetProcessId(&pid);
if (FAILED(hr)) {
LOG(L"IAudioSessionControl2::GetProcessId() failed: hr = 0x%08x", hr);
return -__LINE__;
}
hr = pAudioSessionControl2->IsSystemSoundsSession();
if (FAILED(hr)) {
LOG(L"IAudioSessionControl2::IsSystemSoundsSession() failed: hr = 0x%08x", hr);
return -__LINE__;
}
bool bIsSystemSoundsSession = (S_OK == hr);
// get the current audio peak meter level for this session
CComPtr<IAudioMeterInformation> pAudioMeterInformation;
hr = pAudioSessionControl->QueryInterface(IID_PPV_ARGS(&pAudioMeterInformation));
if (FAILED(hr)) {
LOG(L"IAudioSessionControl::QueryInterface(IAudioMeterInformation) failed: hr = 0x%08x", hr);
return -__LINE__;
}
float peak = 0.0f;
hr = pAudioMeterInformation->GetPeakValue(&peak);
if (FAILED(hr)) {
LOG(L"IAudioMeterInformation::GetPeakValue() failed: hr = 0x%08x", hr);
return -__LINE__;
}
LOG(
L" Session #%d\n"
L" State: %d\n"
L" Peak value: %g\n"
L" Process ID: %u\n"
L" System sounds session: %s",
session,
state,
peak,
pid,
(bIsSystemSoundsSession ? L"yes" : L"no")
);
} // session
} // while
return 0;
}
Here is one instance of a crash dump analysis: https://pastebin.com/tvRV8ukY.
Is this an issue with the Windows 7 implementation of the Core Audio APIs, or am I doing something wrong?
Thanks.
I eventually contacted Matthew van Eerde by e-mail, and he said it does look like a race condition in Core Audio API on Windows 7, and my best bet is to file a support request to Microsoft.
Thanks for your help, Matthew!

ADSI Filter All computers from an OU using c++

Iam new to c++. Using the help of MSDN article iam trying to fetch all the computers under a OU in domain using ADSI. But Iam getting 8254L (FILTER_UNKNOWN) error. Iam not sure what iam doing wrong here. I tried to update the filter but no change in the error. Please suggest what iam doing wrong here.
Below is the code am using for getting the list.
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <objbase.h>
#include <wchar.h>
#include <activeds.h>
#include <sddl.h>
#include <comutil.h>
#include <string.h>
#include <stdio.h>
HRESULT FindComputers(IDirectorySearch *pContainerToSearch, // IDirectorySearch pointer to the container to search.
LPOLESTR szFilter, // Filter to find specific users.
// NULL returns all user objects.
LPOLESTR *pszPropertiesToReturn, // Properties to return for user objects found.
// NULL returns all set properties.
BOOL bIsVerbose // TRUE indicates that display all properties for the found objects.
// FALSE indicates that only the RDN.
);
// Entry point for the application.
void wmain(int argc, wchar_t *argv[])
{
// Initialize COM.
CoInitialize(NULL);
HRESULT hr = S_OK;
// Get rootDSE and the current user domain container distinguished name.
IADs *pObject = NULL;
IDirectorySearch *pContainerToSearch = NULL;
LPOLESTR szPath = new OLECHAR[MAX_PATH];
BOOL bReturnVerbose = FALSE;
DWORD dwLength = MAX_PATH * 2;
LPOLESTR pszBuffer = new OLECHAR[dwLength];
VARIANT var;
hr = ADsOpenObject(L"LDAP://rootDSE",
NULL,
NULL,
ADS_SECURE_AUTHENTICATION, // Use Secure Authentication.
IID_IADs,
(void**)&pObject);
if (FAILED(hr))
{
wprintf(L"Cannot execute query. Cannot bind to LDAP://rootDSE.\n");
if (pObject)
pObject->Release();
return;
}
if (SUCCEEDED(hr))
{
hr = pObject->Get(_bstr_t("defaultNamingContext"), &var);
if (SUCCEEDED(hr))
{
wprintf(L"bstrVal: %s\n", var.bstrVal);
// Build path to the domain container.
// wcsncpy_s(szPath, L"LDAP://", MAX_PATH);
// wcsncat_s(szPath, var.bstrVal, MAX_PATH - wcslen(szPath));
//hr = ADsOpenObject(szPath,
hr = ADsOpenObject(L"LDAP://OU=IA Computers,OU=Infosys,DC=iaseries,Dc=local",
NULL,
NULL,
ADS_SECURE_AUTHENTICATION, // Use Secure Authentication.
IID_IDirectorySearch,
(void**)&pContainerToSearch);
if (SUCCEEDED(hr))
{
hr = FindComputers(pContainerToSearch, // IDirectorySearch pointer to domainDNS container.
pszBuffer,
NULL, // Return all properties.
bReturnVerbose
);
if (SUCCEEDED(hr))
{
if (S_FALSE == hr)
wprintf(L"Computer object cannot be found.\n");
}
else if (E_ADS_INVALID_FILTER == hr)
wprintf(L"Cannot execute query. Invalid filter was specified.\n");
else
wprintf(L"Query failed to run. HRESULT: %x\n", hr);
}
else
{
wprintf(L"Cannot execute query. Cannot bind to the container.\n");
}
if (pContainerToSearch)
pContainerToSearch->Release();
}
VariantClear(&var);
}
if (pObject)
pObject->Release();
// Uninitialize COM.
CoUninitialize();
delete[] szPath;
delete[] pszBuffer;
getchar();
}
HRESULT FindComputers(IDirectorySearch *pContainerToSearch, // IDirectorySearch pointer to the container to search.
LPOLESTR szFilter, // Filter for finding specific users.
// NULL returns all user objects.
LPOLESTR *pszPropertiesToReturn, // Properties to return for user objects found.
// NULL returns all set properties.
BOOL bIsVerbose // TRUE indicates that all properties for the found objects are displayed.
// FALSE indicates only the RDN.
)
{
if (!pContainerToSearch)
return E_POINTER;
DWORD dwLength = (MAX_PATH * 2)+100;
// Create search filter.
LPOLESTR pszSearchFilter = new OLECHAR[dwLength];
// Add the filter.
//swprintf_s(pszSearchFilter, dwLength, L"((objectClass=Computer)%s)", szFilter);
swprintf_s(pszSearchFilter, dwLength, L"(&(objectClass=*)(objectCategory=Computer)%s)", szFilter);
// Specify subtree search.
ADS_SEARCHPREF_INFO SearchPrefs;
SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;
SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE;
DWORD dwNumPrefs = 1;
// COL for iterations.
LPOLESTR pszColumn = NULL;
ADS_SEARCH_COLUMN col;
HRESULT hr = S_OK;
// Interface Pointers
IADs *pObj = NULL;
IADs * pIADs = NULL;
// Search handle.
ADS_SEARCH_HANDLE hSearch = NULL;
// Set search preference.
hr = pContainerToSearch->SetSearchPreference(&SearchPrefs, dwNumPrefs);
if (FAILED(hr))
return hr;
LPOLESTR pszBool = NULL;
DWORD dwBool = 0;
PSID pObjectSID = NULL;
LPOLESTR szSID = NULL;
LPOLESTR szDSGUID = new WCHAR[39];
LPGUID pObjectGUID = NULL;
SYSTEMTIME systemtime;
DATE date;
VARIANT varDate;
LPOLESTR *pszPropertyList = NULL;
LPOLESTR pszNonVerboseList[] = { L"name", L"distinguishedName" };
LPOLESTR szName = new OLECHAR[MAX_PATH];
LPOLESTR szDN = new OLECHAR[MAX_PATH];
VariantInit(&varDate);
int iCount = 0;
DWORD x = 0L;
if (!bIsVerbose)
{
// Return non-verbose list properties only.
hr = pContainerToSearch->ExecuteSearch(pszSearchFilter,
pszNonVerboseList,
sizeof(pszNonVerboseList) / sizeof(LPOLESTR),
&hSearch
);
}
else
{
if (!pszPropertiesToReturn)
{
// Return all properties.
hr = pContainerToSearch->ExecuteSearch(pszSearchFilter,
NULL,
(DWORD)-1,
&hSearch
);
}
else
{
// Specified subset.
pszPropertyList = pszPropertiesToReturn;
// Return specified properties.
hr = pContainerToSearch->ExecuteSearch(pszSearchFilter,
pszPropertyList,
sizeof(pszPropertyList) / sizeof(LPOLESTR),
&hSearch
);
}
}
if (SUCCEEDED(hr))
{
// Call IDirectorySearch::GetNextRow() to retrieve the next data row.
hr = pContainerToSearch->GetFirstRow(hSearch);
if (SUCCEEDED(hr))
{
while (hr != S_ADS_NOMORE_ROWS)
{
// Keep track of count.
iCount++;
if (bIsVerbose)
wprintf(L"----------------------------------\n");
// Loop through the array of passed column names,
// print the data for each column.
while (pContainerToSearch->GetNextColumnName(hSearch, &pszColumn) != S_ADS_NOMORE_COLUMNS)
{
hr = pContainerToSearch->GetColumn(hSearch, pszColumn, &col);
if (SUCCEEDED(hr))
{
// Print the data for the column and free the column.
if (bIsVerbose)
{
// Get the data for this column.
wprintf(L"%s\n", col.pszAttrName);
switch (col.dwADsType)
{
case ADSTYPE_DN_STRING:
for (x = 0; x< col.dwNumValues; x++)
{
wprintf(L" %s\r\n", col.pADsValues[x].DNString);
}
break;
case ADSTYPE_CASE_EXACT_STRING:
case ADSTYPE_CASE_IGNORE_STRING:
case ADSTYPE_PRINTABLE_STRING:
case ADSTYPE_NUMERIC_STRING:
case ADSTYPE_TYPEDNAME:
case ADSTYPE_FAXNUMBER:
case ADSTYPE_PATH:
for (x = 0; x< col.dwNumValues; x++)
{
wprintf(L" %s\r\n", col.pADsValues[x].CaseIgnoreString);
}
break;
case ADSTYPE_BOOLEAN:
for (x = 0; x< col.dwNumValues; x++)
{
dwBool = col.pADsValues[x].Boolean;
pszBool = dwBool ? L"TRUE" : L"FALSE";
wprintf(L" %s\r\n", pszBool);
}
break;
case ADSTYPE_INTEGER:
for (x = 0; x< col.dwNumValues; x++)
{
wprintf(L" %d\r\n", col.pADsValues[x].Integer);
}
break;
case ADSTYPE_OCTET_STRING:
if (_wcsicmp(col.pszAttrName, L"objectSID") == 0)
{
for (x = 0; x< col.dwNumValues; x++)
{
pObjectSID = (PSID)(col.pADsValues[x].OctetString.lpValue);
// Convert SID to string.
ConvertSidToStringSid(pObjectSID, &szSID);
wprintf(L" %s\r\n", szSID);
LocalFree(szSID);
}
}
else if ((_wcsicmp(col.pszAttrName, L"objectGUID") == 0))
{
for (x = 0; x< col.dwNumValues; x++)
{
// Cast to LPGUID.
pObjectGUID = (LPGUID)(col.pADsValues[x].OctetString.lpValue);
// Convert GUID to string.
::StringFromGUID2(*pObjectGUID, szDSGUID, 39);
// Print the GUID.
wprintf(L" %s\r\n", szDSGUID);
}
}
else
wprintf(L" Value of type Octet String. No Conversion.");
break;
case ADSTYPE_UTC_TIME:
for (x = 0; x< col.dwNumValues; x++)
{
systemtime = col.pADsValues[x].UTCTime;
if (SystemTimeToVariantTime(&systemtime,
&date) != 0)
{
// Pack in variant.vt.
varDate.vt = VT_DATE;
varDate.date = date;
VariantChangeType(&varDate, &varDate, VARIANT_NOVALUEPROP, VT_BSTR);
wprintf(L" %s\r\n", varDate.bstrVal);
VariantClear(&varDate);
}
else
wprintf(L" Could not convert UTC-Time.\n", pszColumn);
}
break;
case ADSTYPE_NT_SECURITY_DESCRIPTOR:
for (x = 0; x< col.dwNumValues; x++)
{
wprintf(L" Security descriptor.\n");
}
break;
default:
wprintf(L"Unknown type %d.\n", col.dwADsType);
}
}
else
{
#ifdef _MBCS
// Verbose handles only the two single-valued attributes: cn and ldapdisplayname,
// so this is a special case.
if (0 == wcscmp(L"name", pszColumn))
{
//wcscpy_s(szName, col.pADsValues->CaseIgnoreString);
szName = col.pADsValues->CaseIgnoreString;
}
if (0 == wcscmp(L"distinguishedName", pszColumn))
{
//wcscpy_s(szDN, col.pADsValues->CaseIgnoreString);
szDN = col.pADsValues->CaseIgnoreString;
}
#endif _MBCS
}
pContainerToSearch->FreeColumn(&col);
}
FreeADsMem(pszColumn);
}
if (!bIsVerbose)
wprintf(L"%s\n DN: %s\n\n", szName, szDN);
// Get the next row.
hr = pContainerToSearch->GetNextRow(hSearch);
}
}
// Close the search handle to cleanup.
pContainerToSearch->CloseSearchHandle(hSearch);
}
if (SUCCEEDED(hr) && 0 == iCount)
hr = S_FALSE;
delete[] szName;
delete[] szDN;
delete[] szDSGUID;
delete[] pszSearchFilter;
return hr;
}
Iam able to figure out the issue. Below is the code for working sample.
I haven't cleaned the code much. Please update if you intend to use.
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <objbase.h>
#include <wchar.h>
#include <activeds.h>
#include <sddl.h>
#include <comutil.h>
#include <string.h>
#include <stdio.h>
HRESULT FindComputers(IDirectorySearch *pContainerToSearch); // IDirectorySearch pointer to the container to search.
// Entry point for the application.
void wmain(int argc, wchar_t *argv[])
{
// Initialize COM.
CoInitialize(NULL);
HRESULT hr = S_OK;
// Get rootDSE and the current user domain container distinguished name.
IADs *pObject = NULL;
IDirectorySearch *pContainerToSearch = NULL;
LPOLESTR szPath = new OLECHAR[MAX_PATH];
BOOL bReturnVerbose = FALSE;
DWORD dwLength = MAX_PATH * 2;
VARIANT var;
hr = ADsOpenObject(L"LDAP://rootDSE",
NULL,
NULL,
ADS_SECURE_AUTHENTICATION, // Use Secure Authentication.
IID_IADs,
(void**)&pObject);
if (FAILED(hr))
{
wprintf(L"Cannot execute query. Cannot bind to LDAP://rootDSE.\n");
if (pObject)
pObject->Release();
return;
}
if (SUCCEEDED(hr))
{
hr = pObject->Get(_bstr_t("defaultNamingContext"), &var);
if (SUCCEEDED(hr))
{
//wprintf(L"bstrVal: %s\n", var.bstrVal);
// Build path to the domain container.
// wcsncpy_s(szPath, L"LDAP://", MAX_PATH);
// wcsncat_s(szPath, var.bstrVal, MAX_PATH - wcslen(szPath));
//hr = ADsOpenObject(szPath,
hr = ADsOpenObject(L"LDAP://OU=IA Computers,OU=MyDept,DC=Test,Dc=com",
NULL,
NULL,
ADS_SECURE_AUTHENTICATION, // Use Secure Authentication.
IID_IDirectorySearch,
(void**)&pContainerToSearch);
if (SUCCEEDED(hr))
{
hr = FindComputers(pContainerToSearch); // IDirectorySearch pointer to domainDNS container.
if (SUCCEEDED(hr))
{
if (S_FALSE == hr)
wprintf(L"Computer object cannot be found.\n");
}
else if (E_ADS_INVALID_FILTER == hr)
wprintf(L"Cannot execute query. Invalid filter was specified.\n");
else
wprintf(L"Query failed to run. HRESULT: %x\n", hr);
}
else
{
wprintf(L"Cannot execute query. Cannot bind to the container.\n");
}
if (pContainerToSearch)
pContainerToSearch->Release();
}
VariantClear(&var);
}
if (pObject)
pObject->Release();
// Uninitialize COM.
CoUninitialize();
delete[] szPath;
getchar();
}
HRESULT FindComputers(IDirectorySearch *pContainerToSearch) // IDirectorySearch pointer to the container to search.
{
if (!pContainerToSearch)
return E_POINTER;
DWORD dwLength = (MAX_PATH * 2);
// Create search filter.
LPOLESTR pszSearchFilter = new OLECHAR[dwLength];
// Add the filter.
pszSearchFilter = L"((objectCategory=computer))";
// Specify subtree search.
ADS_SEARCHPREF_INFO SearchPrefs;
SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;
SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE;
DWORD dwNumPrefs = 1;
// COL for iterations.
LPOLESTR pszColumn = NULL;
ADS_SEARCH_COLUMN col;
HRESULT hr = S_OK;
// Interface Pointers
IADs *pObj = NULL;
IADs * pIADs = NULL;
// Search handle.
ADS_SEARCH_HANDLE hSearch = NULL;
// Set search preference.
hr = pContainerToSearch->SetSearchPreference(&SearchPrefs, dwNumPrefs);
if (FAILED(hr))
return hr;
LPOLESTR pszNonVerboseList[] = { L"name", L"distinguishedName" };
LPOLESTR szName = new OLECHAR[MAX_PATH];
LPOLESTR szDN = new OLECHAR[MAX_PATH];
int iCount = 0;
DWORD x = 0L;
// Return non-verbose list properties only.
hr = pContainerToSearch->ExecuteSearch(pszSearchFilter,
pszNonVerboseList,
sizeof(pszNonVerboseList) / sizeof(LPOLESTR),
&hSearch
);
if (SUCCEEDED(hr))
{
// Call IDirectorySearch::GetNextRow() to retrieve the next data row.
hr = pContainerToSearch->GetFirstRow(hSearch);
if (SUCCEEDED(hr))
{
while (hr != S_ADS_NOMORE_ROWS)
{
// Keep track of count.
iCount++;
// Loop through the array of passed column names,
// print the data for each column.
while (pContainerToSearch->GetNextColumnName(hSearch, &pszColumn) != S_ADS_NOMORE_COLUMNS)
{
hr = pContainerToSearch->GetColumn(hSearch, pszColumn, &col);
if (SUCCEEDED(hr))
{
// Verbose handles only the two single-valued attributes: cn and ldapdisplayname,
// so this is a special case.
if (0 == wcscmp(L"name", pszColumn))
{
//wcscpy_s(szName, col.pADsValues->CaseIgnoreString);
szName = col.pADsValues->CaseIgnoreString;
}
if (0 == wcscmp(L"distinguishedName", pszColumn))
{
//wcscpy_s(szDN, col.pADsValues->CaseIgnoreString);
szDN = col.pADsValues->CaseIgnoreString;
}
pContainerToSearch->FreeColumn(&col);
}
FreeADsMem(pszColumn);
}
wprintf(L"%s\n DN: %s\n\n", szName, szDN);
// Get the next row.
hr = pContainerToSearch->GetNextRow(hSearch);
}
}
// Close the search handle to cleanup.
pContainerToSearch->CloseSearchHandle(hSearch);
}
if (SUCCEEDED(hr) && 0 == iCount)
hr = S_FALSE;
delete[] szName;
delete[] szDN;
delete[] pszSearchFilter;
return hr;
}
Thanks,
Vijay

Need to retrieve all groups a user belongs to... in C++

I need to find all the groups a particular user is a member of. I'm using C++, not Powershell, if this is the wrong forum I apologize.
From what I've found on the web I need to retrieve the memberOf property, but I get an error that the property doesn't exist. Any help would be appreciated. Here's the code:
HRESULT hrObj = E_FAIL;
HRESULT hr = E_FAIL;
ADS_SEARCHPREF_INFO SearchPrefs;
// COL for iterations
ADS_SEARCH_COLUMN col;
// Handle used for searching
ADS_SEARCH_HANDLE hSearch;
// Search entire subtree from root.
SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;
SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE;
// Set the search preference.
DWORD dwNumPrefs = 1;
hr = pSearchBase->SetSearchPreference(&SearchPrefs, dwNumPrefs);
if (FAILED(hr))
{
return hr;
}
// Create search filter.
LPWSTR pszFormat = L"(&(objectCategory=person)(objectClass=user)(sAMAccountName=%s))";
int len = wcslen(pszFormat) + wcslen(szFindUser) + 1;
LPWSTR pszSearchFilter = new WCHAR[len];
if(NULL == pszSearchFilter)
{
return E_OUTOFMEMORY;
}
swprintf_s(pszSearchFilter, len, pszFormat, szFindUser);
// Set attributes to return.
LPWSTR pszAttribute[NUM_ATTRIBUTES] = {L"ADsPath"};
// Execute the search.
hr = pSearchBase->ExecuteSearch(pszSearchFilter,
pszAttribute,
NUM_ATTRIBUTES,
&hSearch);
if (SUCCEEDED(hr))
{
// Call IDirectorySearch::GetNextRow() to retrieve the next row of data.
while(pSearchBase->GetNextRow(hSearch) != S_ADS_NOMORE_ROWS)
{
// Loop through the array of passed column names and
// print the data for each column.
for (DWORD x = 0; x < NUM_ATTRIBUTES; x++)
{
// Get the data for this column.
hr = pSearchBase->GetColumn(hSearch, pszAttribute[x], &col);
if (SUCCEEDED(hr))
{
// Print the data for the column and free the column.
// Be aware that the requested attribute is type CaseIgnoreString.
if (ADSTYPE_CASE_IGNORE_STRING == col.dwADsType)
{
IADs *pADS;
hr = ADsOpenObject( col.pADsValues->CaseIgnoreString,
L"Administrator",
L"passW0rd",
ADS_SECURE_AUTHENTICATION,
IID_IADs,
(void**)&pADS);
VARIANT var;
VariantInit(&var);
if (SUCCEEDED(hr))
{
hr = pADS->GetEx(L"memberOf", &var); <-- FAILS!!!
wprintf(L"Found User.\n",szFindUser);
wprintf(L"%s: %s\r\n",pszAttribute[x],col.pADsValues->CaseIgnoreString);
hrObj = S_OK;
}
}
pSearchBase->FreeColumn( &col );
}
else
{
hr = E_FAIL;
}
}
}
// Close the search handle to cleanup.
pSearchBase->CloseSearchHandle(hSearch);
}
delete pszSearchFilter;
if (FAILED(hrObj))
{
hr = hrObj;
}
Unless you're set on using AD directly, it's probably easier to use the Windows Net* functions for the job:
#include <windows.h>
#include <lm.h>
#include <stdio.h>
int main() {
wchar_t user[256];
DWORD size = sizeof(user)/sizeof(user[0]);
GetUserNameW(user, &size);
printf("User: %S\n", user);
printf("Local groups: \n");
LPBYTE buffer;
DWORD entries, total_entries;
NetUserGetLocalGroups(NULL, user, 0, LG_INCLUDE_INDIRECT, &buffer, MAX_PREFERRED_LENGTH, &entries, &total_entries);
LOCALGROUP_USERS_INFO_0 *groups = (LOCALGROUP_USERS_INFO_0*)buffer;
for (int i=0; i<entries; i++)
printf("\t%S\n", groups[i].lgrui0_name);
NetApiBufferFree(buffer);
printf("Global groups: \n");
NetUserGetGroups(NULL, user, 0, &buffer, MAX_PREFERRED_LENGTH, &entries, &total_entries);
GROUP_USERS_INFO_0 *ggroups = (GROUP_USERS_INFO_0*)buffer;
for (int i=0; i<entries; i++)
printf("\t%S\n", ggroups[i].grui0_name);
NetApiBufferFree(buffer);
return 0;
}
Thanks for the reply, I think I found what I was looking for in MSDN.
HRESULT CheckUserGroups(IADsUser *pUser)
{
IADsMembers *pGroups;
HRESULT hr = S_OK;
hr = pUser->Groups(&pGroups);
pUser->Release();
if (FAILED(hr)) return hr;
IUnknown *pUnk;
hr = pGroups->get__NewEnum(&pUnk);
if (FAILED(hr)) return hr;
pGroups->Release();
IEnumVARIANT *pEnum;
hr = pUnk->QueryInterface(IID_IEnumVARIANT,(void**)&pEnum);
if (FAILED(hr)) return hr;
pUnk->Release();
// Enumerate.
BSTR bstr;
VARIANT var;
IADs *pADs;
ULONG lFetch;
IDispatch *pDisp;
VariantInit(&var);
hr = pEnum->Next(1, &var, &lFetch);
while(hr == S_OK)
{
if (lFetch == 1)
{
pDisp = V_DISPATCH(&var);
pDisp->QueryInterface(IID_IADs, (void**)&pADs);
pADs->get_Name(&bstr);
printf("Group belonged: %S\n",bstr);
SysFreeString(bstr);
pADs->Release();
}
VariantClear(&var);
pDisp=NULL;
hr = pEnum->Next(1, &var, &lFetch);
};
hr = pEnum->Release();
return S_OK;
}