hariharan baskaran
hariharan baskaran

Reputation: 354

what is V_VT and what does it return?

Am working in Active Directory and i get to know that V_VT is used to get the type of the variant but when i used it and print it it shows 3 and what exactly does that mean? where can i fnd the documentation about it?

VARIANT var;
    VariantInit(&var);
    hr = pUsr->Get(CComBSTR("userAccountControl"), &var);
 if (SUCCEEDED(hr)) {
        std::cout << V_VT(&var) << std::endl;
        VariantClear(&var);
}

Upvotes: 0

Views: 484

Answers (1)

Pepijn Kramer
Pepijn Kramer

Reputation: 13076

I think you should not be using VT_V at all (macros are not typesafe) V_VT(x) provides (as documentation states) a "convenient shorthand" to access VARIANT fields. E.g.V_VT(&vtXmlSource) = VT_UNKNOWN; is equivalent to vtXmlSource.vt = VT_UNKNOWN;

BSTR's are wide character strings (with different allocator/deallocator), https://learn.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr. But you can use them to construct std::wstring from them.

Since you use ATL::CComBSTR, also consider using ATL::CComVariant to avoid memory leaks (manually calling ::VariantClear).

#include <cassert>
#include <atlbase.h>
#include <iostream>
#include <string>

int main()
{
    ATL::CComVariant var{ L"hello world!" };
    assert(var.vt == VT_BSTR);

    ATL::CComBSTR bstr{ var.bstrVal };
    std::wstring str{ var.bstrVal };

    std::wcout << bstr.m_str << "\n";
    std::wcout << str << "\n";

    return 0;
}

Upvotes: 1

Related Questions