根据主机名称/IP查询主机IP/名称
char * CMyTestDlg::GetHostbyIPorName(char* server)
{
    struct hostent *hp;
    unsigned int addr; 
    WSADATA wsaData;    if (WSAStartup(0x202,&wsaData) == SOCKET_ERROR)
{
WSACleanup(); 
return "";
} if (isalpha(server[0])) {/* server address is a name */ 
hp = gethostbyname(server); 

else  { /* Convert nnn.nnn address to a usable one */ 
TRACE(L"ip = %s\n", server);
addr = inet_addr(server); 
hp = gethostbyaddr((char *)&addr,4,AF_INET); 
}
if(NULL == hp) {
WSACleanup(); 
return "";
}     .......
}调用处:
char *hname;
char ip[50]; memset(ip,0,50);
//从ListBox控件中获取选定的主机名称/IP
m_IP_List.GetText(m_IP_List.GetCurSel(), (LPTSTR)ip);
hname = GetHostbyIPorName(ip);在Unicode环境下编译时,GetHostbyIPorName中hp得到的返回值总是NULL,跟踪到函数体内,主机名称/IP均正确。而如果在gethostbyname()或inet_addr()中输入直接主机名称/IP,如gethostbyname("tser")/inet_addr("192.168.10.14"),就能得到正确的结果。请问各位大虾这是为什么? 我怀疑是调用处字符串转换还是有问题,但不知怎么修改。请各位不吝赐教!!! 谢谢!!!

解决方案 »

  1.   

    UNICODE字符串是uchar*,不是char*,如果你要两者兼容需要用TCHAR
      

  2.   

    gethostbyname()/inet_addr()的参数要求是const char far *类型,传入TCHAR类型编译时出错!!
    请大家多帮忙!!
      

  3.   

    MultiByteToWideCharThe MultiByteToWideChar function maps a character string to a wide-character (Unicode) string. The character string mapped by this function is not necessarily from a multibyte character set. int MultiByteToWideChar(
      UINT CodePage,         // code page
      DWORD dwFlags,         // character-type options
      LPCSTR lpMultiByteStr, // string to map
      int cbMultiByte,       // number of bytes in string
      LPWSTR lpWideCharStr,  // wide-character buffer
      int cchWideChar        // size of buffer
    );
      

  4.   

    WideCharToMultiByteThe WideCharToMultiByte function maps a wide-character string to a new character string. The new character string is not necessarily from a multibyte character set. int WideCharToMultiByte(
      UINT CodePage,            // code page
      DWORD dwFlags,            // performance and mapping flags
      LPCWSTR lpWideCharStr,    // wide-character string
      int cchWideChar,          // number of chars in string
      LPSTR lpMultiByteStr,     // buffer for new string
      int cbMultiByte,          // size of buffer
      LPCSTR lpDefaultChar,     // default for unmappable chars
      LPBOOL lpUsedDefaultChar  // set when default char used
    );
      

  5.   

    The parameter UINT CodePage is used to perform then conversion.This parameter can be given the value of any code page that is installed or available in the system.You can specify the value CP_ACP (Ansi Code Page).
      

  6.   

    问题已经得到解决,非常感谢 promaster(财源广进) 帮助
      

  7.   

    #ifdef _UNICODE
    struct _wfinddata_t c_file;
    #else
    struct _finddata_t c_file;
    #endif
    上面的代码是自适应的有的时候需要强制类型转换,如下面的例子HINSTANCE ghinstWNASPI32;
    LPGETASPISUPPORT gpGetASPISupport;
    LPSENDASPICOMMAND gpSendASPI32Command;ghinstWNASPI32 = LoadLibrary( _T("WNASPI32") );
    gpGetASPISupport=(LPGETASPISUPPORT) GetProcAddress( ghinstWNASPI32, (LPSTR)(LPCSTR)("GetASPI32SupportInfo") );
    gpSendASPI32Command=(LPSENDASPICOMMAND) GetProcAddress( ghinstWNASPI32, (LPSTR)(LPCSTR)"SendASPI32Command" );
    最近才把一个工程改成Unicode,函数需要改动的位置有500多处。整整改了3天,有些也不知道改的对不对