//=====================================================================
//== Delete Windows User using ADSI C++ sample - Copyright © 2007, Dave Moats
//==
//== 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.
//==
//== Permission is granted to anyone to use this sample code for any purpose,
//== including commercial applications, subject to the following restrictions:
//==
//== The origin of this code must not be misrepresented;
//==       you must not claim that you wrote the original code.
//==       If you use this code, an acknowledgment in the
//==       documentation is requested - shown below:
//==
//== Portions Copyright © 2007, Dave Moats (http://www.davemoats.com/).
//==
//=====================================================================
//===================================================================
//==
//==    ADSI delete user sample - will work on an NT4 domain or for
//==                                           local windows accounts
//==
//==    NOTE: Do NOT use this sample on Active Directory, this sample
//==             uses the ADSI WinNT provider and for Active Diretory, you
//==             should use the ADSI LDAP provider
//==
//===================================================================
#ifndef UNICODE
#define UNICODE
#endif

#ifndef _UNICODE
#define _UNICODE
#endif

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

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

//===================================================================
// standard headers
//===================================================================
#include <stdio.h>

int wmain ( int argc, wchar_t *argv[] )
{
   int retVal = 0;

   //================================================================
   //    initialize the COM subsystem
   //================================================================
   CoInitialize ( NULL);
  
   HRESULT hr = E_FAIL;

   //================================================================
   //    need to specify what class type will be deleted
   //================================================================
   BSTR userClass = SysAllocString ( L"user" );

   //================================================================
   //    make sure the allocation worked as expected
   //================================================================
   if ( userClass != NULL )
   {
         //============================================================
         //    setting up the pointer needed to delete the user
         //============================================================
         IADsContainer* domainContainer = NULL;

         //============================================================
         //    try to bind to domain or server and get a container
         //    pointer to it
         //============================================================
         hr = ADsGetObject ( L"WinNT://someDomain or Server",
                                       IID_IADsContainer,
                                       ( void**)&domainContainer );


         //============================================================
         //    using the SUCCEEDED macro to check the return value
         //============================================================
         if ( SUCCEEDED ( hr ))
         {
               //=========================================================
               //    since we bound to the domain/server, try to
               //    delete the user object in question
               //=========================================================
               hr = domainContainer->Delete ( userClass, L"user's samAccountName" );
         }

      //==============================================================
      //    release the reference to the domain
      //==============================================================
      domainContainer->Release ( );
   }

   //================================================================
   //    free the string we created earlier
   //================================================================
   if ( userClass != NULL )
         SysFreeString ( userClass );

   //================================================================
   //    see if the user was deleted
   //================================================================
   if ( SUCCEEDED ( hr ))
         printf ( "Deleted User\n" );
   else
         printf ( "Did NOT Delete User\n" );

   //================================================================
   //    uninitialize the COM subsystem
   //================================================================
   CoUninitialize ( );

   return retVal;
}


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.