简体   繁体   English

使用 c++ 代码获取活动目录图片

[英]Get active directory picture using c++ code

I am working on a java web application with active directory using JNI interface.我正在使用 JNI 接口使用带有活动目录的 java web 应用程序。 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.我使用 IADsUser 来获取图片的值,但我找不到任何文档或代码如何使用该值。 I want to send this data through jni to java where i can render the picture.我想通过 jni 将这些数据发送到 java 在那里我可以渲染图片。 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 .我用来编写此链接的 Microsoft 文档。 In this documentation they specified Get picture method which is what am looking for.在本文档中,他们指定了正在寻找的获取图片方法。 Here as the hr prints operation completed successfully.此处为 hr 打印操作成功完成。 Help me how to work with this value or atleast help me to print the value stored in var in c++.帮助我如何使用这个值,或者至少帮助我打印存储在 c++ 中的 var 中的值。 Thank you.谢谢你。

I had an old C++ project where I was already getting data from AD, so I decided to try this out.我有一个旧的 C++ 项目,我已经从 AD 获取数据,所以我决定尝试一下。 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如果没有图片, vt属性将为VT_EMPTY ,而VT_ARRAY | VT_UI1 VT_ARRAY | VT_UI1 if it does. VT_ARRAY | VT_UI1如果有的话。 The data is a SAFEARRAY .数据是SAFEARRAY You need to use SafeArrayGetLBound and SafeArrayGetUBound to get the lower bound (probably always 0 ) and the upper bound (length).您需要使用SafeArrayGetLBoundSafeArrayGetUBound来获得下限(可能总是0 )和上限(长度)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM