home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / lanserv / mlogon32 / mlogon32.c < prev    next >
C/C++ Source or Header  |  1998-10-01  |  6KB  |  172 lines

  1. /****************************************************************************/
  2. /*
  3.  *    PROGRAM NAME: MLOGON32
  4.  *    ------------
  5.  *
  6.  *    What this program does: It logs on a user, waits for the SPACE key
  7.  *    to be pressed, and logs off the user. User name, password (if required)
  8.  *    and domain name are passed on the command line.
  9.  *
  10.  *    REQUIRED FILES:
  11.  *    ---------------
  12.  *    MLOGON.C        -  Source code for this program
  13.  *
  14.  *    REQUIRED LIBRARIES:
  15.  *    -------------------
  16.  *    UPM.LIB         -  UPM library (in IBMLAN\NETSRC\OS2\LIB directory)
  17.  *
  18.  *    HOW TO COMPILE THIS PROGRAM:
  19.  *    ----------------------------
  20.  *    icc /Gt+ /DPURE_32 /C mlogon.c
  21.  *
  22.  ****************************************************************************/
  23.  
  24. /*------- OS/2 include files -----------------------------------------------*/
  25. #define INCL_BASE
  26. #include <os2.h>
  27.  
  28. /*------- UPM include files ------------------------------------------------*/
  29. #include <upm.h>
  30.  
  31. /*------- C include files --------------------------------------------------*/
  32. #include <stdio.h>
  33. #include <string.h>
  34.  
  35. /****************************************************************************/
  36. /* MAIN C function                                                          */
  37. /*--------------------------------------------------------------------------*/
  38. VOID
  39. main(int argc, char *argv[])
  40. {
  41.     USHORT      usRc,
  42.                 usIndex;
  43.     HKBD        KbdHandle;
  44.     KBDKEYINFO  CharData;
  45.  
  46.     strupr(argv[0]);
  47.  
  48.     /* Validate command line input. */
  49.     if (argc < 3)
  50.     {
  51.         printf("Usage: %s <username> <password> <domainname>\n",
  52.                 strupr(argv[0]));
  53.         printf("                or\n");
  54.         printf("Usage: %s <username> <domainname>\n",
  55.                 strupr(argv[0]));
  56.         DosExit(EXIT_PROCESS, 0);
  57.     }
  58.  
  59.     printf("%s:  logon is in progress...\n", strupr(argv[0]));
  60.  
  61.     /* Call the api to log the user on. */
  62.     if (argc == 4)
  63.     {
  64.         strupr(argv[2]);                /* password must be UPPERCASE */
  65.  
  66.         usRc = u32eulgn(argv[1],                        /*  userid      */
  67.                         argv[2],                        /*  password    */
  68.                         argv[3],                        /*  remotename  */
  69.                         (unsigned short)UPM_DOMAIN,     /*  remotetype  */
  70.                         UPM_FL_DOMVER);                 /*  flags       */
  71.  
  72.         usIndex = 3;
  73.     }
  74.     else
  75.     {
  76.         usRc = u32eulgn(argv[1],                        /*  userid      */
  77.                         NULL,                           /*  password    */
  78.                         argv[2],                        /*  remotename  */
  79.                         (unsigned short)UPM_DOMAIN,     /*  remotetype  */
  80.                         UPM_FL_DOMVER);                 /*  flags       */
  81.  
  82.         usIndex = 2;
  83.     }
  84.  
  85.     if (usRc == UPM_OK)
  86.     {
  87.         printf("%s: \"%s\" is now logged on.\n",
  88.                 strupr(argv[0]), strupr(argv[1]));
  89.     }
  90.     else if (usRc == UPM_LOGGED)
  91.     {
  92.         printf("Logon could not complete.\n"
  93.                "The specified user may already be logged on to the specified domain.\n");
  94.         DosExit(EXIT_PROCESS, usRc);
  95.     }
  96.     else
  97.     {
  98.         printf("Error: u32eulgn() returned = %X\n",usRc);
  99.         DosExit(EXIT_PROCESS, usRc);
  100.     }
  101.  
  102.     /* Create a virtual keyboard. */
  103.     usRc = KbdOpen(&KbdHandle);
  104.  
  105.     if (usRc != 0)
  106.     {
  107.         printf("\nError: KbdOpen() returned = %d\n",usRc);
  108.     }
  109.     else
  110.     {
  111.         /* Set focus to the current session. */
  112.         usRc = KbdGetFocus (0,KbdHandle);
  113.         if (usRc != 0)
  114.         {
  115.             printf("\nError: KbdGetFocus() returned = %d\n",usRc);
  116.         }
  117.     }
  118.  
  119.     if (!usRc)
  120.     {
  121.         do
  122.         {
  123.             printf("\nTo log off, please press the spacebar.");
  124.             fflush(stdout);
  125.  
  126.             /* Read from the keyboard until the space bar is pressed. */
  127.             do
  128.             {
  129.                 usRc = KbdCharIn(&CharData, 0, KbdHandle);
  130.                 if (usRc != 0)
  131.                 {
  132.                     printf("\nError: KbdCharLn() returned = %d\n", usRc);
  133.                     break;
  134.                 }
  135.             } while (CharData.chChar != 0x20);
  136.  
  137.             if (!usRc)
  138.             {
  139.                 printf("\n\n%s: logoff is in progress...\n", strupr(argv[0]));
  140.  
  141.                 usRc = u32eulgf(argv[1],            /*  userid            */
  142.                                 argv[usIndex],      /*  remotename length */
  143.                                 UPM_DOMAIN);        /*  remotetype        */
  144.  
  145.                 if (usRc == UPM_OK)
  146.                 {
  147.                     printf("%s: \"%s\" was successfully logged off.\n",
  148.                             strupr(argv[0]), strupr(argv[1]));
  149.                     break;
  150.                 }
  151.                 else if (usRc == UPM_LOGGED)
  152.                 {
  153.                     printf("Logoff could not complete.\nMake sure that "\
  154.                            "no other session is using a network device "\
  155.                            "and try again.\n");
  156.                 }
  157.                 else
  158.                 {
  159.                     printf("Error: u32eulgf() returned = %X\n", usRc);
  160.                     break;
  161.                 }
  162.             }
  163.         } while (usRc == UPM_LOGGED);
  164.     }
  165.  
  166.     usRc = KbdClose(KbdHandle);
  167.     if (usRc != 0)
  168.         printf("\nError: KbdClose() returned = %d\n", usRc);
  169.  
  170.     DosExit(EXIT_PROCESS, usRc);
  171. }
  172.