求大神,C++如何检索局域网主机的共享文件?

SureGOGOGO 2016-09-06 04:24:04
今天下载一个枚举远程主机共享目录的程序,但是发现只能读取本机的共享目录,我想检索的是局域网主机的共享目录,在资源管理器是可以打开该远程的共享目录的,其中程序参考MSDN的,代码我都看得懂,,,关键代码如下,对话框头文件:
// WL006Dlg.h : header file
//

#if !defined(AFX_WL006DLG_H__23B4BD6F_06B1_4F90_A8FD_FE97EEC2F0E5__INCLUDED_)
#define AFX_WL006DLG_H__23B4BD6F_06B1_4F90_A8FD_FE97EEC2F0E5__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CWL006Dlg dialog

class CWL006Dlg : public CDialog
{
// Construction
public:
CWL006Dlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
//{{AFX_DATA(CWL006Dlg)
enum { IDD = IDD_WL006_DIALOG };
CListCtrl m_FileList;
CString m_strAddr;
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CWL006Dlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
HICON m_hIcon;
void GetShareDir(LPCTSTR lpszAddr);
// Generated message map functions
//{{AFX_MSG(CWL006Dlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnButtonFind();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_WL006DLG_H__23B4BD6F_06B1_4F90_A8FD_FE97EEC2F0E5__INCLUDED_)


源文件:
// WL006Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "WL006.h"
#include "WL006Dlg.h"
#include "ConnectDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWL006Dlg dialog

CWL006Dlg::CWL006Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CWL006Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWL006Dlg)
m_strAddr = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWL006Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWL006Dlg)
DDX_Control(pDX, IDC_LIST_FILE, m_FileList);
DDX_Text(pDX, IDC_EDIT_ADDR, m_strAddr);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWL006Dlg, CDialog)
//{{AFX_MSG_MAP(CWL006Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_FIND, OnButtonFind)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWL006Dlg message handlers

BOOL CWL006Dlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
DWORD dwExStyle = m_FileList.GetExtendedStyle();
dwExStyle |= LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT;
m_FileList.SetExtendedStyle(dwExStyle);
m_FileList.InsertColumn(0, _T("共享文件名"), LVCFMT_LEFT, 120);
m_FileList.InsertColumn(1, _T("共享文件路径"), LVCFMT_LEFT, 240);
return TRUE; // return TRUE unless you set the focus to a control
}

void CWL006Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CWL006Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CWL006Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}


void CWL006Dlg::OnButtonFind()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
GetShareDir(m_strAddr);
}

void CWL006Dlg::GetShareDir(LPCTSTR lpszAddr)
{
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);
}



...全文
522 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-09-20
  • 打赏
  • 举报
回复
上下求索
SureGOGOGO 2016-09-08
  • 打赏
  • 举报
回复
引用 14 楼 lishunihaoa 的回复:
[quote=引用 13 楼 zhao4zhong1 的回复:]
WinExec("cmd /c net view VIRTUAL>netviewoutput.txt",SW_HIDE);
//然后读文件netviewoutput.txt的内容
谢谢赵老师,也只能这样了。。。都搞了好久[/quote] 问题已经解决了,还是用EnumShare函数,不过要把上面的PSHARE_INFO_502,改为PSHARE_INFO_0
引用 14 楼 lishunihaoa 的回复:
[quote=引用 13 楼 zhao4zhong1 的回复:]
WinExec("cmd /c net view VIRTUAL>netviewoutput.txt",SW_HIDE);
//然后读文件netviewoutput.txt的内容
谢谢赵老师,也只能这样了。。。都搞了好久[/quote] 问题终于解决了,好幸苦,把 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); 改成 PSHARE_INFO_0 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,0, (LPBYTE*)&pBufPtr, MAX_PREFERRED_LENGTH, &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->shi0_netname, 0); p++; } NetApiBufferFree(pBufPtr); } else { CString errcode=""; errcode.Format(L"无法读取共享文件,错误码:%u",res); AfxMessageBox(errcode); } }while(res == ERROR_MORE_DATA); 成功读取到共享文件
赵4老师 2016-09-07
  • 打赏
  • 举报
