Click here to Skip to main content
6,292,811 members and growing! (11,197 online)
Email Password   helpLost your password?
General Programming » Internet / Network » General     Intermediate

Simple class to work with the NetShare*() APIs

By John Gonzalez

Simple class to work with the NetShare*() APIs.
VC6, VC7, VC7.1Win2K, WinXP, Win2003, MFC, VS.NET2003, Dev
Posted:19 Apr 2004
Views:23,189
Bookmarked:8 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
5 votes for this article.
Popularity: 3.30 Rating: 4.73 out of 5

1

2

3
2 votes, 40.0%
4
3 votes, 60.0%
5

Sample Image - NTShares.jpg

Introduction

Working with the Net*() NT APIs can be a pain. Having experienced this pain a while back, I decided to take some code I had and make it into an article. This is just a simple class to perform just the basics: enumerating, adding, deleting and updating shares on a local or remote computer. It can be expanded further to get and set security configuration. Enterprise level development would likely use active directory to manipulate shares, this class works with NT4 style domains and workgroups as well as Win2000 networks not using active directory.

Using the code

Here's part of the class. The public fields are where we retrieve share configuration after a First() / Next() call. All but one are documented in the SDK. The m_fIsAdmin is not there. It just means that the share was created by the system, such as the hidden shares created for fixed drives.

class CNTShares : public CObject
{
...Privates omitted
public:
  CString m_sName;
  CString m_sRemark;
  CString m_sPath;
  DWORD   m_dwShareType;
  DWORD   m_dwCurrentConnections;
  int     m_nMaxConnections;
  BOOL    m_fIsAdmin;
  
  void  First(LPCTSTR pszServer);
  BOOL  Next();
  void  Add(LPCTSTR pszServer,LPCTSTR pszName,LPCTSTR pszRemark,
                              LPCTSTR pszPath,int nMaxConnections);
  void  Delete(LPCTSTR pszServer,LPCTSTR pszName);
  void  Update(LPCTSTR pszServer,LPCTSTR pszName,LPCTSTR pszRemark,
                              LPCTSTR pszPath,int nMaxConnections);
};

Note that all the classes' methods can throw a CWin32Exception, a CException derived class included in the zip file. Makes it easier to catch API style errors.

Use is simple, first instantiate an instance...

// Instantiate an instance

    
CNTShares m_Shares;

Then use the First() / Next() methods to enumerate shares. The machine name gets passed into the First() method. Note that the Next() method needs to be called after the First() method to actually know if there are any shares to enumerate or left for enumeration. Internally, the index is set to -1 in First(), and incremented in Next().

TRY
{
  m_Shares.First(sServer);
  while (m_Shares.Next())
  {
    // Do something with the instance fields...

  };
}
CATCH(CWin32Exception,e)
{
  e->ReportError();
  return;
}
END_CATCH

To add a share...

// Computer name can be an empty string for the local machine, path name is 

// local to the machine name, and -1 indicates that there is an unlimited # of 

// connections allowed


m_Share.Add(_T("ComputerName"),_T("ShareName"),_T("c:\Path"),_T("Remark"),-1);

To delete a share...

// Delete "ShareName" on "ComputerName"


m_Share.Delete(_T("ComputerName"),_T("ShareName"));

Finally, to update a share

// Update a share. "ShareName" must exist on "ComputerName"


m_Share.Update(_T("ComputerName"),_T("ShareName"),_T("c:\Path"),_T("Remark"),-1);

History

  • April 19 2004
    • Initial release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

John Gonzalez


Member

Occupation: Software Developer
Location: United States United States

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralNice work Pinmembermicutzu1:52 26 Aug '05  
Generalhow to compile in VC6 PinmemberKmAshif21:29 27 Apr '04  
GeneralRe: how to compile in VC6 PinmemberJohn Gonzalez8:41 4 May '04  
GeneralRe: how to compile in VC6 PinmemberJohn Gonzalez23:17 6 May '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Apr 2004
Editor: Smitha Vijayan
Copyright 2004 by John Gonzalez
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project