How to initialize a PZPWSTR (wchar_t**) parameter for WinApi ldap_search_s?

-1

I am using ldap_search_s (ldap_search_sW) to extract AD user groups. It works when the attrs parameter (PZPWSTR, or wchar_t**) is NULL, but when I tried to specify it I got Exception at 0x7ffc1885bd95, code: 0xc0000005: read access violation at: 0xfffffffffffffffe.

wchar_t *attrs[] = {
    const_cast<wchar_t *>(L"memberOf"),
    const_cast<wchar_t *>(L"")
};
ret = ldap_search_s(pLdap, const_cast<wchar_t *>(dn.c_str()), LDAP_SCOPE_SUBTREE, const_cast<wchar_t *>(filter.c_str()), attrs, 0, &pSearchResult);

According to the docs it needs to be "a null-terminated array of null-terminated strings indicating the attributes to return for each matching entry. Pass NULL to retrieve all available attributes."

I tried different ways using vectors, arrays, etc. and always get the same error.

The example in MSDN shows only the ANSI version, but it seems to be a very similar array of pointers.

c++
c
winapi
visual-c++
ldap
asked on Stack Overflow Sep 10, 2019 by Alex P. • edited Sep 11, 2019 by Alex P.

1 Answer

2

Your array is not null-terminated. NULL and an empty string are not the same.

Use NULL (or nullptr in modern C++).

wchar_t *attrs[] = {
    const_cast<wchar_t *>(L"memberOf"),
    nullptr
};
answered on Stack Overflow Sep 10, 2019 by Alex P. • edited Sep 11, 2019 by Alex P.

User contributions licensed under CC BY-SA 3.0