BleepingComputer.com: C++ outputs to text file

Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

C++ outputs to text file

#1 User is offline   aommaster 

  • I !<3 malware
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Team
  • Posts: 5,171
  • Joined: 08-June 08
  • Gender:Male
  • Location:Dubai

Posted 11 August 2009 - 02:15 PM

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:
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:
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!

This post has been edited by aommaster: 11 August 2009 - 02:16 PM

My website: http://www.aommaster.com
Posted Image
Please do not send me PM's requesting for help. The forums are there for a reason : )
If I am helping you and do not respond to your thread for 48 hours, please send me a PM

All my help is free. However, if you would like to make a donation, then please click here.



#2 User is offline   Billy O'Neal 

  • Bleepin Engineer
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,079
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

Posted 11 August 2009 - 03:06 PM

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

#3 User is offline   aommaster 

  • I !<3 malware
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Team
  • Posts: 5,171
  • Joined: 08-June 08
  • Gender:Male
  • Location:Dubai

Posted 11 August 2009 - 04:02 PM

Hi Billy!

Thanks for your reply :thumbsup:

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!
My website: http://www.aommaster.com
Posted Image
Please do not send me PM's requesting for help. The forums are there for a reason : )
If I am helping you and do not respond to your thread for 48 hours, please send me a PM

All my help is free. However, if you would like to make a donation, then please click here.



Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users