Click here to Skip to main content
13,355,275 members (60,116 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 19 Apr 2004

Simple class to work with the NetShare*() APIs

, 19 Apr 2004
Rate this:
Please Sign up or sign in to vote.
Simple class to work with the NetShare*() APIs.

Sample Image - NTShares.jpg


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
  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().

  while (m_Shares.Next())
    // Do something with the instance fields...

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


To delete a share...

// Delete "ShareName" on "ComputerName"


Finally, to update a share

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



  • April 19 2004
    • Initial release.


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
Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralNice work Pin
micutzu26-Aug-05 1:52
membermicutzu26-Aug-05 1:52 
Questionhow to compile in VC6 Pin
KmAshif27-Apr-04 21:29
memberKmAshif27-Apr-04 21:29 
i want compile this project in could it posssible.
AnswerRe: how to compile in VC6 Pin
John Gonzalez4-May-04 8:41
memberJohn Gonzalez4-May-04 8:41 
AnswerRe: how to compile in VC6 Pin
John Gonzalez6-May-04 23:17
memberJohn Gonzalez6-May-04 23:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 20 Apr 2004
Article Copyright 2004 by John Gonzalez
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid