The Win32 Network Management APIs 47

 

 

 

 

 

NetSessionGetInfo() Program Example

 

This function retrieves information about a session established between a particular server and workstation. The following code sample demonstrates how to retrieve information about a session using a call to the NetSessionGetInfo() function. The sample calls NetSessionGetInfo(), specifying information level 10 (SESSION_INFO_10). If the call succeeds, the code prints information about the session. Finally, the sample frees the memory allocated for the information buffer.

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

 

NetSessionGetInfo() Program Example: Creating new Win32 C++ console application Visual C++ .NET project

 

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

 

NetSessionGetInfo() Program Example: Adding new C++ source file for the 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_t *argv[])

{

   DWORD dwLevel = 10;

   LPSESSION_INFO_10 pBuf = NULL;

   LPTSTR pszServerName = NULL;

   LPTSTR pszUNCClientName = NULL;

   LPTSTR pszUserName = NULL;

   NET_API_STATUS nStatus;

 

   // Check command line arguments.

   if (argc == 3)

   {

      pszUNCClientName = argv[1];

      pszUserName = argv[2];

   }

   else if (argc == 4)

   {

      pszServerName = argv[1];

      pszUNCClientName = argv[2];

      pszUserName = argv[3];

   }

   else

   {

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

      wprintf(L"Example: %s \\\\Serverbodo \\\\clientgedik Jane\n", argv[0]);

      exit(1);

   }

 

   // Call the NetSessionGetInfo() function, specifying level 10.

   nStatus = NetSessionGetInfo(pszServerName,pszUNCClientName,pszUserName,dwLevel,(LPBYTE *)&pBuf);

 

   // If the call succeeds,

   if (nStatus == NERR_Success)

   {

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

      if (pBuf != NULL)

      {

         // Print the session information.

         wprintf(L"\n\tClient: %s\n", pBuf->sesi10_cname);

         wprintf(L"\tUser:   %s\n", pBuf->sesi10_username);

         wprintf(L"\tActive: %d\n", pBuf->sesi10_time);

         wprintf(L"\tIdle:   %d\n", pBuf->sesi10_idle_time);

      }

   }

   // Otherwise, indicate a system error.

   else

   {

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

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

   }

   // Free the allocated memory.

   if (pBuf != NULL)

      NetApiBufferFree(pBuf);

 

   return 0;

}

 

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

 

NetSessionGetInfo() Program Example: A sample program output

 

 

 

 

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