Jump to content
  • Advertisement
Sign in to follow this  
Grahf750

winApi string vs char*

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

all of the functions in the winApi seem to want char*s. I like strings much more because they are easier to work with but it seems I may have to use char*s while doing api programming. Do you guys have any ideas on a way I can use strings with the api instead of char*s I know I can convert a string into a c style string but when I get some text from a text box I get it as a char* and so it would be useless to convert it to a string and then back.

Share this post


Link to post
Share on other sites
Advertisement
No, it wouldn't necessarily be useless to convert it to a string and then back. Pretty much the only time you would need to create your own C-style string variables (char* / char[]) is, like you said, when getting the data from a control or other Win32 object. You can then assign it to a std::string directly if you need to play around with it, and I'm sure you already know about the .c_str() function. So I would definitely recommend using std::strings wherever possible.

Cheers,
Twilight Dragon

Share this post


Link to post
Share on other sites
1) Send a const char* to an API function from a std::string (i.e. the function will not change the data):


ApiFunction(myString.c_str());


2) Send a non-const char* to an API function (from a std::string) that will change the string contents but not its length, and which doesn't care that there isn't a null terminator at the end (these are pretty rare):


ApiFunction(&(myString[0]));


This is extracting a raw pointer to memory that happens to be within "the string object" (actually it points to a raw char* somewhere else and you effectively clone that pointer, but it's the same effect), and is quite dangerous. With the wrong API call it could invalidate the string object's idea of the string length or even scribble on memory you don't own (and thus segfault). Of course, you have that sort of danger all the time with char*'s :)

3) Send a non-const char* to an API function (from a std::string) that may change the string contents and length - i.e. we must copy the string into some buffer that is big enough to hold whatever the API might try to put in there:


char tempBuffer[API_EXPECTED_BUFFER_SIZE];
strcpy(tempBuffer, myString.c_str());
ApiFunction(tempBuffer);


4) Receive a char* from an API function into a std::string:


std::string result(ApiFunction());


Depending on specific circumstances (in particular, how badly the API is designed), there may be additional memory management steps required. When in doubt, consult the API doc.

Share this post


Link to post
Share on other sites
I found the first one you said TDragon and did that but I didn't even think of trying to just set one equal to the other I was looking for a function man that is so easy.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!