The first time I used MFC for network programming today, it could be very painful to get the server end of the chat program after a day. Now I will summarize the main problems encountered today. The first one is :: postmessage method, which has plagued me for a long time. The prototype of this method is: postmessage (hwnd, MSG, wparam, lparam); hwnd is the handle of the main window, today I mistakenly thought that the control handle was too busy for a long time, MSG was a message, and the other two were parameters. This method can be used in a multi-threaded environment, while postmessage (MSG, wparam, lparam) this function must depend on an object, so it cannot be used in a multi-threaded environment. Another major problem encountered today is Garbled text. Now let's make a conclusion: char * can be directly assigned to cstring, and the method for converting cstring to Char is as follows:
Wchar * temp1 = Str. getbuffer (Str. getlength () + 1 );
Char * temp = new char [Str. getlength () + 1];
Widechartomultibyte (cp_acp, 0, temp1,-1,
Temp, str. getlength () + 1, null, null );
The following is an article I copied from the Internet. It is a summary of how char * is used to convert it to lpcwstr:
In Windows programming, conversion between strings is often encountered. Conversion from char * To lpcwstr is also a common conversion. The following lists several commonly used conversion methods.
1. multibytetowidechar Function Conversion
The multibytetowidechar function is an API function that converts multiple bytes into a wide byte. Its prototype is as follows:
[CPP]View plaincopy
- Int multibytetowidechar (
- Uint codePage, // code page
- DWORD dwflags, // character-type options
- Lpcstr lpmultibytestr, // string to map
- Int cbmultibyte, // number of bytes in string
- Lpwstr lpwidecharstr, // wide-Character Buffer
- Int cchwidechar // size of Buffer
- );
The lpcwstr type is actually const wchar *.
[CPP]View plaincopy
- Char * szstr = "test string ";
- Wchar wszclassname [256];
- Memset (wszclassname, 0, sizeof (wszclassname ));
- Multibytetowidechar (cp_acp, 0, szstr, strlen (szstr) + 1, wszclassname,
- Sizeof (wszclassname)/sizeof (wszclassname [0]);
2. Convert macros with T2W
[CPP]View plaincopy
- Char * szstr = "test string ";
- Cstring STR = cstring (szstr );
- Uses_conversion;
- Lpcwstr wszclassname = new wchar [Str. getlength () + 1];
- Wcscpy (lptstr) wszclassname, T2W (lptstr) Str. getbuffer (null )));
- Str. releasebuffer ();
3. a2cw Conversion
[CPP]View plaincopy
- Char * szstr = "test string ";
- Cstring STR = cstring (szstr );
- Uses_conversion;
- Lpcwstr wszclassname = a2cw (w2a (STR ));
- Str. releasebuffer ();
The above methods are all tested in the Unicode environment.
First Network Programming