gpt4 book ai didi

c++ - 如何获取用于 Active Directory API 的 IADsUser 对象?

转载 作者:行者123 更新时间:2023-11-30 03:19:49 24 4
gpt4 key购买 nike

如何设置传入参数pUser传入这个函数CheckUserGroups(IADsUser *pUser)?我在另一个帖子上看到了功能,但他们没有详细解释:Need to retrieve all groups a user belongs to... in C++

请指教。

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;
}

最佳答案

这是创建和设置 IADsUser *pUser(将“Administrator”更改为您的用户名)的示例:

    HRESULT hr = S_OK;
IADsUser *pUser;
_bstr_t bstr;
DWORD ll_len = 255;
char lbBuffer[255];
::GetComputerName(lbBuffer, &ll_len);

bstr = "WinNT://" + _bstr_t(lbBuffer) + "/" + _bstr_t("Administrator") + ",user";

hr = CoInitialize(NULL);
hr = ADsGetObject(bstr, IID_IADsUser, (void **)&pUser);

if (SUCCEEDED(hr))
{
hr = CheckUserGroups(pUser);
/*
hr = pUser->SetPassword(_bstr_t("123456"));

if (SUCCEEDED(hr))
{
CoUninitialize();
return TRUE;
}
*/
}
CoUninitialize();

对于LDAP,您可能会从this document 中的示例获得帮助,更改ADsGetObject 的第一个参数。

关于c++ - 如何获取用于 Active Directory API 的 IADsUser 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53382468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com