Parameter 1 cannot be converted from "cstring" to "const char *"

Source: Internet
Author: User
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 );


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 ());


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


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 );

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: 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.