Use a C++ std::string in your program, it'll save you a lot of grief with memory management, string lengths, and even
provides a lot of basic (and advanced) operations, with relatively simple syntax ( = for assignment, + for concatenation,
size() for string size ... )
Pass the internal (const) char*-string to API functions when needed by calling std::string::c_str()
Copy the internal char*-string to a char array with std::string::copy() when you must. Remember to add the NULL,
char*-strings suck.
Usage :
#include <string> using namespace std; // to remove the std:: prefix // a pointer to a char*-string (or an array of char*-string) LPSTR* lpPathList; // assign the first char*-string from lpPathList string str1 = lpPathList[0]; // copy the C++ string to another C++ string, it's easy string str2 = str1; // pass the underlying const char*-string to a C API function // The API function cannot modify the string. myApiFunction1( str1.c_str() ); // copy the string into a char*-string buffer, // so that a C API can modify it char buffer[256]; int size = str1.copy( buffer, 255 ); buffer[size] = 0; // Add the NULL terminator myApiFunction2( buffer );
// now put back the modified buffer into the C++ string
str1 = buffer;
// or, if you want to get all the strings from you char*-string array const int count = 10; // Assume you had 10 char*-strings int i; string str; for( i = 0; i < count; ++i ) { str += lpPathList[i]; // Append the char*-string. str += "/n"; // Append a newline to separate them. } // Create a big enough buffer, '+1' for the NULL termination char* buffer = new char[str.size() + 1]; // Copy the combined strings to the buffer int size = str.copy( buffer, str.size() ); buffer[size] = 0; // Add the NULL terminator