Help - Search - Members - Calendar
Full Version: C++ outputs to text file
BleepingComputer.com > Software > Programming
   
aommaster
Hi everyone!

I'm a bit new to this aspect of C++ (I've never done programming like this to this extent) and I believe I have a problem with variable conversions.

What I'm doing is querying the WMI service for hardware information. I followed the MSDN closely and I can have the output shown to the console.

However, I'd like to go one step further and have this output thrown into a text file instead.

The code in question is this:
CODE
VARIANT vtProp;
hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
wcout << "OS Name : " << vtProp.bstrVal; << endl;


I've never dealt with Variants before (but I believe it stored information in the form of pointers which can be converted to different types depending on the need?)

I'd like to have the above output to a text file instead. However, just using the current form of bstr (this is VB String, yes?) does not produce the desired result (I get a chain of numbers instead of the string that is displayed in the console).

IE. This doesn't work:
CODE
myfile << "OS : " <<


and outputs:
"OS : 0016EA2C"

How would I go about converting it to the type I need so that it outputs correctly to a text file?

Thanks!
Billy O'Neal
You need to convert the bstr returned by WMI into a std::string or a null terminated character array first. BSTRs are WMI's standard string type -- they are length prefix rather than null terminated strings commonly used in C.

Hope that helps,
Billy3
aommaster
Hi Billy!

Thanks for your reply smile.gif

Yeah, I figured I had a variable type problem, but was not sure how to convert between the types. I finally found that I could re-cast the variables in pointer form and that worked.

Thanks again!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.