How to Add a Member Through ADSI Using Visual C++
ID: Q243725
|
The information in this article applies to:
-
Microsoft Site Server version 3.0
-
Microsoft Site Server version 3.0, Commerce Edition
SUMMARY
You can add a member through ADSI using Visual C++ by following these steps.
- Create a new project, an empty Win32 Console Application.
- Create a new .cpp file and paste in the following code:
#include <comdef.h>
#include <atlbase.h>
#include <activeds.h>
#include <stdio.h>
#include <conio.h>
HRESULT AddMember (OLECHAR * strServerName, OLECHAR * strServerPort, OLECHAR * strOrganization,
OLECHAR * strMemberName, OLECHAR * strPassword)
{
HRESULT hr = NULL;
IADsContainer * pIADsContainer;
IADs * pIADsNewMember;
GUID guid;
wchar_t szGUID[39];
int cGUIDStrLen;
CComBSTR strPathName;
CComBSTR strMemberObjectName;
CComBSTR strAdminDN;
// Create our connection string
strPathName = OLESTR ("LDAP://");
strPathName.Append (strServerName);
strPathName.Append (OLESTR (":"));
strPathName.Append (strServerPort);
strPathName.Append (OLESTR ("/o="));
strPathName.Append (strOrganization);
strPathName.Append (OLESTR ("/ou=members"));
// Create our Admin DN based on the organiation name
strAdminDN = OLESTR ("cn=Administrator, ou=members, o=");
strAdminDN.Append (strOrganization);
// Connect to the LDAP server using an authenticated bind
printf ("Binding to LDAP.\n");
hr = ADsOpenObject (strPathName, strAdminDN, L"password", 0, IID_IADsContainer, (void**)&pIADsContainer);
if (FAILED (hr)) return (hr);
// Generate a GUID and convert it to a text string
printf ("Generating a GUID.\n");
hr = CoCreateGuid (&guid);
if (FAILED (hr)) return (hr);
cGUIDStrLen = ::StringFromGUID2 (guid, szGUID, 39);
if (cGUIDStrLen == 0) return (E_FAIL);
// Prepend the member name with "cn="
strMemberObjectName = OLESTR ("cn=");
strMemberObjectName.Append (strMemberName);
// Create the new member
printf ("Creating the member.\n");
hr = pIADsContainer->Create (L"member", strMemberObjectName, (IDispatch**)&pIADsNewMember);
if (FAILED (hr)) return (hr);
// Set our required attributes
printf ("Saving our attributes.\n");
hr = pIADsNewMember->Put (L"GUID", (_variant_t)szGUID);
hr = pIADsNewMember->Put (L"userPassword", (_variant_t)strPassword);
// Complete the transaction
printf ("Calling SetInfo().\n");
hr = pIADsNewMember->SetInfo ();
if (FAILED (hr)) return (hr);
// Clean up and return
pIADsNewMember->Release ();
pIADsContainer->Release ();
return S_OK;
}
void main (void)
{
OLECHAR strServerName [100];
OLECHAR strServerPort [100];
OLECHAR strOrganization [100];
OLECHAR strMemberName [100];
OLECHAR strPassword [100];
char buffer [100];
CoInitialize (NULL);
HRESULT hr = NULL;
printf ("Server Name:\t");
gets (buffer);
MultiByteToWideChar (CP_ACP, 0, buffer, -1, strServerName, 100);
printf ("LDAP Port:\t");
gets (buffer);
MultiByteToWideChar (CP_ACP, 0, buffer, -1, strServerPort, 100);
printf ("Organization:\t");
gets (buffer);
MultiByteToWideChar (CP_ACP, 0, buffer, -1, strOrganization, 100);
printf ("Member Name:\t");
gets (buffer);
MultiByteToWideChar (CP_ACP, 0, buffer, -1, strMemberName, 100);
printf ("Password:\t");
gets (buffer);
MultiByteToWideChar (CP_ACP, 0, buffer, -1, strPassword, 100);
printf ("\n\nCalling AddMember () function...\n");
hr = AddMember (strServerName, strServerPort, strOrganization, strMemberName, strPassword);
if (FAILED (hr)) printf ("Failed to create member. Error = %8X\n", hr);
else printf ("New member created successfully.\n");
CoUninitialize ();
}
- Select the Project Settings.
- From the Link tab, add the Adsiid.lib and Activeds.lib items to the Object/library modules
- Compile the code
Additional query words:
Keywords : prodMBS
Version : winnt:3.0
Platform : winnt
Issue type : kbhowto
|