Creating home directories with permissions in ActiveDirectory using COM
Posted & filed under active directory, ADSI, C++, com, GADfly, integration.
Creating users in ActiveDirectory is pretty straightforward. You connect to the domain controller:
ADsOpenObject(m_connectionString,
(LPCWSTR)m_username, (LPCWSTR)m_password,
ADS_SECURE_AUTHENTICATION,
IID_IADsContainer,
(void**)&m_pContainer);
create the user, getting an IDispatch interface back:
m_pContainer->Create(CComBSTR("user"), CComBSTR(wcCN), &pDispatch);
query the IDispatch interface to get the IADsUser interface:
pDispatch->QueryInterface(IID_IADsUser, (void**)&pUser);
and use it to set the various properties of the new user object:
BSTR prop = SysAllocString(L"samAccountName");
var.vt = VT_BSTR;
var.bstrVal = SysAllocString(pUserDetails->GetUsername());
m_hr = pUser->Put(prop, var);
SysFreeString(prop);
VariantClear(&var);
if (!SUCCEEDED(m_hr)) {
pUser->Release();
return false;
} (more...)