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\ Backgrounders\Windows Platform\Guidelines\Differences 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 SDK\Networking and Distributed Servises\Windows Networking (WNet)\ Using Windows Networking\Enumerating 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



Comments

  • Unsuccessful try to close share on Windows 98

    Posted by Legacy on 11/22/2003 12:00am

    Originally posted by: Ketan Vyas

    Hi 
    
    

    I tried to close win98 share from windows 2k but failed.

    After that i turned on remote administration on 98 machine and tried with NEtShareDel but again it failed.

    Any clue ?

    Ketan Vyas

    Reply
  • Read permissions doesn't set :(

    Posted by Legacy on 09/12/2003 12:00am

    Originally posted by: Andrew Potachits

    This sample doesn't work under Win2k SP4.
    It doesn't set Read permission to the share.
    Sad, but true...

    Reply
  • What shall i do if i want to close share on remote machine

    Posted by Legacy on 04/07/2003 12:00am

    Originally posted by: Ketan Vyas

    I want to close share on remote machine.

    I used NetShareDel, but it is giving me Error 2250. CONNECTION_NOT_FOUND error.

    Please let me know solution

    Reply
  • if you have Problem with class CWiCNet under Win98

    Posted by Legacy on 10/03/2002 12:00am

    Originally posted by: Dmitry

    For correct work under Windows 98
    Please correct shi50_info struct

    before
    si50.shi50_remark = const_cast<char*>(comment);

    after
    LPTSTR sRemark=(CHAR)0;
    si50.shi50_remark = (LPTSTR)&sRemark;

    otherwise in properties sharing will not be seen.

    Reply
  • problem under windows98

    Posted by Legacy on 01/25/2002 12:00am

    Originally posted by: jifeng li

    Use the sample to share a folder in the local machine under windows 98,seems like successful,but can't connect to the share from another computer in the local workgroup use the password I set.and when reboot the system,the share disappear,I found that the function NetshareAdd didn't write anthing to windows registry. what's the matter?

    Reply
  • A good job

    Posted by Legacy on 01/20/2002 12:00am

    Originally posted by: jifeng li

    The programe give me a timely help
    Thank you, Vasilenko Serhiy

    Reply
  • RC File Missing?

    Posted by Legacy on 01/02/2002 12:00am

    Originally posted by: Gord

    Demo won't build. Can't find the project's main RC file.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds