Directory Services

Invoking Creation Wizards from Your Application

An application or component can use the same Microsoft® Active Directory® directory service object creation wizards used by the Active Directory administrative MMC snap-ins. This is accomplished with the IDsAdminCreateObj interface.

Using the IDsAdminCreateObj Interface

An application or component (client) creates an instance of the IDsAdminCreateObj interface by calling CoCreateInstance with the CLSID_DsAdminCreateObj class identifier. COM must be initialized by calling CoInitialize before CoCreateInstance is called.

The client then calls IDsAdminCreateObj::Initialize to initialize the IDsAdminCreateObj object. IDsAdminCreateObj::Initialize accepts an IADsContainer interface pointer that represents the container that the object should be created in, and the class name of the object to be created. When creating user objects, it is also possible to specify an existing object that will be copied to the new object.

When the IDsAdminCreateObj object has been initialized, the client calls IDsAdminCreateObj::CreateModal to display the object creation wizard.

The following code example demonstrates how the IDsAdminCreateObj interface can be created and used to start the object creation wizard for a user object.

#include <atlbase.h>
#include <atlstr.h>

/**************************************************************************

	GetUserContainer()

**************************************************************************/

IADsContainer* GetUserContainer(void)
{
	IADsContainer *pUsers = NULL;

	HRESULT hr;
	IADs	*pRoot;

	// Bind to the rootDSE.
	hr = ADsGetObject(L"LDAP://rootDSE", IID_IADs, (LPVOID*)&pRoot);

	if(SUCCEEDED(hr))
	{
		VARIANT var;
		VariantInit(&var);
		CComBSTR sbstr(L"defaultNamingContext");

		// Get the default naming context (domain) DN.
		hr = pRoot->Get(sbstr, &var);
		if(SUCCEEDED(hr) && (VT_BSTR == var.vt))
		{
			CStringW sstr(L"LDAP://CN=Users,");
			sstr += var.bstrVal;

			// Bind to the User's container.
			hr = ADsGetObject(sstr, IID_IADsContainer, (LPVOID*)&pUsers);

			VariantClear(&var);
	}

	return pUsers;
}

/**************************************************************************

	LaunchNewUserWizard()

**************************************************************************/

HRESULT LaunchNewUserWizard(IADs** ppadsOut)
{
	if(NULL == ppadsOut)
	{
		return E_INVALIDARG;
}

	HRESULT hr;
	IDsAdminCreateObj* pCreateObj = NULL;

	hr = ::CoCreateInstance(	CLSID_DsAdminCreateObj,
								NULL, 
								CLSCTX_INPROC_SERVER,
								IID_IDsAdminCreateObj,
								(void**)&pCreateObj);

	if(SUCCEEDED(hr))
	{
		IADsContainer   *pContainer;

		pContainer = GetUserContainer();

		if(pContainer)
		{
			hr = pCreateObj->Initialize(pContainer, NULL, L"user");
			if(SUCCEEDED(hr))
			{
				HWND	hwndParent;

				hwndParent = GetDesktopWindow();

				hr = pCreateObj->CreateModal(hwndParent, ppadsOut);
		}

			pContainer->Release();
	}
	
		pCreateObj->Release();
}

	return hr; 
}

/**************************************************************************

	main()

**************************************************************************/

int main(void)
{
	HRESULT hr;
	IADs	*pads = NULL;

	CoInitialize(NULL);

	hr = LaunchNewUserWizard(&pads);
	if((S_OK == hr) && (NULL != pads))
	{
		pads->Release();
}

	CoUninitialize();

	return 0;
}