First Network Programming

Source: Internet
Author: User

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
  1. Int multibytetowidechar (
  2. Uint codePage, // code page
  3. DWORD dwflags, // character-type options
  4. Lpcstr lpmultibytestr, // string to map
  5. Int cbmultibyte, // number of bytes in string
  6. Lpwstr lpwidecharstr, // wide-Character Buffer
  7. Int cchwidechar // size of Buffer
  8. );

The lpcwstr type is actually const wchar *.

 

[CPP]View plaincopy
  1. Char * szstr = "test string ";
  2. Wchar wszclassname [256];
  3. Memset (wszclassname, 0, sizeof (wszclassname ));
  4. Multibytetowidechar (cp_acp, 0, szstr, strlen (szstr) + 1, wszclassname,
  5. Sizeof (wszclassname)/sizeof (wszclassname [0]);


2. Convert macros with T2W

[CPP]View plaincopy
  1. Char * szstr = "test string ";
  2. Cstring STR = cstring (szstr );
  3. Uses_conversion;
  4. Lpcwstr wszclassname = new wchar [Str. getlength () + 1];
  5. Wcscpy (lptstr) wszclassname, T2W (lptstr) Str. getbuffer (null )));
  6. Str. releasebuffer ();


 

3. a2cw Conversion

[CPP]View plaincopy
  1. Char * szstr = "test string ";
  2. Cstring STR = cstring (szstr );
  3. Uses_conversion;
  4. Lpcwstr wszclassname = a2cw (w2a (STR ));
  5. Str. releasebuffer ();


The above methods are all tested in the Unicode environment.

First Network Programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.