void CMyDlg::OnOK()
{
CStringArray nay;
CStringArray iay;
GetNameAndIp(nay, iay);
int nn = nay.GetSize();
int in = iay.GetSize();
if (nn == in)
{
TRACE("Have Tracked:%dConnections\n",in);
}
char strFile[256];
CString strFilePath = GetCurrentDirectory(256,strFile);
strFilePath.Format("%s\\Name_Ip.ini",strFile);
//Check File is exixted
CFileFind finder;
bool findRslt = finder.FindFile(strFilePath);
if (findRslt)
{
finder.Close();
}
else
{
CFile outFile;
outFile.Open(strFilePath,CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
CString content = "[Name_Ip]";
outFile.Write(content,content.GetLength());
outFile.Close();
}
CString nameip;
CString ipname;
for (int qq=0;qq<nn;qq++)
{
nameip.Format(_T("Name%d"),qq);
ipname.Format(_T("Ip%d"),qq);
//HostName And Ip Write To ini File
WritePrivateProfileString("Name_Ip",nameip,nay.GetAt(qq),strFilePath);
WritePrivateProfileString("Name_Ip",ipname,iay.GetAt(qq),strFilePath);
//TRACE(iay.GetAt(qq) + "\n");
}
CDialog::OnOK();
TRACE("++++EndTrack+++++++\n");
//
CDialog::OnOK();
}
void CMyDlg::GetNameAndIp(CStringArray &NameArray, CStringArray &IpArray)
{
NameArray.RemoveAll();
IpArray.RemoveAll();
struct hostent *host;
struct in_addr *ptr;
DWORD dwScope = RESOURCE_CONTEXT;
NETRESOURCE *NetResource = NULL;
HANDLE hEnum;
WNetOpenEnum( dwScope, NULL, NULL, NULL, &hEnum );
WSADATA wsaData;
WSAStartup(MAKEWORD(1,1),&wsaData);
if(hEnum)
{
DWORD Count = 0xFFFFFFFF;
DWORD BufferSize = 10240;
LPVOID Buffer = new char[10240];
WNetEnumResource(hEnum, &Count,Buffer, &BufferSize);
NetResource = (NETRESOURCE*)Buffer;
char szHostName[200];
for( unsigned int i = 0;i < BufferSize/sizeof(NETRESOURCE); i++, NetResource++)
{
if (NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && NetResource->dwType == RESOURCETYPE_ANY )
{
if (NetResource->lpRemoteName)
{
CString strFullName = NetResource->lpRemoteName;
if ( 0 == strFullName.Left(2).Compare(_T("\\\\")) )
strFullName = strFullName.Right(strFullName.GetLength()-2);
gethostname(szHostName,strlen(szHostName));
USES_CONVERSION;
char *pchar = T2A(strFullName.GetBuffer(30));
//char *pchar;
host = gethostbyname(pchar);
if(host == NULL) continue;
ptr = (struct in_addr *) host->h_addr_list[0];
int a = ptr->S_un.S_un_b.s_b1; // 211
int b = ptr->S_un.S_un_b.s_b2; // 40
int c = ptr->S_un.S_un_b.s_b3; // 35
int d = ptr->S_un.S_un_b.s_b4; // 76
CString strTemp;
strTemp.Format(_T("%d.%d.%d.%d"),a,b,c,d);
NameArray.Add(strFullName);
IpArray.Add(strTemp);
strFullName.ReleaseBuffer();
}
}
}
delete Buffer;
WNetCloseEnum( hEnum );
}
WSACleanup();
}