Instant messaging on Windows PlatformsProgramTo obtain the input string from the edit box and send it to the other end using the socket function. The project attribute is Unicode.
The string obtained from the edit box is cstring, And the socket function requires a char string. The conversion is a little troublesome. cstring itself does not provide this function.
The solution summarized after Tangle is as follows:
Non-Unicode project:
1. Obtain the memory address of the cstring storage string, which can be forcibly converted or copied directly.
Example:
Cstring strtemp;
Char sztemp [128];
Strtemp = _ T ("abckdkfei ");
Memset (sztemp, 0, sizeof (sztemp ));
Strcpy (sztemp, strtemp. getbuffer (0 ));
2. Use(Lpstr) (lpcstr) forced conversion
Example:
Char * Buf;
cstring STR =" hello ";
Buf = (lpstr) (lpctstr) STR;
The principles of the above two methods are based on the cstring class to save the char string. The first address of the internal string can be obtained to copy the data or directly convert the data.
Unicode Engineering
The preceding method in the Unicode project reports an error. It should be that wcahr cannot be directly converted to Char, so you can use the widechartomultibyte function to convert the above.
Example:
Cstring sendstring;
M_sendedit.getwindowtext (sendstring );
M_sendedit.setwindowtext (L "");
Char Buf [1024] = {'\ 0 '};
Widechartomultibyte (cp_acp, 0, sendstring. getbuffer (0), sendstring. getlength (), Buf, 1024,0, 0 );
Send (clientsocket, Buf, strlen (BUF), 0 );
3.Use the member function of the cstring class: oemtoansi. I did not verify this method. I checked the msdn explanation:
Cstring: oemtoansi
Visual Studio 6.0 this topic has not been rated-
Cstring: oemtoansi
Void oemtoansi ();
Remarks
Converts all the characters in this cstring object from the OEM character set to the ANSI character set. See the http://technet.microsoft.com/zh-cn/library/ms857349.aspx in the C ++ language reference.
This function is not available if _ Unicode is defined.