Windows Access Control List (ACL) Example 30

 

 

 

 

Log on a user to a machine Program Example

 

The following program example demonstrates how to log on a user to a machine programmatically.  It uses the LogonUser() function.

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

 

Log on a user to a machine:  Creating new Win32 C++ project

 

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

 

Log on a user to a machine Program Example: Adding the C++ source file

 

Next, add the following source code.

 

// Log a user on to the local computer. This computer is logged on as Mike spoon,

// a user with Administrators group, then this program try to log

// on a restricted user named usergedik another valid user account

// created in the same machine

#include <windows.h>

#include <stdio.h>

 

int main(int argc, WCHAR **argv)

{

      // usergedik is just a restrictive user created

      // in the XP machine that runs this program

      LPTSTR lpszUsername = Lusergedik;

      // Local account database

      LPTSTR lpszDomain = L.;

      LPTSTR lpszPassword = L123;

      DWORD dwLogonType = LOGON32_LOGON_INTERACTIVE;

      DWORD dwLogonProvider = LOGON32_PROVIDER_DEFAULT;

      HANDLE hToken;

     

      if(LogonUser(

            lpszUsername,     // Username

            lpszDomain,       // Domain or server where the Username is reside

            lpszPassword,     // Plaintext password

            dwLogonType,      // Type of logon

            dwLogonProvider,  // The logon provider

            &hToken                 // Pointer to handle that received the token

            ))

           

            wprintf(LWell, \%s\ user logged on to this machine successfully!\n, lpszUsername);

      else

      {

            wprintf(L%s failed to log on to this machine! error %u\n, lpszUsername, GetLastError());

            exit(-1);

      }

     

      ////////////////

      // TODO: Use the logon token for the desired tasks

      /////////////////

 

      if(CloseHandle(hToken) != 0)

            wprintf(LThe handle that received the token has been closed.\n);

      else

            wprintf(LSomething wrong, the handle cannot be closed! error: %u\n, GetLastError());

 

      return 0;

}

 

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

 

Log on a user to a machine Program Example: A sample console output

 

 

 

< Windows ACL Example 29 | Windows Access Control List (ACL) Main | Win32 Programming | Windows ACL Example 31 >