winApi string vs char*
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.
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
Cheers,
Twilight Dragon
1) Send a const char* to an API function from a std::string (i.e. the function will not change the data):
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):
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:
4) Receive a char* from an API function into a std::string:
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement