windows下vs2010显示本机ip无法通过,源码如下,请求指教,多谢

windows下vs2010显示本机ip无法通过,源码如下,请求指教,谢谢#include StdAfx.h#includeafx.h#include

windows下vs2010显示本机ip无法通过,源码如下,请求指教,谢谢
#include "StdAfx.h" 
#include<afx.h>
#include<winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main(int argc,char**argv)
{
   
  struct hostent*hn;
  CString strIPAddr;
  char HostName[32];
  if(gethostname(HostName,sizeof(HostName))==0)
  {
if( hn=gethostbyname(HostName))
{
strIPAddr=inet_ntoa(*(struct in_addr*)hn->h_addr_list[0]);
cout<<strIPAddr<<endl
}
 
  }  
return 0;
}

[解决办法]

探讨
可能需要先WSAStartup