博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取主机共享文件夹 等
阅读量:5875 次
发布时间:2019-06-19

本文共 3672 字,大约阅读时间需要 12 分钟。

获取主机共享文件夹 
#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;
 
 

转载地址:http://tnzix.baihongyu.com/

你可能感兴趣的文章
poll---socket客户/服务器端编程
查看>>
调整数组中奇数偶数位置
查看>>
数据库SQL优化大总结之 百万级数据库优化方案
查看>>
Lua For Windows 环境配置及使sciTE支持中文
查看>>
找回密码测试注意
查看>>
步进器和分组按钮
查看>>
php 字符串转成二维数组
查看>>
Linux通配符和正则表达式的异同
查看>>
GCD 使用中需要注意的细节以及容易混淆的知识点
查看>>
CCNA学习笔记--VLAN划分及vlan间通信
查看>>
李新海:不甘心,就让自己更忙一点
查看>>
10.WinFor练习--自动播放图片小程序
查看>>
Java - 在WebService中使用Client调用三方的RestAPI
查看>>
Linux 装 Python3.6
查看>>
字典及datetime模块
查看>>
常见的web服务器
查看>>
Android操作系统漏洞允许***者跟踪用户位置
查看>>
一般人不知道的SEO术语!!!
查看>>
Java 本地环境设置
查看>>
Centos yum 安装zabbix3.4
查看>>