⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ntshares.cpp

📁 此源码为设置本地文件夹为局域网内的共享文件夹
💻 CPP
字号:
/*
CNTShares - A simple class to work with the NetShare*() API's. Works for ANSI & Unicode
Initial release 4/19/2004
Contact : jg@jgsoftware.com
Use the code for anything, just submit suggestions and bug fixes to above address.
*/

#include "stdafx.h"
#include "NTShares.h"

#pragma comment(lib,"NetApi32.lib")

// CNTShares

//****************************************************************************
// CNTShares::CNTShares - Constructor
//****************************************************************************

CNTShares::CNTShares()
{
  m_pShares = 0;
}

//****************************************************************************
// CNTShares::~CNTShares - Destructor
//****************************************************************************

CNTShares::~CNTShares()
{
  if (m_pShares) NetApiBufferFree(m_pShares);
  m_pShares = 0;
}

// CNTShares member functions

//****************************************************************************
// CNTShares::First - Initializes share enumeration to pszServer. Reads first
//                    share into instance fields and returns TRUE if there are
//                    more shares.
//****************************************************************************

void CNTShares::First(LPCTSTR pszServer)
{
  WCHAR szServer[128];
  DWORD dwTotalEntries;

  // Whether compiling in ANSI or Unicode, the Net* API's always expect Unicode. Convert if needed
#ifdef _UNICODE
  wcscpy(szServer,pszServer);
#else
  USES_CONVERSION;
  wcscpy(szServer,A2W(pszServer));
#endif

  m_dwEntriesReadIndex = -1;
  DWORD dwReturn = NetShareEnum(szServer,2,(LPBYTE*)&m_pShares,MAX_PREFERRED_LENGTH,&m_dwEntriesRead,&dwTotalEntries,0);
  if ((dwReturn != ERROR_SUCCESS) && (dwReturn != ERROR_MORE_DATA)) AfxThrowWin32Exception(dwReturn);
}

//****************************************************************************
// CNTShares::Next - Sets instance fields to next share. Returns TRUE if there
//                   are more shares
//****************************************************************************

BOOL CNTShares::Next()
{
  PSHARE_INFO_2 pInfo;
  
  m_dwEntriesReadIndex++;
  if (m_dwEntriesReadIndex == m_dwEntriesRead) return FALSE;
  
  pInfo = &m_pShares[m_dwEntriesReadIndex];

  // Whether compiling in ANSI or Unicode, the Net* API's always expect Unicode. Convert if needed
#ifdef _UNICODE
  m_sName   = pInfo->shi2_netname;
  m_sPath   = pInfo->shi2_path;
  m_sRemark = pInfo->shi2_remark;
#else
  USES_CONVERSION;
  m_sName   = W2A(pInfo->shi2_netname);
  m_sPath   = W2A(pInfo->shi2_path);
  m_sRemark = W2A(pInfo->shi2_remark);
#endif
  m_dwShareType          = pInfo->shi2_type & 0xF;
  m_fIsAdmin             = (pInfo->shi2_type & 0x80000000) > 0;
  m_nMaxConnections      = pInfo->shi2_max_uses;
  m_dwCurrentConnections = pInfo->shi2_current_uses;

  return TRUE;
}

#ifdef _DEBUG

//****************************************************************************
// CNTShares::Dump - Diagnostic
//****************************************************************************

void CNTShares::Dump(CDumpContext& dc) const
{
  CObject::Dump(dc);

  // TODO: Add your specialized code here and/or call the base class
}

#endif

//****************************************************************************
// CNTShares::Add - Adds new share called pszName on pszServer with parameters
//****************************************************************************

void CNTShares::Add(LPCTSTR pszServer,LPCTSTR pszName,LPCTSTR pszRemark,LPCTSTR pszPath,int nMaxConnections)
{
  SHARE_INFO_2 share;
  WCHAR        szServer[128];
  WCHAR        szName[255];
  WCHAR        szPath[255];
  WCHAR        szRemark[255];
  DWORD        dwResult;

  // Whether compiling in ANSI or Unicode, the Net* API's always expect Unicode. Convert if needed
#ifdef _UNICODE
  wcscpy(szServer,pszServer);
  wcscpy(szName,pszName);
  wcscpy(szPath,pszPath);
  wcscpy(szRemark,pszRemark);
#else
  USES_CONVERSION;
  wcscpy(szServer,A2W(pszServer));
  wcscpy(szName,A2W(pszName));
  wcscpy(szPath,A2W(pszPath));
  wcscpy(szRemark,A2W(pszRemark));
#endif

  ZeroMemory(&share,sizeof(SHARE_INFO_2));
  share.shi2_netname  = szName;
  share.shi2_path     = szPath;
  share.shi2_remark   = szRemark;
  share.shi2_type     = STYPE_DISKTREE;
  share.shi2_max_uses = nMaxConnections;

  dwResult = NetShareAdd(szServer,2,(LPBYTE)&share,0);
  if (dwResult != ERROR_SUCCESS) AfxThrowWin32Exception(dwResult);

  // Set fields to new share info
  m_sName           = pszName;
  m_sPath           = pszPath;
  m_sRemark         = pszRemark;
  m_nMaxConnections = nMaxConnections;
  m_dwShareType     = STYPE_DISKTREE;
}

//****************************************************************************
// CNTShares::Delete - Deletes share pszName on pszServer
//****************************************************************************

void CNTShares::Delete(LPCTSTR pszServer,LPCTSTR pszName)
{
  WCHAR szServer[128];
  WCHAR szName[255];
  DWORD dwResult;

  // Whether compiling in ANSI or Unicode, the Net* API's always expect Unicode. Convert if needed
#ifdef _UNICODE
  wcscpy(szServer,pszServer);
  wcscpy(szName,pszName);
#else
  USES_CONVERSION;
  wcscpy(szServer,A2W(pszServer));
  wcscpy(szName,A2W(pszName));
#endif

  dwResult = NetShareDel(szServer,szName,0);
  if (dwResult != ERROR_SUCCESS) AfxThrowWin32Exception(dwResult);
}

//****************************************************************************
// CNTShares::Update - Update the share pszName on server pszServer with new
//                     configuration
//****************************************************************************

void CNTShares::Update(LPCTSTR pszServer,LPCTSTR pszName,LPCTSTR pszRemark,LPCTSTR pszPath,int nMaxConnections)
{
  SHARE_INFO_2 share;
  WCHAR        szServer[128];
  WCHAR        szName[255];
  WCHAR        szPath[255];
  WCHAR        szRemark[255];
  DWORD        dwResult;

  // Whether compiling in ANSI or Unicode, the Net* API's always expect Unicode. Convert if needed
#ifdef _UNICODE
  wcscpy(szServer,pszServer);
  wcscpy(szName,pszName);
  wcscpy(szPath,pszPath);
  wcscpy(szRemark,pszRemark);
#else
  USES_CONVERSION;
  wcscpy(szServer,A2W(pszServer));
  wcscpy(szName,A2W(pszName));
  wcscpy(szPath,A2W(pszPath));
  wcscpy(szRemark,A2W(pszRemark));
#endif

  ZeroMemory(&share,sizeof(SHARE_INFO_2));
  share.shi2_netname  = szName;
  share.shi2_path     = szPath;
  share.shi2_remark   = szRemark;
  share.shi2_type     = STYPE_DISKTREE;
  share.shi2_max_uses = nMaxConnections;

  dwResult = NetShareSetInfo(szServer,szName,2,(LPBYTE)&share,0);
  if (dwResult != ERROR_SUCCESS) AfxThrowWin32Exception(dwResult);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -