In vs2008, the default character set is Unicode. The characters in cstring are stored in the form of wchar_t, rather than char. You have configured a character set in project-properties-. You can change the character set to a multi-byte character set.
When I use the following code in the Unicode project of VC, the error "error c2664:" gethostbyname "is prompted: The parameter 1 cannot be converted from" cstring "to" const char *"".
Cstring host;
Lphost = gethostbyname (host );
The fastest solution:
Since this function requires ANSI string, I think you have to convert your Unicode onE. There are different approaches. I wocould suggest trying this simple MFC-based method:
Cstring yourstring = ...;
Cstringa ansistring = yourstring;
Gethostbyname (ansistring );
I hope it works.
See also: ct2a macro, widechartomultibyte function.
Other methods:
Conversion of cstring, char *, const char *, and lpctstr
How to assign a variable of the cstring type to a variable of the char * type
1. getbuffer function:
Use the cstring: getbuffer function.
Char * P;
Cstring STR = "hello ";
P = Str. getbuffer (Str. getlength ());
Str. releasebuffer ();
When converting cstring to char *
Cstring STR ("aaaaaaa ");
Strcpy (Str. getbuffer (10), "AA ");
Str. releasebuffer ();
Call getbuffer (int n) when we need a character array, where N is the length of the character array we need. Call releasebuffer () immediately after use ();
It is also important that you do not use char * Where const char * can be used *
2. memcpy:
Cstring MCS = _ T ("CXL ");
Char MCH [20];
Memcpy (MCH, MCS, 20 );
3. Use lpctstr for forced conversion: Do not use it whenever possible
Char * Ch;
Cstring STR;
Ch = (lpstr) (lpctstr) STR;
Cstring STR = "good ";
Char * TMP;
Sprintf (TMP, "% s", (lptstr) (lpctstr) Str );
4,
Cstring MSG;
MSG = MSG + "ABC ";
Lptstr lpsz;
Lpsz = new tchar [msg. getlength () + 1];
_ Tcscpy (lpsz, MSG );
Char * psz;
Strcpy (psz, lpsz );
Cstring class to const char * Conversion
Char A [100];
Cstring STR ("aaaaaa ");
Strncpy (A, (lpctstr) STR, sizeof ());
Or:
Strncpy (A, STR, sizeof ());
The preceding two methods are correct. Because the second parameter type of strncpy is const char *, the compiler automatically converts the cstring class to const char *.
Cstring to lpctstr (const char *)
Cstring CSTR;
Const char * maid = (lpctstr) CSTR;
Convert the string type to the string type
Maid;
Cstring CSTR = lpctstr;
Assign a char * type variable to a cstring type variable.
Values can be assigned directly, for example:
Cstring mystring = "this is a test ";
You can also use constructors, such:
Cstring S1 ("Tom ");
Assign a variable of the cstring type to a variable of the char [] type (string ).
1. sprintf () function
Cstring STR = "good ";
Char TMP [200];
Sprintf (TMP, "% s", (lpcstr) Str );
This forced conversion is equivalent to (lptstr) (lpctstr) Str
When the cstring class variables need to be converted to (char *), use (lptstr) (lpctstr) Str
However, the lpctstr is const char *, that is, the resulting string cannot be written! It is extremely dangerous to forcibly convert it to lptstr to remove const!
If you don't care, you will be finished! To obtain char *, use getbuffer () or getbuffersetlength (). Call releasebuffer () after use ().
2. strcpy () function
Cstring STR;
Char C [256];
Strcpy (C, STR );
Char mychar [1024];
Cstring source = "hello ";
Strcpy (char *) & mychar, (lpctstr) source );