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.
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.
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.
Bookmarks