winApi string vs char*

Started by
5 comments, last by TDragon 18 years, 7 months ago
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.
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
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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.
Is there an easy way to convert a c string into an stl string?
Yes... "std::string mystlstring(mycstring);", or "mystlstring = mycstring;".
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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.
Yep. :)

The string class overloads operator= for char arrays and pointers. Pretty handy.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++

This topic is closed to new replies.

Advertisement