如何为 WinApi ldap_search_s 初始化 PZPWSTR (wchar_t**) 参数?

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

我正在使用 ldap_search_s (ldap_search_sW) 提取 AD 用户组。它在 attrs 参数(PZPWSTRwchar_t**)为 NULL 时起作用,但是当我尝试指定它时我得到 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);

根据docs需要"a null-terminated array of null-terminated strings indicating the attributes to return for each matching entry. Pass NULL to retrieve all available attributes."

我尝试了使用向量、数组等的不同方法,但总是得到同样的错误。

The example in MSDN只显示了ANSI版本,但好像是一个非常相似的指针数组。

您的数组不是以 null 结尾的。 NULL 和空字符串相同。

使用 NULL(或现代 C++ 中的 nullptr)。

wchar_t *attrs[] = {
    const_cast<wchar_t *>(L"memberOf"),
    nullptr
};