回复
C:\>net help view 此命令的语法是: NET VIEW [\\computername [/CACHE] | /DOMAIN[:domainname]] NET VIEW /NETWORK:NW [\\computername] NET VIEW 用于显示一个计算机上共享资源的列表。当不带选项使用本命令时,它就会显示 当前域或网络上的计算机上的列表。 \\computername 指用户希望浏览其共享资源的计算机。 /DOMAIN:domainname 指定用户希望浏览有效的计算机所在的域。如果省略了域名, 就会显示局域网络上的所有域。 /NETWORK:NW 显示 NetWare 网络上所有可用的服务器。如果指定了一个计算 机名,就会显示 NetWare 网络中那个计算机上的可用资源。 /CACHE 显示指定计算机上的脱机客户资源缓存设置
SureGOGOGO 2016-09-07
  • 打赏
  • 举报
回复
而且我上面贴出的能检测到本机的共享,查了MSDN,是可以查看远程主机的共享的,程序运行结果如下:



SureGOGOGO 2016-09-07
  • 打赏
  • 举报
回复
引用 8 楼 zhao4zhong1 的回复:
再供参考: C:\>net help share 此命令的语法是: NET SHARE sharename sharename=drive:path [/GRANT:user,[READ | CHANGE | FULL]] [/USERS:number | /UNLIMITED] [/CACHE:Manual | Documents| Programs | None ] sharename [/USERS:number | /UNLIMITED] [/REMARK:"text"] [/CACHE:Manual | Documents | Programs | None] {sharename | devicename | drive:path} /DELETE NET SHARE 使网络用户可以使用某一服务器上的资源。 当不带选项使用本命令 时,它会列出该计算机上正在被共享的所有资源。对于每一种资源,Windows 都 会报告其设备名或路径名,以及与之相关的描述性注释。 sharename 指共享资源的网络名。输入 NET SHARE 和一个共享名只会 显示那个共享的有关信息。 drive:path 指定将被共享的目录的绝对路径。 /GRANT:user,perm 创建有安全性描述符的共享,对指定用户授予要求的权限。 此选项可以多次使用,将共享权限赋予多个用户。 /USERS:number 设置可以同时访问共享资源的最大用户数。 /UNLIMITED 指定可以同时访问共享资源的用户数目不受限制。 /REMARK:"text" 添加一个有关资源的描述性注释,应将文本包含在引号中。 devicename 指一个或多个被共享名所共享的打印机 (LPT1: 至 LPT9:)。 按共享名共享。 /DELETE 停止共享资源。 /CACHE:Manual 对来自此共享的程序和文档启用手动客户缓存 /CACHE:Documents 对来自此共享的文档启用自动缓存 /CACHE:Programs 对来自此共享的程序和文档启用自动缓存 /CACHE:None 禁用此共享的缓存 NET HELP command | MORE 用于逐屏显示帮助。
您的意思是叫我测试一下该主机是否可以访问?在资源管理器是可以访问该主机的共享的。
赵4老师 2016-09-07
  • 打赏
  • 举报
回复
再供参考: C:\>net help share 此命令的语法是: NET SHARE sharename sharename=drive:path [/GRANT:user,[READ | CHANGE | FULL]] [/USERS:number | /UNLIMITED] [/CACHE:Manual | Documents| Programs | None ] sharename [/USERS:number | /UNLIMITED] [/REMARK:"text"] [/CACHE:Manual | Documents | Programs | None] {sharename | devicename | drive:path} /DELETE NET SHARE 使网络用户可以使用某一服务器上的资源。 当不带选项使用本命令 时,它会列出该计算机上正在被共享的所有资源。对于每一种资源,Windows 都 会报告其设备名或路径名,以及与之相关的描述性注释。 sharename 指共享资源的网络名。输入 NET SHARE 和一个共享名只会 显示那个共享的有关信息。 drive:path 指定将被共享的目录的绝对路径。 /GRANT:user,perm 创建有安全性描述符的共享,对指定用户授予要求的权限。 此选项可以多次使用,将共享权限赋予多个用户。 /USERS:number 设置可以同时访问共享资源的最大用户数。 /UNLIMITED 指定可以同时访问共享资源的用户数目不受限制。 /REMARK:"text" 添加一个有关资源的描述性注释,应将文本包含在引号中。 devicename 指一个或多个被共享名所共享的打印机 (LPT1: 至 LPT9:)。 按共享名共享。 /DELETE 停止共享资源。 /CACHE:Manual 对来自此共享的程序和文档启用手动客户缓存 /CACHE:Documents 对来自此共享的文档启用自动缓存 /CACHE:Programs 对来自此共享的程序和文档启用自动缓存 /CACHE:None 禁用此共享的缓存 NET HELP command | MORE 用于逐屏显示帮助。
赵4老师 2016-09-07
  • 打赏
  • 举报
