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.

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

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, LUsage: %s [\\\\ServerName] SessionTimeOut\n, argv[0]);
wprintf(LExample: %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(LNetWkstaGetInfo() failed!\n);
fwprintf_s(stderr, LA system error has occurred (NetWkstaGetInfo): %d\n, nStatus);
return -1;
}
else
wprintf(LNetWkstaGetInfo() 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, LInvalid 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(LNetWkstaSetInfo() is pretty fine!\n);
fwprintf_s(stderr, LWorkstation information reset: session time-out = %d\n, _wtoi(argv[argc-1]));
}
else
fwprintf_s(stderr, LNetWkstaSetInfo() failed! A system error %d has occurred\n, nStatus);
return 0;
}
Build and run the project. The following screenshot is a sample output.
