直接上代码,获取sid,并且转换成字符串形式。没有仔细优化代码,这只能是一段demo代码,但是能跑成功。跟大家share一下。
// LDAPTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include "Winldap.h"
#include <Dsgetdc.h>
#include <Sddl.h>
#include <string>
#include <algorithm>
//#include <locale>
#define MAX_NAME 1024
std::string ConvertToStringSid(const unsigned char* bsid, const int len);
namespace myldap
{
std::wstring LogInWithLdap(const std::wstring& strLdapServer, const std::wstring& strDomain, const std::wstring& strUserName, const std::wstring& strPwd)
{
const WCHAR* kSAMAccountName = L"sAMAccountName";
const WCHAR* kObjectSid = L"objectSid";
ULONG rt;
LDAP* ld = ldap_initW((PWSTR)strLdapServer.c_str(), 0);
rt = ldap_set_option(ld, LDAP_OPT_PROTOCOL_