Sign in to follow this  
crazy_andy

CString to char* :(

Recommended Posts

more data conversion problems :( I have a url inputted through an edit box. I parse this url into hostname, filepath and filename now for the following line I need the hostname as a const char * if (inet_addr(HostName) == INADDR_NONE) I have googled and tried the GetBuffer method, but to no avail. Also can someone point me to a good unicode explanation tutorial.

Share this post


Link to post
Share on other sites
Sure!

CString HostName = "hostname";
if (inet_addr( HostName.operator LPCSTR() ) == INADDR_NONE)

I'm pretty sure that should work for you, you can also const char* cast it if you wanted to as well.

CString HostName = "hostname";
if (inet_addr( (const char*)(HostName.operator LPCSTR()) ) == INADDR_NONE)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Drew_Benton
Sure!

CString HostName = "hostname";
if (inet_addr( HostName.operator LPCSTR() ) == INADDR_NONE)



No need for the convoluted syntax. This should work just fine:
CString HostName = "hostname";
if (inet_addr( (LPCSTR)HostName ) == INADDR_NONE)

Share this post


Link to post
Share on other sites
In general, an operator foo for a class, where foo is an existing typename rather than some symbol describing an operator, is a "cast operator", used to perform casts to foo when they are requested. LPCSTR is simply a typedef for a char* ("long pointer to C-style string"); thus its presence in the CString definition means we can obtain a const char* by simply casting to that type, as the AP illustrates. (You could also be more explicit by writing static_cast<const char*>(HostName).)

Note that CStrings are nearly as ugly as working with char*'s directly, and you really should prefer std::string for any textual data in C++ (to the extent that it's possible - here you're working with someone else's library, so you'll have to bite the bullet). Note also that std::string does *not* provide this cast operator (but instead a member function .c_str() which performs the task), and the people who designed the library have very good reasons for having done it this way (unfortunately I can't recall at the moment what they are).

Share this post


Link to post
Share on other sites
ok, the

Hostname.Operatoer LPCSTR()

gives an error: "const char *: is ambiguouse ..."

the

(LPCSTR)hostname

gives the error: " can nt convert cstring to LPCSTR"

and as I said in my first post getbuffer doesn't work either I get

"can not coonvert paramater 1 from wchar _t* to const char*

Share this post


Link to post
Share on other sites
neither work :(

there must be a way to do this.

A CEdit box returns a CString, and the inet_addr needs a char *

Is there any other function to replace either of these, to either start with a char[] or use a cstring?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by crazy_andy
neither work :(

there must be a way to do this.

A CEdit box returns a CString, and the inet_addr needs a char *

Is there any other function to replace either of these, to either start with a char[] or use a cstring?


tbh, i don't understand why you're having problems - CString::GetBuffer get buffer will explicitely return an LPCTSTR, so this should be ok as long as UNICODE isn't defined in your project. (like apoch said)

As an alternative to using CString, and at the same time writing unicode/mbcs compliant code to be safe, you could write something like:

TCHAR szHostname[ MAX_BUFFER + 1 ];
m_edtUrl.GetWindowText( szHostname, MAX_BUFFER );
const char* pcHostname( T2CA( szHostname ) );

if ( inet_addr( pcHostname ) == INADDR_NONE )

Assuming that your edit box is called m_edtUrl. i've not tested this, and you might need to include atlconv.h in your pch - but it should explicitely get you a char* in all cases.

Share this post


Link to post
Share on other sites
Quote:
Original post by crazy_andy
neither work :(

there must be a way to do this.

A CEdit box returns a CString, and the inet_addr needs a char *

Is there any other function to replace either of these, to either start with a char[] or use a cstring?




Post the exact code you're trying to use and any/all compiler or linker warnings. I have a feeling it's just a simple miscommunication somewhere. I do know for a fact the solutions that have been posted will do exactly what you need to do, but sometimes going from an answer to an implemented solution can be tricky.

Share this post


Link to post
Share on other sites
OK, the AP said AS LONG AS UNICODE IS NOT DEFINED, which was the problem. I changed it, and it works great now. Now I need to read that tutorial that Drew_Benton posted, which I will do as soon as I get some free time. Thanks alot everyone for all your help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this