回复
仅供参考:
ServerShare = "\\192.168.3.56\d$"
UserName = "somedomain\someuser"
Password = "somepassword"

Set NetworkObject = CreateObject("WScript.Network")

NetworkObject.MapNetworkDrive "", ServerShare, False, UserName, Password

Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Directory = FSO.GetFolder(ServerShare)
        For Each FileName In Directory.Files
            WScript.Echo FileName.Name
        Next
        Set FileName = Nothing
    Set Directory = Nothing
Set FSO = Nothing

NetworkObject.RemoveNetworkDrive ServerShare, True, False

Set NetworkObject = Nothing
SureGOGOGO 2016-09-07
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
Enumerating Network Resources Windows NT Only To enumerate a network container resource, your application should pass the address of a NETRESOURCE structure to the WNetOpenEnum function. WNetOpenEnum creates a handle to the resource described by the NETRESOURCE structure. The application then passes this handle to the WNetEnumResource function, which returns information about the resource in the form of an array of NETRESOURCE structures. When the handle is no longer needed, the application can close it by calling the WNetCloseEnum function. Your application can continue enumerating any container resource described in the array of NETRESOURCE structures retrieved by WNetEnumResource. If the dwUsage member of the NETRESOURCE structure is RESOURCEUSAGE_CONTAINER, the application can pass the address of that structure to WNetOpenEnum to open the container and continue the enumeration. If dwUsage is RESOURCEUSAGE_CONNECTABLE, the application can pass the structure's address to the WNetAddConnection2 function. The following example illustrates an application-defined function (EnumerateFunc) that enumerates all the resources on a network. When calling this function, specify NULL for the pointer to the NETRESOURCE structure. When WNetOpenEnum receives the NULL pointer, it retrieves a handle to the root of the network. Whenever a NETRESOURCE structure retrieved by WNetEnumResource is RESOURCEUSAGE_CONTAINER, the EnumerateFunc function calls itself and uses a pointer to that structure in its call to WNetOpenEnum. BOOL WINAPI EnumerateFunc(HWND hwnd, HDC hdc, LPNETRESOURCE lpnr) { DWORD dwResult, dwResultEnum; HANDLE hEnum; DWORD cbBuffer = 16384; // 16K is a good size DWORD cEntries = 0xFFFFFFFF; // enumerate all possible entries LPNETRESOURCE lpnrLocal; // pointer to enumerated structures DWORD i; dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, // enumerate all resources lpnr, // NULL first time this function is called &hEnum); // handle to resource if (dwResult != NO_ERROR) { // An application-defined error handler is demonstrated in the // section titled "Retrieving Network Errors." NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetOpenEnum"); return FALSE; } do { // Allocate memory for NETRESOURCE structures. lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer); dwResultEnum = WNetEnumResource(hEnum, // resource handle ¢ries, // defined locally as 0xFFFFFFFF lpnrLocal, // LPNETRESOURCE &cbBuffer); // buffer size if (dwResultEnum == NO_ERROR) { for(i = 0; i < cEntries; i++) { // Following is an application-defined function for // displaying contents of NETRESOURCE structures. DisplayStruct(hdc, &lpnrLocal[i]); // // If this NETRESOURCE is a container, call the function // recursively. if(RESOURCEUSAGE_CONTAINER == (lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONTAINER)) if(!EnumerateFunc(hwnd, hdc, &lpnrLocal[i])) TextOut(hdc, 10, 10, "EnumerateFunc returned FALSE.", 29); } } else if (dwResultEnum != ERROR_NO_MORE_ITEMS) { NetErrorHandler(hwnd, dwResultEnum, (LPSTR)"WNetEnumResource"); break; } } while(dwResultEnum != ERROR_NO_MORE_ITEMS); GlobalFree((HGLOBAL) lpnrLocal); dwResult = WNetCloseEnum(hEnum); if(dwResult != NO_ERROR) { NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetCloseEnum"); return FALSE; } return TRUE; }
赵老师啊,我有完整的源码,今天早上都测试一个早上了,但是没办法枚举啊,每次都返回ERROR。
SureGOGOGO 2016-09-07
  • 打赏
  • 举报
