获取主机共享文件夹 等

时间:2022-11-19 15:23:48
获取主机共享文件夹 
#include <lm.h>
#pragma comment (lib,"mpr")
#pragma comment (lib,"netapi32")
 
 
DWORD entriesread, totalentries, resume;
 DWORD i, dwLevel;
 PSHARE_INFO_1 pBuffer, bufptr;
 NET_API_STATUS nStatus;
 entriesread = 0;
 totalentries = 0;
 resume = 0;
 dwLevel = 1;
 do
 {
  nStatus = NetShareEnum(NULL, dwLevel, (LPBYTE*)&bufptr,
               0xFFFFFFFF, &entriesread, &totalentries, &resume);
  if((nStatus==ERROR_SUCCESS) || (nStatus==ERROR_MORE_DATA))
  {
   pBuffer = bufptr;
   for(i = 1; i <= entriesread; i++)
   {
    m_FileList.AddString(pBuffer->shi1_netname);
    pBuffer++;
   }
  }
  if(bufptr != NULL)
  {
   NetApiBufferFree(bufptr);
  }
 }while(nStatus == ERROR_MORE_DATA);
 
获取局域网主机共享文件目录
#include <lm.h>
#pragma comment (lib,"mpr.lib")
#pragma comment (lib,"Netapi32")
 
 
PSHARE_INFO_502 pBufPtr, p;
    NET_API_STATUS res;
 TCHAR szText[MAX_PATH];
 _stprintf(szText, L"\\\\%s", lpszAddr);
    WCHAR *lpszServer = (LPTSTR)szText;
 DWORD dwEnriesRead = 0;
 DWORD dwTotalEntries = 0;
 DWORD dwResume = 0;
 m_FileList.DeleteAllItems();
 do
 {
        res = NetShareEnum(lpszServer, 502, (LPBYTE*)&pBufPtr, -1, &dwEnriesRead,
                    &dwTotalEntries, &dwResume);
 
        if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
        {
              p = pBufPtr;
              for(DWORD i = 0; i < dwEnriesRead; i++)
              {
      int nCount = m_FileList.GetItemCount();
      m_FileList.InsertItem(nCount, p->shi502_netname, 0);
      m_FileList.SetItemText(nCount, 1, p->shi502_path);
      p++;
     }
     NetApiBufferFree(pBufPtr);
  }
  else
  {
   AfxMessageBox(L"未读取到共享文件夹");
  }
 
  }while(res == ERROR_MORE_DATA);
 
 
在局域网查找存活主机
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;