res = NetShareEnum(lpszServer, 502, (LPBYTE*)&pBufPtr, -1, &dwEnriesRead,
DWORD dwScope = RESOURCE_GLOBALNET;
DWORD dwType = RESOURCETYPE_ANY;
NETRESOURCE *pNetResource = NULL;
HANDLE hEnum;
WNetOpenEnum(dwScope, dwType, NULL, pns, &hEnum);
if(hEnum == INVALID_HANDLE_VALUE) return FALSE;
DWORD dwCount = 0xFFFFFFFF;
DWORD dwBufSize = 2048;
LPVOID buffer = new char[2048];
DWORD dwRetVal = WNetEnumResource(hEnum, &dwCount, buffer, &dwBufSize);
pNetResource = (NETRESOURCE*)buffer;
UINT i;
if(dwRetVal != NO_ERROR) return FALSE;
for (i = 0; i<dwCount; i++, pNetResource++)
{
if(RESOURCEUSAGE_CONTAINER == (pNetResource->dwUsage & RESOURCEUSAGE_CONTAINER))
{
GetHostList(pNetResource, nDepth + 1);
}
if(pNetResource->dwUsage ==RESOURCEUSAGE_CONTAINER && pNetResource->dwType ==RESOURCETYPE_ANY)
{
if(NULL != pNetResource->lpRemoteName && nDepth == 2)
{
CString strHostName = pNetResource->lpRemoteName;
if(!strHostName.Left(2).Compare("\\\\"))
{
strHostName = strHostName.Right(strHostName.GetLength() - 2);
}
int nItem = m_HostList.GetItemCount();
m_HostList.InsertItem(nItem, strHostName, 0);
hostent *p = gethostbyname(strHostName);
if(p != NULL)
{
char *pAddr = inet_ntoa(*(in_addr*)p->h_addr);
m_HostList.SetItemText(nItem, 1, pAddr);
}
else
{
m_HostList.SetItemText(nItem, 1, "未知");
}
m_HostList.SetItemText(nItem, 2, pNetResource->lpComment);
}
}
}
delete buffer;
WNetCloseEnum(hEnum);
端口扫描
CSK002Dlg *pDlg = (CSK002Dlg*)pParam;
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(pDlg->m_strAddr);
int nretval;
struct timeval tv;
DWORD lMode = 1;
fd_set fdsock;
SOCKET hSocket;
tv.tv_sec = 2;
tv.tv_usec = 0;
for(int i = 0; i < 14; i++)
{
SOCKET hSocket = socket(AF_INET, SOCK_STREAM, 0);
if(hSocket == INVALID_SOCKET) continue;
ioctlsocket(hSocket, FIONBIO, &lMode);
FD_ZERO(&fdsock);
FD_SET(hSocket, &fdsock);
addr.sin_port = htons(nPortArray[i]);
nretval = connect(hSocket, (const sockaddr*)&addr, sizeof(addr));
if(nretval == SOCKET_ERROR)
{
nretval = select(0, NULL, &fdsock, NULL, &tv);
if(nretval > 0)
{
pDlg->SendMessage(WM_ADDPORT, 0, (LPARAM)&nPortArray[i]);
}
}
else
{
pDlg->SendMessage(WM_ADDPORT, 0, (LPARAM)&nPortArray[i]);
}
closesocket(hSocket);
}
AfxMessageBox("端口扫描已完成!");
pDlg->GetDlgItem(IDC_BUTTON_PORT_SCAN)->EnableWindow(TRUE);
return 0L;