Re: WMI question

Kim Gräsman <[email protected]>
Newsgroups gmane.comp.windows.off-topic
Message-ID <[email protected]>
Hi John,

On Sat, Dec 4, 2010 at 02:21, John Smith <[email protected]> wrote:
>
> I just found out that google will not let me attach a lib file that is
> needed for the project. the file is WbemUuid.lib, if your SDK's are up to
> date, you should have it already.
>

            VARIANT vtProp;

            // Get the value of the Name property
            hres = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
            wcout<< "Data Name : " << vtProp.bstrVal<< endl;   //changing
vtProp.bstrVal to one of the other 'dot' member
            VariantClear(&vtProp);   //variables seems to keep it from
crashing, but the data
        }   //that is reported is garbage...

I think the BSTR you get back may not be zero-terminated. They are
apparently supposed to be, see [1], but I seem to recall the rule was that
they may be but aren't required to, since the length is already available in
the prefix.

I'd try and wrap it in a _bstr_t and call its conversion operator to a wide
string;

  wcout<< "Data Name : " << (const wchar_t*) _bstr_t(vtProp.bstrVal) <<
endl;

Also, when you get a VARIANT out of a COM method, it's always a good idea to
check its type before attempting to use it. Try and inspect the vtProp.vt
member in your debugger, and compare it to the VT_ flags in wtypes.h. If the
type is anything other than VT_BSTR, the bstrVal member is not valid.

So, you may need to change the type of the value into a string before
printing it, using VariantChangeType(Ex).

Hope that helps,
- Kim

[1] http://msdn.microsoft.com/en-us/library/ms221069.aspx

- BSTRs are length-prefixed and wide strings are zero-terminated
- BSTRs MAY BE zero-terminated but aren't required to be, since the length
is available from the prefix
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.