The Win32 Network Management APIs 45

 

 

 

 

 

NetSessionDel() Program Example

 

This function ends a network session between a server and a workstation. Only members of the Administrators or Server Operators local group can successfully execute the NetSessionDel() 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 session functions. The following code sample demonstrates how to terminate a session between a server and a workstation using a call to the NetSessionDel() function.

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

 

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

 

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

 

NetSessionDel() Program Example: Adding new C++ source file for C++ source code

 

Then, add the following source code.

 

#ifndef UNICODE

#define UNICODE

#endif

 

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

 

#include <stdio.h>

#include <windows.h>

#include <lm.h>

 

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

{

   DWORD dwError = 0;

   LPTSTR pszServerName = NULL;

   LPTSTR pszClientName = NULL;

   LPTSTR pszUserName = NULL;

   NET_API_STATUS nStatus;

 

   // Check command line arguments.

   if (argc > 4 || argc == 1)

   {

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

      wprintf(L"Example: %s \\\\ServerGedik \\\\Gedikclient Mikespoon\n", argv[0]);

      exit(1);

   }

 

   if (argc >= 2)

      pszServerName = argv[1];

   if (argc >= 3)

      pszClientName = argv[2];

   if (argc == 4)

      pszUserName = argv[3];

 

   // Call the NetSessionDel() function to delete the session.

   nStatus = NetSessionDel(pszServerName,pszClientName,pszUserName);

   // Display the result of the call.

   if (nStatus == NERR_Success)

   {

         wprintf(L"NetSessionDel() is OK!\n");

         fwprintf_s(stderr, L"The specified session(s) has been successfully deleted\n");

   }

   else

   {

         wprintf(L"NetSessionDel() failed!\n");

         fwprintf_s(stderr, L"A system error has occurred: %d\n", nStatus);

   }

 

   return 0;

}

 

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

 

NetSessionDel() Program Example: A sample console program output

 

 

 

 

< Win32 Network Management APIs 44 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 46 >