The Win32 Network Management APIs 25

 

 

 

 

 

NetWkstaSetInfo() Program Example

 

The NetWkstaSetInfo() function configures a workstation with information that remains in effect after the system has been reinitialized. The following code sample demonstrates how to set the session time-out value associated with a workstation using a call to the NetServerSetInfo() function. (The session time-out is the number of seconds the server waits before disconnecting an inactive session.) The code specifies information level 502 (WKSTA_INFO_502).

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

 

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

 

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

 

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

 

Then, add the following source code.

 

/* Unicode setting if needed

#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[])

{

   LPWKSTA_INFO_502 pBuf = NULL;

   WKSTA_INFO_502 wi;

   DWORD dwLevel = 502;

   NET_API_STATUS nStatus;

   LPWSTR pszServerName = NULL;

 

   if ((argc < 2) || (argc > 3))

   {

      fwprintf_s(stderr, L"Usage: %s [\\\\ServerName] SessionTimeOut\n", argv[0]);

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

      exit(1);

   }

 

   if (argc == 3)

      pszServerName = argv[1];

 

   // Retrieve the current settings.

   nStatus = NetWkstaGetInfo(pszServerName,dwLevel,(LPBYTE *)&pBuf);

 

   if (nStatus != NERR_Success)

   {

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

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

         return -1;

   }

   else

         wprintf(L"NetWkstaGetInfo() looks fine!\n");

 

   if (pBuf != NULL)

   {

      // Copy the existing settings to the new structure and free the buffer.

      CopyMemory(&wi, pBuf, sizeof(wi));

      NetApiBufferFree(pBuf);

   }

   else

   {

      fwprintf_s(stderr, L"Invalid memory lol!\n");

      return -1;

   }

 

   // Set a new session time-out value by calling the NetWkstaSetInfo function.

   wi.wki502_sess_timeout = _wtoi(argv[argc-1]);

   nStatus = NetWkstaSetInfo(pszServerName,dwLevel,(LPBYTE)&wi,NULL);

 

   // Display the result of the call.

   if (nStatus == NERR_Success)

   {

         wprintf(L"NetWkstaSetInfo() is pretty fine!\n");

         fwprintf_s(stderr, L"Workstation information reset: session time-out = %d\n", _wtoi(argv[argc-1]));

   }

   else

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

 

   return 0;

}

 

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

 

NetWkstaSetInfo() Program Example: A sample console program output

 

 

 

 

< Win32 Network Management APIs 24 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 26 >