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.

Photo

C++ outputs to text file


  • Please log in to reply
2 replies to this topic

#1 aommaster

aommaster

    I !<3 malware


  • Malware Response Team
  • 5,294 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Dubai
  • Local time:06:55 PM

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!

Edited by aommaster, 11 August 2009 - 02:16 PM.

My website: http://aommaster.com
unite_blue.png
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


BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:07:55 AM

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
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 aommaster

aommaster

    I !<3 malware

  • Topic Starter

  • Malware Response Team
  • 5,294 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Dubai
  • Local time:06:55 PM

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://aommaster.com
unite_blue.png
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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users