| Platform SDK: Active Directory, ADSI, and Directory Services |
Using the IDsAdminCreateObj COM object, an application can use the creation wizard for a specified class to create a new object of that class. For example, an application can use IDsAdminCreateObj to invoke the creation wizard for creating a new user.
The following function starts the user creation wizard so that the user can create a new user in the container (specified by an IADsContainer pointer). The function returns an IADs pointer to the new user object.
// To compile this code, you must create static variables for the
// GUIDs defined in dsadmin.h. To do this, include initguid.h before
// including dsadmin.h.
#include <initguid.h>
#include <dsadmin.h>
HRESULT StartCreateUserWizard(HWND hWnd, //Handle to window that should own the wizard.
IADsContainer *pContainer, //Container in which to create the new object
IADs **ppNewObject //Return a pointer the new object
)
{
if ((!pContainer)||(!ppNewObject))
return E_POINTER;
CHAR *szText = new CHAR[MAX_PATH*4];
VARIANT varname;
BSTR bstr;
HRESULT hr = E_FAIL;
//For creating a new user, set class to user.
LPOLESTR szClass = L"user";
//Create the creation handler for creation wizards
IDsAdminCreateObj* pCreateObj = NULL;
hr = ::CoCreateInstance(CLSID_DsAdminCreateObj,
NULL, CLSCTX_INPROC_SERVER,
IID_IDsAdminCreateObj,
(void**)&pCreateObj);
if (SUCCEEDED(hr))
{
// Initialize handler
hr = pCreateObj->Initialize(pContainer, NULL, szClass);
if (SUCCEEDED(hr))
{
//Invoke the creation wizard.
hr = pCreateObj->CreateModal(hWnd, ppNewObject);
//S_FALSE means user clicked Cancel
if (hr == S_FALSE)
{
MessageBox(hWnd,"User cancelled the wizard\nThe new object was not created.","Start Create User Wizard",MB_OK);
//Set *ppNewObject to NULL
*ppNewObject = NULL;
hr = E_FAIL;
}
//S_OK means user clicked OK
else if (hr==S_OK)
{
//Display the new user object's name and ADsPath
hr = (*ppNewObject)->Get(L"name",&varname);
hr = (*ppNewObject)->get_ADsPath(&bstr);
if (SUCCEEDED(hr))
{
wsprintf(szText,"The new user %ws was successfully created. \nNew object ADsPath: %ws",varname.bstrVal,bstr);
MessageBox(NULL,szText,"Start Create User Wizard",MB_OK);
VariantClear(&varname);
SysFreeString(bstr);
}
else
{
wsprintf(szText,"The new object was successfully created. But properties could not be read from the new object.");
MessageBox(hWnd,szText,"Start Create User Wizard",MB_OK);
//Return S_FALSE to tell caller that the property read failed.
hr = S_FALSE;
}
}
else
{
wsprintf(szText,"Creation Wizard could not be started. hr: %x",hr);
MessageBox(hWnd,szText,"Start Create User Wizard",MB_OK);
}
}
}
if (pCreateObj)
pCreateObj->Release();
return hr;
}