Unicode + Name Resolution

This is a discussion on Unicode + Name Resolution within the Windows Programming forums, part of the Platform Specific Boards category; So I guess gethostbyname is deprecated, and I am trying to be all polite, and trying to make a unicode ...

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Unicode + Name Resolution

    So I guess gethostbyname is deprecated, and I am trying to be all polite, and trying to make a unicode compatible application with the new name resolution technique of getaddrinfo + getnameinfo. I end up with a dotted IP address representing the host, as a Unicode string. I do not know how to convert this to an IN_ADDR structure, inet_addr only takes ANSI strings.

    Code:
    	ADDRINFOT * addr = { 0 };
    	TCHAR *     host = _T("www.yahoo.com");
    	TCHAR       hostBuffer[256];
    
    
    	int ret = ::GetAddrInfo(host, 0, 0, &addr);
    	if(ret)
    	{
    		ErrorMessage(_T("GetAddrInfo: %d"), ::WSAGetLastError());
    	}
    	if(addr)
    	{
    		::GetNameInfo
    			(addr->ai_addr, sizeof sockaddr, 
    			hostBuffer,     sizeof hostBuffer, 
    			0, 0, NI_NUMERICHOST);
    
    		// unsigned long ip = inet_addr(hostBuffer); //
    		//      how how how how how how how how      //
    	}
    	::FreeAddrInfo(addr);

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    check out wcstombs()

    Kuphryn

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Thanks. Quick question:

    Return Value

    If wcstombs successfully converts the multibyte string, it returns the number of bytes written into the multibyte output string, excluding the terminating NULL (if any). If the mbstr argument is NULL, wcstombs returns the required size in bytes of the destination string. If wcstombs encounters a wide character it cannot convert to a multibyte character, it returns –1 cast to type size_t and sets errno to EILSEQ.

    Remarks

    The wcstombs function converts the wide-character string pointed to by wcstr to the corresponding multibyte characters and stores the results in the mbstr array. The count parameter indicates the maximum number of bytes that can be stored in the multibyte output string (that is, the size of mbstr). In general, it is not known how many bytes will be required when converting a wide-character string. Some wide characters will require only one byte in the output string; others require two. If there are two bytes in the multibyte output string for every wide character in the input string (including the wide character NULL), the result is guaranteed to fit.
    I can't tell what they mean by that second statement. Is it okay for me to say

    Code:
    		int    len = wcstombs(NULL, hostBuffer, 0);
    		char * mbs = new char[len];
    And then call wcstombs again with the dest buffer.
    Last edited by Tonto; 07-31-2006 at 05:00 PM.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    size_t a = wcstombs(NULL, b, wcslen(b)) + 1

    Kuphryn

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Is there any point of me supporting unicode there if I'm just going to convert it back?

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    all api should be unicode and 32-bit api are legacy code

    Kuphryn

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    GetAddrInfo returns an address. There should be no need to call GetNameInfo or inet_addr. If you just need a sockaddr it can be obtained with addr->ai_addr. This has the added advantage of being compatible with other address systems such as IPv6. If you really need an in_addr or even an unsigned long, you can obtain them too:
    Code:
    GetAddrInfo(host, NULL, NULL, &addr);
    
    /* Make sure that we have an IPv4 address before treating it as such. */
    if ( addr->ai_family  == PF_INET &&  
         addr->ai_addrlen == sizeof(struct sockaddr_in) ) 
    {
        /* You can get an in_addr: */
        struct in_addr ia = ((struct sockaddr_in*)(addr->ai_addr))->sin_addr;
    
        /* and an unsigned long: */
        unsigned long ip = ia.S_addr;
    }
    Is there any point of me supporting unicode there if I'm just going to convert it back?
    Generally, no. However, it can make your application easier to update down the track if its mostly unicode rather than zero unicode.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  2. Unicode - a lot of confusion...
    By Jumper in forum Windows Programming
    Replies: 11
    Last Post: 07-05-2004, 07:59 AM
  3. Should I go to unicode?
    By nickname_changed in forum C++ Programming
    Replies: 10
    Last Post: 10-13-2003, 11:37 AM
  4. printing non-ASCII characters (in unicode)
    By dbaryl in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 01:00 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21