How to use string functions with winapi?

Mar 9, 2014 at 5:49pm
So, I am making a program (in win32) that picks an episode of a TV show randomly. I want to make a function string EpisodePicker() that will pick the episode, and call it when the user clicks a button. I would like a function that returns a word, basically. How would I do that?
Mar 11, 2014 at 11:13am
Exactly like you posted have a function returning std::string

something like
1
2
3
4
5
6
7
std::string EpisodePicker()
{
    //some code 
    std::string episode = "Clone Wars 2-4"; //implement your logic to pick this
    //some other code
    return episode;
}
Last edited on Mar 11, 2014 at 11:13am
Mar 12, 2014 at 1:52am
Oh, yeah, thank you. But I figured it out. I was trying to use CreateWindow() to show static text on the screen, and I just made the EpisodePicker() function to return a LPSTR, which is the parameter for the window name. Thank you anyway.
Last edited on Mar 12, 2014 at 1:52am
Mar 12, 2014 at 6:49pm
You can keep confusion at bay by always remembering that the WinApi functions all take pointers to null terminated strings as parameters, not string objects, as in Std C++ Lib String Class or wstring. Its of course convenient to perform string manipulations on string objects, but in passing one to a WinApi function you need to use the string::c_str() method. When retrieving a string pointer from a WinApi function, assigning it to a std::string will give you something you can perhaps more easily work with.
Topic archived. No new replies allowed.