CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Environment: CNetService was developed for Microsoft.
Windows NT. platforms using Microsoft. VisualC++ 6.0

The Microsoft. Win32. API provides Windows networking (WNet)
functions, providing networking capabilities for applications. The
WNet functions are network independent, enabling an application
to query and control network connections directly without knowledge of
the underlying network transports.

CNetService is a C++ class which wraps
methods around each of the WNet functions, providing an
object-oriented interface to these functions, grouping common variables
and structures in a single class object. This allows focusing on the
network project rather than the structures required by WNet.

The following WNet functions are used in CNetService:













The CNetService class can be extended
to provide access to specific keys and values, and to
enhace its capabilities. Refer to the Microsoft Platform
SDK: Windows Networking (WNet)
documentation from the MSDN (

CNetService contains the following methods,
fully documented in the download file:

    Method Function
    Constructor Initializes the class.
    AddConnection Creates the specified network connection.
    CloseEnum Ends a network resource enumeration.
    EnumerateNext Continues an enumeration of network resources.
    EnumerateResource Continues an enumeration of resources.
    Error Value in GetLastError.
    ErrorMessage Message associated with GetLastError.
    GetNetInfo Get a pointer to NETINFOSTRUCT.
    GetNetStatus Get the network status.
    GetNetType Get the network type code.
    GetNetInfoPrinters Bit vector of valid print devices.
    GetNetInfoDrives Bit vector of valid disk devices.
    GetProviderName Returns the network provider name.
    GetUNC Retrieves the UNC representing a network share.
    NetConnect Creates the specified network connection.
    NetDisconnect Disconnect from the specified network connection.
    NetError Value of WNetGetLastError.
    NetErrorMessage Message associated with WNetGetLastError.
    NetErrorProvider Name of provider returning WNetGetLastError.
    NetUser Returns the connection owner name (account).
    NetworkInformation Information about a specific network transport.
    OpenEnum Starts an enumeration of network resources.
    OSVersion Version of the operating system currently running.
    ResourceDisplayType ResourceDisplayType from NETRESOURCE
    Result Result of last operation.
    ServiceError Value in ServiceError.
    ServiceErrorMessage Message associated with ServiceError.
    UNCRemotePath Return the remote share path (see GetUNC).
    UNCRemoteServer Return the remote share name (see GetUNC).
    UniversalName Return the complete share name (see GetUNC).

Installing the Class

To use this class,

  1. Copy CNetService.cpp and CNetService.h to the same directory as
    your project or class libraries;
  2. Include CNetService.cpp and CNetService.h in your project;
  3. Add CNetService.h to any modules which make reference to
    CNetService or one of its’ methods or variables.
    This will normally take the form of

      #include "CNetService.h"

    in the header files in which the CNetService object
    is declared.

  4. Include mpr.lib in the linker. In VisualC++,
    this is accomplished by adding mpr.lib to your
    project settings dialog, link tab, object/library modules

Sample Application

A VisualC++ 6 workspace containing several sample projects is included
with the CNetService download:


    A Windows console program which connects a local drive to
    a network resource.


    A Windows console program which enumerates current network
    resources to a console window.


    A Windows dialog which enumerates current network resources in
    a graphical manner.


Click here for larger image

WNetEnum is a Microsoft. Windows NT. program to enumerate
current network resources in a graphical manner.


  • Enumerates current network servers, workstations and resources.
  • VisualC++ project and source code.
  • Based on CNetService class.


Download demo projects – 173 Kb

Download source – 48 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read