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
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

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.


Download demo project – 50.7 Kb
Download source – 4.14 Kb

More by Author

Must Read