delbert Harry
April 29th, 1999, 01:04 PM
How can I convert a CString variable to a LPSTR variable?
|
Click to See Complete Forum and Search --> : CString ------> LPSTR delbert Harry April 29th, 1999, 01:04 PM How can I convert a CString variable to a LPSTR variable? Michael Decker April 29th, 1999, 01:08 PM That may depend on whether or not you'll be modifying the LPSTR. Michael Decker April 29th, 1999, 01:17 PM Look at the example supplied with the CString::GetBuffer() description in the VC++ help. It returns a LPTSTR, but unless you're using UNICODE, it should be the same thing. Joerg Nowak April 29th, 1999, 01:37 PM I use a simple little macro that casts the string class to a pointer The macro is below #define CHARPTR(CMyString) (LPSTR)((LPCTSTR)(CMyString)) CMyString is the name of the string class variable. Then wherever you need to change to a string pointer you use CHARPTR(CMyString). Remember however that any LPSTR variable has to have the appropriate memory set aside, otherwise you will potentially clobber your program. Michael Decker April 29th, 1999, 02:30 PM We shouldn't be showing beginners how to do that. It's an extremely unsafe way to do it. Especially if the developer using the macro doesn't know you've simply typecast your way from a const to a non-const and then assumes that it can be modified. It may introduce a bug that can be difficult to track down. Joerg Nowak April 29th, 1999, 04:47 PM True there is a danger in using this macro if it is used incorrectly. However it can be safely implemented, by using the macro to make a char array copy of the CString in situations where legacy code only accepts char pointers. Additionally it can be used as a function parameter if ( and this is obviously a major caveat), the function is known not to change the passed parameter or where the compiler makes local copies of the passed parameter. It is important to remember that it is a macro and therefore does not have any error checking etc.,and any usage needs to take that into account. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |