www.pudn.com > DmyNP_source.rar > GetHname.c, change:2004-12-02,size:2571b


/*--> 
Damayi Net-Phone, 2004/11 (damayi.org) 
<--*/ 
 
#include "src\winmain.h" 
 
#define WM_GETHNAME WM_USER+79 
static char gheb[MAXGETHOSTSTRUCT]; 
static HANDLE ghGhn; 
 
//提示框窗口回调函数 
static BOOL CALLBACK _GetseripCallback(HWND h, UINT u, WPARAM w, LPARAM l) 
{ 
   RECT rect; 
   switch(u) 
   { 
      case WM_INITDIALOG: 
         ghGhn = WSAAsyncGetHostByName(h,WM_GETHNAME,SERNAME,gheb,sizeof(gheb)); 
         if(ghGhn==0) 
         { 
            EndDialog(h, 0); 
            PromptError("无法获取服务器地址! (网络故障)"); 
            break; 
         } 
         SetWindowText(h, "DmyNP - 正在获取服务器地址..."); 
         GetClientRect(h, &rect); 
         CreateCaret(h, (HBITMAP)1, rect.right, rect.bottom); 
         SetCaretPos(0, 0); 
         ShowCaret(h); 
         break; 
      case WM_CLOSE: 
         if(ghGhn) WSACancelAsyncRequest(ghGhn); 
         DestroyCaret(); 
         EndDialog(h, 0); 
         return TRUE; 
      case WM_GETHNAME: 
         if(w!=(WPARAM)ghGhn) break; 
         DestroyCaret(); 
         if(WSAGETASYNCERROR(l)==0) 
            EndDialog(h, *((int*)(((HOSTENT*)gheb)->h_addr_list[0]))); 
         else 
         { 
            EndDialog(h, 0); 
            wsprintf(g_pTmp, strGETSERIP, SERNAME, URLNAME); 
            MessageBox(0, g_pTmp, "DmyNP", MB_ICONWARNING); 
         } 
         ghGhn = 0; 
         return TRUE; 
   } 
   return FALSE; 
} 
 
//获取服务器IP地址 
//备注: 成功返回网络顺序的IP地址, 失败返回0。 
static DWORD GetSerIP(void) 
{ 
   int iRst; 
   iRst = DialogBox(g_hInst, "Prompt", 0, _GetseripCallback); 
   if(iRst!=-1) 
      return (DWORD)iRst; 
   else 
   { 
      PromptError("加载对话框资源失败! (GetSerIP) "); 
      return 0; 
   } 
} 
 
//获取本机IP地址 
//参数: IP列表索引号 
//备注: 返回网络顺序的IP地址。 
static DWORD GetLocIP(DWORD dwInx) 
{ 
   int i; 
   HOSTENT *pstHEnt; 
   gethostname(g_pTmp, sizeof(g_pTmp)); 
   pstHEnt = gethostbyname(g_pTmp); 
   if(pstHEnt==0) return inet_addr("127.0.0.1"); 
   for(i=0; i<10; i++) 
   { 
      if(!pstHEnt->h_addr_list[i]) break; 
      if((DWORD)i==dwInx) return *((DWORD*)(pstHEnt->h_addr_list[i])); 
   } 
   return inet_addr("127.0.0.1"); 
} 
 
//获取主机地址 
//返回: 成功返回零, 失败返回非零。 
int GetHnameIP(void) 
{ 
   if(g_IV.bStartMd) return 0; 
   g_SO.dwLocIP = GetLocIP(g_IV.dwLocAr); 
   switch(g_IV.dwSerAr) 
   { 
      case 1: g_SO.dwSerIP=inet_addr(LOCIP_1); break; 
      case 2: g_SO.dwSerIP=inet_addr(LOCIP_2); break; 
      default: g_SO.dwSerIP=GetSerIP(); 
   } 
   return !g_SO.dwSerIP; 
}