NetShareAdd '& NetShareDel Function Calls | CodeGuru

NetShareAdd ‘& NetShareDel Function Calls

Environment: Program can be compiled with VC++6.0 under Win98 or Win2k and run on both platforms (not Unicode of course). One problem of using NetShareAdd & NetShareDel (and other LAN Manager functions) in Windows applications lies in "Differences in Win32 API Implementations Among Windows Operating Systems" as Noel Nyman explain in MSDN Visual Studio 6.0 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 28, 2002
3 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: Program can be compiled with VC++6.0 under Win98 or Win2k and run on both platforms (not Unicode of course).

One problem of using NetShareAdd & NetShareDel (and other LAN Manager functions) in
Windows applications lies in "Differences in Win32 API Implementations Among
Windows Operating Systems" as Noel Nyman explain in MSDN Visual Studio 6.0
BackgroundersWindows PlatformGuidelinesDifferences in Win32 API Implementations
Among Windows Operating Systems. Article Q125700 from Microsoft Knowledge Base "
Windows 95 Support for Net Function Calls" make the information more exact: Windows
95 support for these functions differs from Windows NT in two ways. First, because
Windows 95 doesn’t support Unicode, these functions require ANSI strings. Second,
Windows 95 exports the Net functions from SVRAPI.DLL instead of NETAPI32.DLL. That
source give more explanation:

To handle these differences, applications targeted to both Windows NT and Windows 95
should do the following:

  1. Avoid importing Net functions from NETAPI32.DLL at link time. Instead, applications
    should do a run time version check and dynamically link to NETAPI32.DLL for Windows NT
    or SVRAPI.DLL for Windows 95.
  2. 2. Make sure the application doesn’t depend on the presence of unsupported API’s.
  3. 3. When calling Net functions, pass strings using a character set appropriate for
    the host operating system. Use Unicode strings for Windows NT and ANSI strings for
    Windows 95.

My class, CWiCNet, is the impementation of the above-mentioned guidelines.
But the troubles arise when you see (I spend few day before understand
cause of appearing "First chance exceptions.." message while call
functions in Win98) that same function requires different arguments among
different Windows operating systems. You can’t include both header file in a
project simultaneously because this invoke compilation error. Since constants defined
in SVRAPI.h (for win 9x) and lmshare.h (for win2k) are equal, I don’t redifine them
and just move some structure definitions from SVRAPI.h to my files and include just
lmshare.h for functions prototypes.

The project have two main files: CWiCNet.h and CWiCNet.cpp (interface of the class
and realization of it accordingly). You may compile it under Win98 or Win2k
and it must work on both platforms. File CWiCNet.cpp does not use precompiled header.

So if you want to make some local resource shared you must create an object
of CWiCNet class and call its public function

NetShareAdd(
  const char* dir_path, // path to disk resource you want
                        // to be shared
  const char* net_name, // desired net name
  const char* comment = NULL, // comment on the resource
  bool bReadOnly = true, // set to false if you want to
                         // give full access to resource
  const char* password  = NULL ); // you may set password
                                  // for access to resource

If you want to delete a shared resource (I mean close access to the resource) you are able to do
this by calling a NetShareDel public member function of object of class CWiCNet. This function
requires only one parameter – resource name:

bool NetShareDel(
  const char* net_name // name of resource to be
                       // deleted from sharing
);

Additionally I include example project CWiCNetExample which explain the use of
CWiCNet class. Just one remark: if you want use "Share Del" menu item –
you must select local resource, only in this case the menu item will be enabled.
Another one note: The data in the network structure (in tree) is not updated automatically after an add or
del share, so you must "Enumerate resources" again if you want to see changes.
And: just be patient when resources enumerates, even if you see message "EnumerateFunc
returned FALSE"

Network resources enumeration code was given from MSDN Visual Studio 6.0
Platform SDKNetworking and Distributed ServisesWindows Networking (WNet)
Using Windows NetworkingEnumerating Network Resources.

Code for Browsing shell namespace by Selom Ofori (a.k.a SubRosa).

Get hostname and ip address of local computer by Jeff Lundgren & Jaroslav Pisk.

Downloads

Download demo project – 50.7 Kb
Download source – 4.14 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.