Re: WMI question
John Smith <[email protected]>
| Newsgroups | gmane.comp.windows.off-topic |
|---|---|
| Message-ID | <[email protected]> |
Hi Kim, I think you're on to something here, I used a query that normally causes the program to crash and I commented out the wcout<< "Data Name : " << vtProp.bstrVal<< endl; line and although the program didn't print any information, it also did not crash, which is a positive sign. I did try casting the bstr to a const wchar_t* but it didn't seem to help, I wonder if I could load the data into an array and step through it in a for loop just to see what information is actually there before it is sent to wcout( )? I'll keep working and see what I can come up with. Thanks again for your help. John On Sat, Dec 4, 2010 at 12:38 AM, Kim Gräsman <[email protected]> wrote: > > > 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 > > >