C ++ mainly uses C-style strings, while M $ adds many variants of C-style strings in windows. If there are more than one, it's easy to mess up ~
The so-called string is a combination of characters, so we will first set the character type.
First, there are two basic character types: Char and wchar_t. Char is familiar to everyone, so I will skip it. As for wchar_t, It is a unicode wide character, that is, a character 2 bytes, 16 bits. In fact
Typedef unsigned short wchar_t
Define wchar_t
Then for ease of writing (I didn't think there was much difference between MS), M $ re-gave the two basic character types to typedef again, namely:
Typedef char
Typedef wchar_t wchar
To improve compatibility, M $ defines the tchar data type:
# Ifdef uniocde
Typedef wchar tchar
# Else
Typedef char tchar
# End if
In this way, you do not need to worry about whether to use ANSI strings or Unicode. the compiler will automatically choose based on your OS.
Then, M $ defines some string pointer types using the above basic data types.
Lpstr and lpcstr: lpstr are pointers to ANSI strings ending with 0, and the latter is the const pointer.
Typedef char * lpstr
Typedef const char * lpcstr
Lpwstr and lpcwstr: lpwstr are pointers to Unicode strings ending with 0, and the latter is the const pointer.
Typedef wchar * lpwstr
Typedef const wchar * lpcwstr
Similarly, to avoid the trouble of selecting ANSI or Unicode, M $ also adds two string pointer types: lptstr and lpctstr. They are defined as follows:
Typedef tchar * lptstr
# Ifdef Unicode
Typedef lpstr lptstr
# Else
Typedef lpwstr lptstr
# Endif
/////////////////////////////////////
Typedef const tchar * lpctstr
# Ifdef Unicode
Typedef maid
# Else
Typedef maid
# Endif
PS: You will see the existence of pstr/pwstr/ptstr in some places, and so on, with only one l string pointer type above. In fact, this is a long pointer and short pointer problem. Lpxx is a long pointer, and pxx is a short pointer. However, there is no difference between the 32bit systems.
Then let's talk about two mature and useful string types: string and cstring.
String is the standard string of C ++ and must be supported by the string (without. h) header file and STD namespace.
Cstring is a string designed by M $ for MFC. It is more powerful and secure. The atlstr. h file and mfc dll are required. However, someone has separated this type from MFC ~
To enhance the versatility of the program, you must pay attention to the following points:
1. Since char cannot appear in the program, it means that char * cannot be used habitually when it is a string. It should be changed to tchar * or ptstr. The latter type is Windows variables, such as pstr, ptstr, lptstr, lpstr, and pctstr. This is also a big place for people to get started. In fact, it is not so horrible. I take pctstr as an example to explain: p represents a pointer (and LP is a thing, LP is meant to be long pointer, a 16-bit legacy in the Windows era .), C stands for const, T stands for tchar, and STR stands for string. Therefore, pctstr is actually the meaning of const tchar. Pstr means char. Therefore, we cannot use variable type names without T, such as pstr, when representing strings.
2. When it represents a String constant, it cannot be simply enclosed in double quotation marks, because it represents an ASCII string. Similarly, you cannot add L to the front, because it represents Unicode. What our program needs to do is versatility, that is, it is neither ASCII nor Unicode. Therefore, we should add text before the string, such as MessageBox (null, text ("fypher"), text ("FF"), mb_ OK ). Text can also be used as a character. For example, tchar M = text ('A ');
3. tchar FF [50]. How many characters can FF contain? Ha! Do not habitually sizeof (ff). It should be _ countof (FF) or sizeof (FF)/sizeof (tchar ). Because we are not sure whether tchar is Char or wchar_t.
4. I should say goodbye to a bunch of old friends ...... We can no longer use the previous string processing function or character processing function. For example, strlen, strcat, strcmp, etc ...... Because these are ASCII-specific, you can use the t family. Replace the prefix with _ TCS. For example, _ tcslen, _ tcscat, _ tcscmp, and so on ...... By the way, we can add that the whitelist prefix is wchar_t. Well, there is also a sprintf that everyone uses to use. In the future, change it to _ stprintf ~ Haha. Supplement: swprintf is the home of wchar_t. By the way, Windows considers _ tcscpy, _ tcscat and so on unsafe, so using these function compilers will trigger an alarm. You can use the _ tcscpy_s, _ tcscat_s, and other "security" functions provided by windows to specify the buffer size (remember to use _ countof ~! ^_^ ). Windows also introduced a set of string processing functions, such as stringcchcat, which I have never used. Windows also has a string comparison function comparestring. More powerful than _ tcscmp. For example, you can set case-insensitive settings.
5. The istextunicode function can use a series of statistical methods to determine whether a string is a unicode string. The multibytetowidechar and widechartomultibyte functions can convert ASCII and Unicode strings. These are rarely used. Because our program should do "no" ASCII and Unicode.
6. Well, although tchar should be used in most cases, remember the special function getprocadress. Its Parameter can only be char *. Because the function name in the export function table is written in ASCII code ......
7. At the last point, remember to # include <tchar. h>! Haha ~ Because the Windows kernel uses Unicode, the Unicode version of the program must be more efficient than the ASCII version of the program (for example, you do not need to allocate space in the heap when calling a function to convert the parameter to Unicode, then call the Unicode function), so we 'd better add # define Unicode and # DEFINE _ Unicode at the beginning of the program to convert the program to the Unicode version. If the program's string processing is done in full accordance with the above general requirements, there will be no error.
8. Note: Both the STR prefix and the WCS prefix are standard C functions and must have a standard C Runtime Library. The lstr prefix is the native function provided by window and does not require the Standard C Runtime Library.