| Platform SDK: Active Directory, ADSI, and Directory Services |
The objectGUID property of each Active Directory object is stored in the directory as an octet string (an array of one-byte characters). Use the IADs::get_GUID method to retrieve the bindable string form of a directory object's objectGUID.
The following code fragments show a function that reads the objectGUID attribute and returns a string representation of the GUID that can be used to bind to the object.
Dim sADsPathObject As String
Dim sObjectGUID As String
Dim sBindByGuidStr As String
Dim IADsObject As IADs
On Error Resume Next
' Ask the user for an ADsPath to start
sADsPathObject = InputBox("This code binds to a directory object by ADsPath, retrieves the GUID, then rebinds by GUID." & vbCrLf & vbCrLf & "Specify the ADsPath of the object to bind to :")
If sADsPathObject = "" Then
Exit Sub
End If
MsgBox "Binding to " & sADsPathObject
' Bind to initial object
Set IADsObject = GetObject(sADsPathObject)
If (Err.Number <> 0) Then
MsgBox Err.Number, "on GetObject method"
Exit Sub
End If
' Save the GUID of the object
sObjectGUID = IADsObject.Guid
MsgBox "The GUID for " & vbCrLf & vbCrLf & sADsPathObject & vbCrLf & vbCrLf & " is " & vbCrLf & vbCrLf & sObjectGUID
' Release the initial object
Set IADsObject = Nothing
' Build a string for Binding to the object by GUID
sBindByGuidStr = "LDAP://<GUID=" & sObjectGUID & ">"
' Bind BACK to the Same object using the GUID
Set IADsObject = GetObject("LDAP://<GUID=" & sObjectGUID & ">")
If (Err.Number <> 0) Then
MsgBox Err.Number, "on GetObject method"
Exit Sub
End If
MsgBox "Successfully RE bound to " & sADsPathObject & vbCrLf & vbCrLf & " using the path:" & vbCrLf & vbCrLf & sBindByGuidStr
' Release bind by GUID Object
Set IADsObject = Nothing
#define INC_OLE2
#define UNICODE 1
#define _WIN32_DCOM
#include <windows.h>
#include <winuser.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <winldap.h>
#include <activeds.h>
#include "ADSIhelpers.h"
#include <assert.h>
void main()
{
// Initialize COM
CoInitialize(0);
WCHAR pwszADsPathObject[1024];
WCHAR pwszObjectGUID[1024];
WCHAR pwszBindByGuidStr[1024];
HRESULT hr;
IADs * pIADsObject = NULL;
IADs * pIADsObjectByGuid = NULL;
// Ask the user for a ADsPath to start
_putws(L"This code binds to a directory object by ADsPath, retrieves the GUID,\n"
L" then rebinds by GUID. \n\nSpecify the ADsPath of the object to bind to :\n");
_getws(pwszADsPathObject);
if (pwszADsPathObject[0] == NULL)
return;
wprintf(L"\nBinding to %s\n",pwszADsPathObject);
// Bind to initial object
hr = ADsGetObject( pwszADsPathObject,IID_IADs, (void **)& pIADsObject);
if (SUCCEEDED(hr))
{
BSTR bsGuid = NULL;
hr = pIADsObject->get_GUID(&bsGuid);
if (SUCCEEDED(hr))
{
wprintf(L"\n The GUID for\n\n%s\n\nis\n\n%s\n",pwszADsPathObject,bsGuid);
// Build a string for Binding to the object by GUID
wsprintf(pwszBindByGuidStr,L"LDAP://<GUID=%s>",bsGuid);
// Bind BACK to the Same object using the GUID
hr = ADsGetObject( pwszBindByGuidStr,IID_IADs, (void **)& pIADsObjectByGuid);
if (SUCCEEDED(hr))
{
wprintf(L"\nSuccessfully RE bound to\n\n%s\n\nUsing the path:\n\n%s\n", pwszADsPathObject,pwszBindByGuidStr);
// Release bind by GUID Object
pIADsObjectByGuid->Release();
pIADsObjectByGuid = NULL;
}
SysFreeString(bsGuid);
}
pIADsObject->Release();
pIADsObject = NULL;
}
if (FAILED(hr))
_putws(L"Failed");
}