//-------------------------------------------------------------------
//-- Simple ADSI C++ sample - davemoats.com
//--
//-- This sample is provided 'as-is', without any express or implied warranty.
//-- In no event will the authors be held liable for any damages arising from
//-- the use of this sample code.
//--
//-------------------------------------------------------------------
//---------------------------------------------------------
// quick ADSI sample - binding to a user and setting the
//                                   first and last names
//---------------------------------------------------------

//---------------------------------------------------------
// should use unicode - saves a lot of conversion work
//---------------------------------------------------------
#define _UNICODE

//---------------------------------------------------------
// libraries needed to use ADSI
//---------------------------------------------------------
#pragma comment ( lib, "Activeds.lib" )
#pragma comment ( lib, "Adsiid.lib" )

//---------------------------------------------------------
// ADSI header
//---------------------------------------------------------
#include <activeds.h>

int wmain( int argc, wchar_t *argv[] )
{
    //-----------------------------------------------------
    // HRESULT hr is the return code value from all ADSI
    // calls - using the SUCCEEDED MACRO to check for
    // success
    //-----------------------------------------------------
    HRESULT hr;

    //-----------------------------------------------------
    // pointer to our IADsUser object
    //-----------------------------------------------------
    IADsUser *pUser = NULL;

    //-----------------------------------------------------
    // path to the user we are going to try to update
    // make sure you replace this with something
    // specific to your environment
    //-----------------------------------------------------
    LPWSTR pszADsPath = L "LDAP://CN=someUser,CN=Users,DC=someDomain,DC=com";

    //-----------------------------------------------------
    // intialize the COM subsystem before doing any work
    //-----------------------------------------------------
    CoInitialize(NULL);

    //-----------------------------------------------------
    // try to get the user
    //-----------------------------------------------------
    hr = ADsGetObject(pszADsPath, IID_IADsUser,( void**)&pUser);

    //-----------------------------------------------------
    // did not put any other checks in to keep sample
    // short - error checking should be added
    //-----------------------------------------------------
    if(SUCCEEDED(hr))
    {
        //-------------------------------------------------
        // set the first name in the cache
        //-------------------------------------------------
        BSTR bszVal = SysAllocString( L "John" );
        hr = pUser->put_FirstName( bszVal );
        SysFreeString( bszVal );

        //-------------------------------------------------
        // set the last name in the cache
        //-------------------------------------------------
        bszVal = SysAllocString(L "Doe" );
        hr = pUser->put_LastName( bszVal );
        SysFreeString( bszVal );

        //-------------------------------------------------
        // write the change to the directory
        //-------------------------------------------------
        hr = pUser->SetInfo();

        //-------------------------------------------------
        // release the reference to the user object
        //-------------------------------------------------
        pUser->Release();
    }

    //-----------------------------------------------------
    // kill the COM subsystem we were using
    //-----------------------------------------------------
    CoUninitialize();

    return 0;
}


Copyright © 2005 - 2013 Dave Moats.  All rights reserved.  Links: Copyright © by their respective owners.
NO WARRANTIES EXTENDED.  Void where prohibited by law.  Please report any issues or broken links.
You may link to this site freely from your own site.  You may quote from this site, but please include a link to the original source on the originating site.