回复
引用 2 楼 oyljerry 的回复:

BOOL   EnumLanRes(PACK_HANDLE   *hPack,   NETRESOURCE   *pNetContainer,   DWORD   dwScope,   char   *svSpacer)   
  {   
  DWORD   OpenRet;   
    
  //   Open   network   resource   list   
  HANDLE   hNet;   
  OpenRet=WNetOpenEnum(dwScope,RESOURCETYPE_ANY,0,pNetContainer,&hNet);   
  if   (OpenRet!=NO_ERROR)   
  {   
  pfReply(hPack,CMD_ERROR,0,"LAN   net   open   failed");   
  sprintf(g_szError,"WNetOpenEnum   failed,[%d]",OpenRet);   
  return   FALSE;   
  }   
    
  //   Enumerate   resources   
  int   ret;   
  DWORD   dwCount,dwBufSize;   
  NETRESOURCE   *pNetRes;   
  pNetRes=(NETRESOURCE   *)malloc(16384);   
  if(pNetRes==NULL)   
  {   
  WNetCloseEnum(hNet);   
  pfReply(hPack,CMD_ERROR,0,"ヤカウフヨ愷レエ豐サラ・);   
  return   FALSE;   
  }   
    
  dwCount=1;   
  dwBufSize=8192;   
  ret=WNetEnumResource(hNet,&dwCount,pNetRes,&dwBufSize);   
  while(ret!=ERROR_NO_MORE_ITEMS)   
  {   
  char   *svType,*svLocalName,*svRemoteName,*svComment;   
    
  char   svURLHead[MAX_PATH+1];   
  char   svURLFoot[MAX_PATH+1];   
  svURLHead[0]='\0';   
  svURLFoot[0]='\0';   
    
  int   iNetType;   
    
  switch(pNetRes->dwDisplayType)   
  {   
  case   RESOURCEDISPLAYTYPE_DOMAIN:   
  svType="DOMAIN";   
  iNetType   =   RESOURCE_DOMAIN;   
  break;   
  case   RESOURCEDISPLAYTYPE_GENERIC:   
  svType="GENERIC";   
  iNetType   =   RESOURCE_GENERIC;   
  break;   
  case   RESOURCEDISPLAYTYPE_SERVER:   
  svType="SERVER";   
  iNetType   =   RESOURCE_SERVER;   
  break;   
  case   RESOURCEDISPLAYTYPE_SHARE:   
  switch(pNetRes->dwType)   
  {   
  case   RESOURCETYPE_DISK:   
  svType="FOLDER";   
  iNetType   =   RESOURCE_FOLDER;   
  break;   
  case   RESOURCETYPE_PRINT:   
  svType="PRINTER";   
  iNetType   =   RESOURCE_PRINTER;   
  break;   
  default:   
  svType="UNKNOWN";   
  iNetType   =   RESOURCE_UNKNOWN;   
  break;   
  }   
  break;   
  default:   
  svType="NETWORK";   
  iNetType   =   RESOURCE_NETWORK;   
  break;   
  }   
    
  if(pNetRes->lpLocalName==NULL)   svLocalName="";   
  else   svLocalName=pNetRes->lpLocalName;   
    
  if(pNetRes->lpRemoteName==NULL)   svRemoteName="";   
  else   svRemoteName=pNetRes->lpRemoteName;   
    
  if(pNetRes->lpComment==NULL)   svComment="";   
  else   svComment=pNetRes->lpComment;   
    
  _network_resource   res;   
  memset(&res,0,sizeof(res));   
    
  if(!pNetRes->lpLocalName   &&   !pNetRes->lpRemoteName)   
  {   
  res.iType   =   iNetType;   
  wsprintf(res.Comment,"%.99s",svComment);   
  }   
  else   
  {   
  res.iType   =   iNetType;   
  wsprintf(res.Comment,"%.99s",svComment);   
  wsprintf(res.RemoteName,"%.99s",svRemoteName);   
  wsprintf(res.LocalName,"%.99s",svLocalName);   
  }   
    
  pfReply(hPack,CMD_NETWORK,SUBCMD_RESOURCE,&res);   
    
  //   Recurse   if   necessary   
  if   (pNetRes->dwUsage   &   RESOURCEUSAGE_CONTAINER   &&   dwScope   ==   RESOURCE_GLOBALNET)   
  {   
  char   svSpacer2[100];   
  lstrcpyn(svSpacer2,svSpacer,100);   
  if(lstrlen(svSpacer2)<98)   lstrcat(svSpacer2,"     ");   
  EnumLanRes(hPack,pNetRes,dwScope,svSpacer2);   
  }   
    
  dwCount=1;   
  dwBufSize=16384;   
  ret=WNetEnumResource(hNet,&dwCount,pNetRes,&dwBufSize);   
  }   
  free(pNetRes);   
  WNetCloseEnum(hNet);   
  return   TRUE;   
  }   
我试试吧,都快奔溃了,搞了两天
SureGOGOGO 2016-09-07
  • 打赏
  • 举报
回复
引用 1 楼 VisualEleven 的回复:
WNetOpenEnum WNetEnumResource WNetCloseEnum
不行啊,今天查看了下MSDN,连主机名都得不到。。。,而且我需要对指定的主机获取共享目录.
赵4老师 2016-09-07
  • 打赏
  • 举报
回复
Enumerating Network Resources Windows NT Only To enumerate a network container resource, your application should pass the address of a NETRESOURCE structure to the WNetOpenEnum function. WNetOpenEnum creates a handle to the resource described by the NETRESOURCE structure. The application then passes this handle to the WNetEnumResource function, which returns information about the resource in the form of an array of NETRESOURCE structures. When the handle is no longer needed, the application can close it by calling the WNetCloseEnum function. Your application can continue enumerating any container resource described in the array of NETRESOURCE structures retrieved by WNetEnumResource. If the dwUsage member of the NETRESOURCE structure is RESOURCEUSAGE_CONTAINER, the application can pass the address of that structure to WNetOpenEnum to open the container and continue the enumeration. If dwUsage is RESOURCEUSAGE_CONNECTABLE, the application can pass the structure's address to the WNetAddConnection2 function. The following example illustrates an application-defined function (EnumerateFunc) that enumerates all the resources on a network. When calling this function, specify NULL for the pointer to the NETRESOURCE structure. When WNetOpenEnum receives the NULL pointer, it retrieves a handle to the root of the network. Whenever a NETRESOURCE structure retrieved by WNetEnumResource is RESOURCEUSAGE_CONTAINER, the EnumerateFunc function calls itself and uses a pointer to that structure in its call to WNetOpenEnum. BOOL WINAPI EnumerateFunc(HWND hwnd, HDC hdc, LPNETRESOURCE lpnr) { DWORD dwResult, dwResultEnum; HANDLE hEnum; DWORD cbBuffer = 16384; // 16K is a good size DWORD cEntries = 0xFFFFFFFF; // enumerate all possible entries LPNETRESOURCE lpnrLocal; // pointer to enumerated structures DWORD i; dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, // enumerate all resources lpnr, // NULL first time this function is called &hEnum); // handle to resource if (dwResult != NO_ERROR) { // An application-defined error handler is demonstrated in the // section titled "Retrieving Network Errors." NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetOpenEnum"); return FALSE; } do { // Allocate memory for NETRESOURCE structures. lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer); dwResultEnum = WNetEnumResource(hEnum, // resource handle ¢ries, // defined locally as 0xFFFFFFFF lpnrLocal, // LPNETRESOURCE &cbBuffer); // buffer size if (dwResultEnum == NO_ERROR) { for(i = 0; i < cEntries; i++) { // Following is an application-defined function for // displaying contents of NETRESOURCE structures. DisplayStruct(hdc, &lpnrLocal[i]); // // If this NETRESOURCE is a container, call the function // recursively. if(RESOURCEUSAGE_CONTAINER == (lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONTAINER)) if(!EnumerateFunc(hwnd, hdc, &lpnrLocal[i])) TextOut(hdc, 10, 10, "EnumerateFunc returned FALSE.", 29); } } else if (dwResultEnum != ERROR_NO_MORE_ITEMS) { NetErrorHandler(hwnd, dwResultEnum, (LPSTR)"WNetEnumResource"); break; } } while(dwResultEnum != ERROR_NO_MORE_ITEMS); GlobalFree((HGLOBAL) lpnrLocal); dwResult = WNetCloseEnum(hEnum); if(dwResult != NO_ERROR) { NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetCloseEnum"); return FALSE; } return TRUE; }
SureGOGOGO 2016-09-07
  • 打赏
  • 举报
回复
引用 13 楼 zhao4zhong1 的回复:
WinExec("cmd /c net view VIRTUAL>netviewoutput.txt",SW_HIDE);
//然后读文件netviewoutput.txt的内容
谢谢赵老师,也只能这样了。。。都搞了好久
赵4老师 2016-09-07
  • 打赏
  • 举报
回复
WinExec("cmd /c net view VIRTUAL>netviewoutput.txt",SW_HIDE);
//然后读文件netviewoutput.txt的内容
SureGOGOGO 2016-09-07
  • 打赏
  • 举报
回复
引用 11 楼 zhao4zhong1 的回复:
C:\>net help view
此命令的语法是:


NET VIEW
[\\computername [/CACHE] | /DOMAIN[:domainname]]
NET VIEW /NETWORK:NW [\\computername]

NET VIEW 用于显示一个计算机上共享资源的列表。当不带选项使用本命令时,它就会显示
当前域或网络上的计算机上的列表。

\\computername 指用户希望浏览其共享资源的计算机。
/DOMAIN:domainname 指定用户希望浏览有效的计算机所在的域。如果省略了域名,
就会显示局域网络上的所有域。
/NETWORK:NW 显示 NetWare 网络上所有可用的服务器。如果指定了一个计算
机名,就会显示 NetWare 网络中那个计算机上的可用资源。
/CACHE 显示指定计算机上的脱机客户资源缓存设置


net view 是可用显示的
oyljerry 2016-09-06
  • 打赏
  • 举报
回复

BOOL   EnumLanRes(PACK_HANDLE   *hPack,   NETRESOURCE   *pNetContainer,   DWORD   dwScope,   char   *svSpacer)   
  {   
  DWORD   OpenRet;   
    
  //   Open   network   resource   list   
  HANDLE   hNet;   
  OpenRet=WNetOpenEnum(dwScope,RESOURCETYPE_ANY,0,pNetContainer,&hNet);   
  if   (OpenRet!=NO_ERROR)   
  {   
  pfReply(hPack,CMD_ERROR,0,"LAN   net   open   failed");   
  sprintf(g_szError,"WNetOpenEnum   failed,[%d]",OpenRet);   
  return   FALSE;   
  }   
    
  //   Enumerate   resources   
  int   ret;   
  DWORD   dwCount,dwBufSize;   
  NETRESOURCE   *pNetRes;   
  pNetRes=(NETRESOURCE   *)malloc(16384);   
  if(pNetRes==NULL)   
  {   
  WNetCloseEnum(hNet);   
  pfReply(hPack,CMD_ERROR,0,"ヤカウフヨ愷レエ豐サラ・);   
  return   FALSE;   
  }   
    
  dwCount=1;   
  dwBufSize=8192;   
  ret=WNetEnumResource(hNet,&dwCount,pNetRes,&dwBufSize);   
  while(ret!=ERROR_NO_MORE_ITEMS)   
  {   
  char   *svType,*svLocalName,*svRemoteName,*svComment;   
    
  char   svURLHead[MAX_PATH+1];   
  char   svURLFoot[MAX_PATH+1];   
  svURLHead[0]='\0';   
  svURLFoot[0]='\0';   
    
  int   iNetType;   
    
  switch(pNetRes->dwDisplayType)   
  {   
  case   RESOURCEDISPLAYTYPE_DOMAIN:   
  svType="DOMAIN";   
  iNetType   =   RESOURCE_DOMAIN;   
  break;   
  case   RESOURCEDISPLAYTYPE_GENERIC:   
  svType="GENERIC";   
  iNetType   =   RESOURCE_GENERIC;   
  break;   
  case   RESOURCEDISPLAYTYPE_SERVER:   
  svType="SERVER";   
  iNetType   =   RESOURCE_SERVER;   
  break;   
  case   RESOURCEDISPLAYTYPE_SHARE:   
  switch(pNetRes->dwType)   
  {   
  case   RESOURCETYPE_DISK:   
  svType="FOLDER";   
  iNetType   =   RESOURCE_FOLDER;   
  break;   
  case   RESOURCETYPE_PRINT:   
  svType="PRINTER";   
  iNetType   =   RESOURCE_PRINTER;   
  break;   
  default:   
  svType="UNKNOWN";   
  iNetType   =   RESOURCE_UNKNOWN;   
  break;   
  }   
  break;   
  default:   
  svType="NETWORK";   
  iNetType   =   RESOURCE_NETWORK;   
  break;   
  }   
    
  if(pNetRes->lpLocalName==NULL)   svLocalName="";   
  else   svLocalName=pNetRes->lpLocalName;   
    
  if(pNetRes->lpRemoteName==NULL)   svRemoteName="";   
  else   svRemoteName=pNetRes->lpRemoteName;   
    
  if(pNetRes->lpComment==NULL)   svComment="";   
  else   svComment=pNetRes->lpComment;   
    
  _network_resource   res;   
  memset(&res,0,sizeof(res));   
    
  if(!pNetRes->lpLocalName   &&   !pNetRes->lpRemoteName)   
  {   
  res.iType   =   iNetType;   
  wsprintf(res.Comment,"%.99s",svComment);   
  }   
  else   
  {   
  res.iType   =   iNetType;   
  wsprintf(res.Comment,"%.99s",svComment);   
  wsprintf(res.RemoteName,"%.99s",svRemoteName);   
  wsprintf(res.LocalName,"%.99s",svLocalName);   
  }   
    
  pfReply(hPack,CMD_NETWORK,SUBCMD_RESOURCE,&res);   
    
  //   Recurse   if   necessary   
  if   (pNetRes->dwUsage   &   RESOURCEUSAGE_CONTAINER   &&   dwScope   ==   RESOURCE_GLOBALNET)   
  {   
  char   svSpacer2[100];   
  lstrcpyn(svSpacer2,svSpacer,100);   
  if(lstrlen(svSpacer2)<98)   lstrcat(svSpacer2,"     ");   
  EnumLanRes(hPack,pNetRes,dwScope,svSpacer2);   
  }   
    
  dwCount=1;   
  dwBufSize=16384;   
  ret=WNetEnumResource(hNet,&dwCount,pNetRes,&dwBufSize);   
  }   
  free(pNetRes);   
  WNetCloseEnum(hNet);   
  return   TRUE;   
  }   
Eleven 2016-09-06
  • 打赏
  • 举报
回复
WNetOpenEnum WNetEnumResource WNetCloseEnum

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