Get active directory picture using c++ code

Viewed 55

I am working on a java web application with active directory using JNI interface. i want to get the active directory users picture and found that the picture is stored as a octet string . I used IADsUser to get the value of the picture but i cant find any documentation or code how to use that value. I want to send this data through jni to java where i can render the picture. Code i used to get the picture.

HRESULT GetUserObject()
{
    IADsUser* pUser;
    CoInitialize(NULL);
    CComBSTR  path = L"LDAP://WIN-F94H2MP3UJR.Test.local/CN=SomeOne";
    path += L",CN=Users,DC=Test,DC=local";
    HRESULT hr = ADsOpenObject(path, L"Administrator", L"pass@12",
        ADS_SECURE_AUTHENTICATION, // For secure authentication
        IID_IADsUser,
        (void**)&pUser);
    if (FAILED(hr)) { return NULL; }
    VARIANT var;
    hr = pUser->get_Picture((VARIANT*)&var);
    std::string message = std::system_category().message(hr);
    std::cout << message << std::endl;
    return hr;
}

Microsoft documentation i used to code this Link. In this documentation they specified Get picture method which is what am looking for. Here as the hr prints operation completed successfully . Help me how to work with this value or atleast help me to print the value stored in var in c++. Thank you.

1 Answers

I had an old C++ project where I was already getting data from AD, so I decided to try this out. This is what I came up with to get the data into a byte array and write it to a file:

VariantInit(&var);
LONG lBound = 0;
LONG uBound = 0;

if (FAILED(pUser->get_Picture(&var)) ||
    FAILED(SafeArrayLock(var.parray)) ||
    FAILED(SafeArrayGetLBound(var.parray, 1, &lBound)) ||
    FAILED(SafeArrayGetUBound(var.parray, 1, &uBound))) {
    //handle failure
}

if (var.vt != VT_EMPTY) {
    const char* pData = static_cast<const char*>(var.parray->pvData);

    std::ofstream file("picture.jpg", std::ios::binary);
    file.write(pData, uBound);
    file.close();

    SafeArrayUnlock(var.parray);
}
VariantClear(&var);

The vt property will be VT_EMPTY if there is no picture, and VT_ARRAY | VT_UI1 if it does. The data is a SAFEARRAY. You need to use SafeArrayGetLBound and SafeArrayGetUBound to get the lower bound (probably always 0) and the upper bound (length).

Related