/*地址列表*/
int j=0;
for(;phostent->h_addr_list[j]!=NULL;j++)
{
DWORD num2=MultiByteToWideChar(CP_ACP,0,phostent->h_addr_list[j],-1,NULL,0);
LPWSTR ipAddr=new wchar_t[num2];
MultiByteToWideChar(CP_ACP,0,phostent->h_addr_list[j],num2,ipAddr,num2);
m_ipList.InsertString(j,ipAddr);
delete ipAddr;
ipAddr=NULL;
}
不多说了,我调用的函数时gethostbyname()返回给hostent类型的变量phostent让后获取地址列表就出现
了这个问题,ascii都已经转换为unicode了!
http://download.csdn.net/detail/geoff08zhang/4571358
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
void EnumIP(LPCTSTR szHostname)
{
WSADATA wsaData;
int iErr = WSAStartup(MAKEWORD(2, 2), &wsaData );
if ( iErr == 0 )
{
HOSTENT *pHostEnt = gethostbyname( szHostname );
if(pHostEnt)
{
int nAdapter = 0;
while ( pHostEnt->h_addr_list[nAdapter] )
{
// pHostEnt->h_addr_list[nAdapter] is the current address in host
struct in_addr *pIpAddr =
(struct in_addr *)pHostEnt->h_addr_list[nAdapter]; //CHAR *szAddr = inet_ntoa(*pIpAddr);
TCHAR szAddr[32];
_stprintf_s(szAddr, _T("%u.%u.%u.%u"),
pIpAddr->s_net, //b1
pIpAddr->s_host, //b2
pIpAddr->s_lh, //b3
pIpAddr->s_impno); //b4
TRACE(_T("%s==>[%s]\n"), szHostname, szAddr); nAdapter++; //for next
}
}
else
{
iErr = WSAGetLastError();
} WSACleanup();
} ASSERT(iErr == 0);
}
我感觉网络编程方面编码转换很烦人,先不结贴了,有经验的来指导指导,谢谢了!
我下面的代码编译通过了。void CGetIPDlg::OnTest()
{
// TODO: 在此添加控件通知处理程序代码
WSADATA wsaData;
int iErr = WSAStartup(MAKEWORD(2, 2), &wsaData );
if(iErr==0)
{
CString szHostName;
char *hostname;
GetDlgItem(HostName)->GetWindowText(szHostName); DWORD dwNum0=WideCharToMultiByte(CP_ACP,0,szHostName,-1,NULL,0,NULL,NULL);//dwNum0返回的szHostName字符串的长度 DWORD dwNum1=WideCharToMultiByte(CP_ACP,0,szHostName,dwNum0,NULL,0,NULL,NULL);//dwNum1是hostname缓冲区需要的大小 hostname=new char[dwNum1];
WideCharToMultiByte(CP_ACP,0,szHostName,dwNum0,hostname,dwNum1,NULL,NULL);//将unicode编码转换为ascii HOSTENT *pHostEnt=gethostbyname(hostname);
if(pHostEnt)
{
int nAdapter=0;
while(pHostEnt->h_addr_list[nAdapter])
{
struct in_addr *pIpAddr=(struct in_addr*)pHostEnt->h_addr_list[nAdapter];
TCHAR szAddr[32];
wsprintf(szAddr,_T("%u.%u.%u.%u"),pIpAddr->S_un.S_un_b.s_b1,pIpAddr->S_un.S_un_b.s_b2,pIpAddr->S_un.S_un_b.s_b3,pIpAddr->S_un.S_un_b.s_b4);
nAdapter++;//for next
MessageBox(szAddr);
}
}
else
{
iErr=WSAGetLastError();
}
delete hostname;
WSACleanup();
}
ASSERT(iErr==0);
}