The Win32 Network Management APIs 40






NetShareCheck() Program Example


This function checks whether or not a server is sharing a device. This function applies only to Server Message Block (SMB) shares. For other types of shares, such as Distributed File System (DFS) or WebDAV shares, use Windows Networking (WNet) functions, which support all types of shares. No special group membership is required to successfully execute the NetShareCheck() function. If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling the network management share functions. The following code sample demonstrates how to check whether a server is sharing a device, using a call to the NetShareCheck() function. The function returns the type of device being shared, as described in the preceding documentation for the type parameter.

Create a new empty Win32 console application project. Give the project name and change the project location is needed.


NetShareCheck() Program Example: Creating new Win32 console C++ application project


Then, add the source file and give it a suitable name.


NetShareCheck() Program Example: Adding new C++ source file


Then, add the following source code.


#include <windows.h>

#include <stdio.h>

#include <lm.h>


#pragma comment(lib, "Netapi32.lib")


void wmain(int argc, WCHAR *argv[])



   DWORD devType = 0;




      wprintf(L"Usage: %s ServerName SharedDevice\n", argv[0]);

      wprintf(L"Example: %s ServKambing MyTestSharedFolder\n", argv[0]);




      // Call the NetShareCheck() function.

      // Use _wcsupr_s() to convert a string to uppercase,

      //   using the current locale or a specified locale passed in.

      // Device types:

      // STYPE_DISKTREE - Disk drive.

      // STYPE_PRINTQ - Print queue.

      // STYPE_DEVICE - Communication device.

      // STYPE_IPC - Interprocess communication (IPC).

      res = NetShareCheck(argv[1],argv[2], &devType);

      // If the function succeeds, inform the user.



              wprintf(L"NetShareCheck() looks OK!\n");

              wprintf(L"Device is shared as type %u.\n", devType);



                    case STYPE_DISKTREE:

                          wprintf(L"It is Disk drive.\n");


                    case STYPE_PRINTQ:

                          wprintf(L"It is Print queue.\n");


                    case STYPE_DEVICE:

                          wprintf(L"It is Communication device.\n");


                    case STYPE_IPC:

                          wprintf(L"It is Interprocess communication (IPC).\n");



                          wprintf(L"Invalid device!\n");



        // Otherwise, print the error.



              wprintf(L"NetShareCheck() failed! Error %u\n", res);






Build and run the project. The following screenshot is a sample output.


NetShareCheck() Program Example: A sample console program output





< Win32 Network Management APIs 39 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 41 >