Platform SDK: Active Directory, ADSI, and Directory Services

ADsGetLastError

The ADsGetLastError function retrieves the calling thread's last-error code value. This function behaves in the same way as the Win32 function GetLastError.

HRESULT ADsGetLastError(
  LPDWORD lpError, 
  LPWSTR lpErrorBuf, 
  DWORD dwErrorBufLen, 
  LPWSTR lpNameBuf, 
  DWORD dwNameBufLen 
);

Parameters

lpError
[out] Pointer to the location that receives the error code.
lpErrorBuf
[out] Pointer to the location that receives the null-terminated string describing the error.
dwErrorBufLen
[in] Size (in characters) of the lpErrorBuf buffer. If the buffer is too small to receive the error string, the string is truncated, but still null-terminated. A buffer of at least 256 bytes is recommended.
lpNameBuf
[out] Pointer to the location that receives the null-terminated string describing the name of the provider that raised the error.
dwNameBufLen
[in] Size (in characters) of the lpNameBuf buffer. If the buffer is too small to receive the error string, the string is truncated, but still null-terminated.

Return Values

This method supports the standard return values, as well as the following:

S_OK
The last error was retrieved successfully.
E_POINTER
One or more of the parameters passed in is bad.
ERROR_BAD_DEVICE
The thread identifier for the current thread could not be found in that table anywhere.
For other return values, see ADSI Error Codes.

Remarks

ADSI errors fall into two types according to the values of their facility code. The standard ADSI error codes have a facility code value of 0x5 and the extended ADSI error codes assume that of FACILITY_WIN32. The error values of the standard and extended ADSI error codes are of the forms of 0x80005xxx and 0x8007xxxx, respectively. Use the HRESULT_FACILITY(hr) macro to determine the type of ADSI errors you are dealing with.

The following code snippet shows how to get Win32 error codes and their descriptions using ADsGetLastError:

if (FAILED(hr))
{
    wprintf(L"An error occurred.\n  HRESULT: %x\n",hr);
    //If facility is Win32, get the Win32 error 
    if (HRESULT_FACILITY(hr)==FACILITY_WIN32)
    {
        DWORD dwLastError;
        WCHAR szErrorBuf[MAX_PATH];
        WCHAR szNameBuf[MAX_PATH];
        //Get extended error value.
        HRESULT hr_return =S_OK;
        hr_return = ADsGetLastError( &dwLastError,
                                       szErrorBuf,
                                       MAX_PATH-1,
                                        szNameBuf,
                                       MAX_PATH-1);
        if (SUCCEEDED(hr_return))
        {
             wprintf(L"Error Code: %d\n Error Text: %ws\n Provider: %ws\n", dwLastError, szErrorBuf, szNameBuf);
        }
    }
}
 

If hr is 80071392, the sample code produces the following output.

Error creating container object.
    HRESULT: 80071392
    Error Code: 8305
    Error Text: 00002071: UpdErr: DSID-030502F1, problem 6005 (ENTRY_EXISTS), data 0
    Provider: LDAP Provider
 

Requirements

  Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0 with DSClient).
  Windows 95/98: Requires Windows 95 or later (with DSClient).
  Header: Declared in Adshlp.h.
  Library: Included as a resource in ActiveDs.dll.

See Also

ADSI Error Codes, ADSI Functions, ADsSetLastError, GetLastError