home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / snapx1.exe / SNAPPETS.C < prev    next >
Text File  |  1995-02-16  |  17KB  |  649 lines

  1. /****************************************************************************
  2. **    File:    SNAPPETS.C
  3. **
  4. **    Desc:    This is a NWAdmin SNAPIN DLL module which allows the Administrator
  5. **            to add the pets that a user has. A button named "User Pets" appears
  6. **           as an NWAdmin page when "Details" is selected for a User object.
  7. **        
  8. **
  9. **        DISCLAIMER  
  10. **  
  11. **    Novell, Inc. makes no representations or warranties with respect to
  12. **    any NetWare software, and specifically disclaims any express or
  13. **    implied warranties of merchantability, title, or fitness for a
  14. **    particular purpose.  
  15. **
  16. **    Distribution of any NetWare software is forbidden without the
  17. **    express written consent of Novell, Inc.  Further, Novell reserves
  18. **    the right to discontinue distribution of any NetWare software.
  19. **    
  20. **    Novell is not responsible for lost profits or revenue, loss of use
  21. **    of the software, loss of data, costs of re-creating lost data, the
  22. **    cost of any substitute equipment or program, or claims by any party
  23. **    other than you.  Novell strongly recommends a backup be made before
  24. **    any software is installed.   Technical support for this software
  25. **    may be provided at the discretion of Novell.
  26. **
  27. **    Programmers:
  28. **
  29. **        Ini    Who                        Firm
  30. **        -----------------------------------------------------------------------
  31. **        KLB    Karl Bunnell                Novell Developer Support.
  32. **
  33. **    History:
  34. **
  35. **        When        Who    What
  36. **        -----------------------------------------------------------------------
  37. **        10-03-94    klb    First code.
  38. */
  39.  
  40. /****************************************************************************
  41. **    Include headers, macros, function prototypes, etc.
  42. */
  43.  
  44.     /*------------------------------------------------------------------------
  45.     **    MACROS
  46.     */
  47.     #define NWWIN
  48.  
  49.     /*------------------------------------------------------------------------
  50.     **    ANSI
  51.     */
  52.     #include <windows.h>
  53.     #include <toolhelp.h>
  54.     #include <string.h>
  55.     #include <stdio.h>
  56.  
  57.  
  58.     /*------------------------------------------------------------------------
  59.     **    NetWare
  60.     */
  61.     #include <nwnet.h>
  62.     #include <nwsnapin.h>
  63.     #include "snappets.h"
  64.  
  65.  
  66.     /*------------------------------------------------------------------------
  67.     **    Prototypes
  68.     */
  69.  
  70.     #ifdef __cplusplus
  71.     extern "C"
  72.     {
  73.     #endif
  74.  
  75.     N_EXTERN_LIBRARY( NWRCODE )
  76.     SnapinPetsProc(pnstr8 name, nuint16 msg, nparam p1, nparam p2);
  77.     BOOL _export FAR PASCAL petsSnapinPageDlg(HWND hwnd, UINT message,
  78.                         WPARAM wParam, LPARAM lParam);
  79.  
  80.  
  81.     int ModifyUserAttrib(char *objectName);
  82.     int ReadObjectData(void);
  83.     #ifdef __cplusplus
  84.     }
  85.     #endif
  86.  
  87.     /*------------------------------------------------------------------------
  88.     **    Globals
  89.     */
  90.  
  91.     #define     NWWIN
  92.     #define     NUM_PETS_PAGES 1
  93.     char        petList[20][256];
  94.     int        petCount;
  95.     int        petDeltaCount;
  96.     char        petDeltaList[20][256];
  97.     int        petChange=0;
  98.     char        userObjectName[80];
  99.     HINSTANCE hDLL;
  100.     NWAPageStruct petsPages[NUM_PETS_PAGES];
  101.  
  102.  
  103.  
  104.  
  105. /*************************************************************************
  106. **    DLL Entry Point
  107. */
  108. int FAR PASCAL LibMain (HINSTANCE hInstance, WORD wDataSeg, 
  109.                         WORD cbHeapSize, LPSTR lpCmdLine)
  110. {
  111.    hDLL = hInstance;
  112.    if(cbHeapSize != 0)
  113.       UnlockData (0);
  114.    return 1;
  115. };
  116.  
  117.  
  118. /*************************************************************************
  119. ** Function : InitSnapin(void)                                             
  120. ** Description :                                                           
  121. **    Every Snapin DLL must provide this function. In this function,       
  122. **    snapin menu items (under Tools) are registered. Also, object procs   
  123. **    are registered.                                                      
  124. **
  125. */
  126. int _export FAR PASCAL InitSnapin()
  127. {
  128.  
  129.  
  130.    /*---------------------------------------------------------------------
  131.    ** Registering User class procedure
  132.    */
  133.  
  134.    NWARegisterObjectProc(
  135.     /* Class type    */ NWA_DS_OBJECT_TYPE, 
  136.         /* Class name    */ C_USER, 
  137.         /* Devel. Info   */ "Novell, Inc. (C) All rights reserved.", 
  138.         /* HINSTANCE     */ hDLL, 
  139.         /* Obj Call back */ (NWASnapinObjectProc) SnapinPetsProc, 
  140.         /* Version       */ NWA_SNAPIN_VERSION
  141.     );
  142.  
  143.  
  144.  
  145.    return NWA_RET_SUCCESS;
  146. }
  147.  
  148.  
  149. /*************************************************************************
  150. ** Function : SnapinPetsProc                                               
  151. ** Description : Snapin Procedure                                                          
  152. ** Snapin AFP Server Object Class Proc                                   
  153.  
  154. */
  155. N_GLOBAL_LIBRARY( NWRCODE ) _export
  156. SnapinPetsProc(pnstr8 objectName, nuint16 msg, nparam p1, nparam p2)
  157. {
  158.    int    err;
  159.    char returnBuf[80];
  160.  
  161.    switch(msg)
  162.    {
  163.       case NWA_MSG_INITSNAPIN:
  164.            {
  165.                 /*-----------------------------------------------------------
  166.                 **           initialize pages
  167.                 */
  168.                 petsPages[0].dlgProc = (DLGPROC)petsSnapinPageDlg;
  169.                 petsPages[0].resName = "SNAPPETS";
  170.                 petsPages[0].pageTitle = "User Pets";
  171.  
  172.                 return NWA_RET_SUCCESS;
  173.            }
  174.  
  175.       case NWA_MSG_GETPAGECOUNT:
  176.            {
  177.            lstrcpy(userObjectName, objectName);
  178.            petCount = 0;
  179.            petDeltaCount = 0;
  180.            err = ReadObjectData();
  181.            wsprintf(returnBuf, "ReadObjectData returned :%04X", err);
  182.  
  183.            MessageBox(NULL, returnBuf,
  184.                   "ReadObjectData() Returned:", MB_OK);
  185.  
  186.  
  187.                return NUM_PETS_PAGES;
  188.           }
  189.  
  190.       case NWA_MSG_REGISTERPAGE:
  191.             {
  192.             NWAPageStruct  *pageInfo =   (NWAPageStruct *)p2;
  193.             pageInfo->dlgProc    = petsPages[p1].dlgProc;
  194.             pageInfo->resName    = petsPages[p1].resName;
  195.             pageInfo->pageTitle  = petsPages[p1].pageTitle;
  196.             pageInfo->hDLL = hDLL;
  197.             pageInfo->initParam = 0;
  198.             return NWA_RET_SUCCESS;
  199.             }
  200.  
  201.       case NWA_MSG_MODIFY:
  202.             {
  203.             MessageBox(NULL, "NWA_MSG_MODIFY received", 
  204.                             "Pets Proc", MB_OK);
  205.  
  206.             err = ModifyUserAttrib(objectName);
  207.             wsprintf(returnBuf, "ModifyUserAttrib returned :%04X", err);
  208.  
  209.             MessageBox(NULL, returnBuf,
  210.                             "ModifyUserAttrib() Returned:", MB_OK);
  211.  
  212.  
  213.             return NWA_RET_SUCCESS;
  214.             }
  215.  
  216.       case NWA_MSG_CLOSESNAPIN:
  217.             {
  218.             MessageBox(NULL, "NWA_MSG_CLOSESNAPIN received", 
  219.                        "Pets proc", MB_OK);
  220.             return NWA_RET_SUCCESS;
  221.             }
  222.  
  223.       default:
  224.            break;
  225.    }
  226.    return NWA_RET_SUCCESS;
  227. }
  228.  
  229.  
  230. /*************************************************************************
  231. ** Function : PetsSnapinPageDlg                                            
  232. ** Description :                                                           
  233. ** Page dialog to enhance User Details.
  234. */
  235. BOOL _export FAR PASCAL petsSnapinPageDlg(HWND hwnd, UINT message,
  236.                                        WPARAM wParam, LPARAM lParam)
  237. {
  238.    char petName[255];
  239.    int  numOfChars;    
  240.    switch(message)
  241.    {
  242.  
  243.       case WM_INITDIALOG:
  244.             {
  245.             int i;
  246.             petChange = 0;
  247.             for ( i=0; i<petCount; i++)
  248.                 SendDlgItemMessage(hwnd, IDC_PETLIST, LB_ADDSTRING, 0, 
  249.                                            (LPARAM) petList[i] );
  250.             return TRUE;
  251.             }
  252.  
  253.       case NWA_WM_F1HELP:
  254.             {
  255.             MessageBox(NULL, "Enter Pets name in Edit Field!", "Snapin Help", MB_OK);
  256.                             return TRUE;
  257.             }
  258.       case WM_COMMAND:
  259.           switch (wParam)
  260.           {
  261.           case IDC_ADDPET :
  262.                 {
  263.                  memset(petName, 0, 255);
  264.  
  265.                  numOfChars = GetDlgItemText(hwnd, IDC_EDITNAME, petName, 255);
  266.                  if (numOfChars > 0)
  267.                      {
  268.                       SendMessage(hwnd, NWA_WM_SETPAGEMODIFY, (WORD)hwnd,
  269.                                    MAKELONG(0,TRUE));
  270.  
  271.                       SendDlgItemMessage(hwnd, IDC_PETLIST, LB_ADDSTRING, -1,
  272.                                                  (LPARAM)petName);
  273.  
  274.                      } 
  275.                 }
  276.                 break;
  277.  
  278.               case IDC_DELETEPET :
  279.                     {
  280.                     int selCount, i;
  281.                     int *pIndex;
  282.                     HGLOBAL hmem; 
  283.  
  284.                     selCount=SendDlgItemMessage(hwnd, IDC_PETLIST, 
  285.                                                 LB_GETSELCOUNT, 0, 0L );
  286.                     if ( selCount !=LB_ERR )
  287.                         {
  288.                           SendMessage(hwnd, NWA_WM_SETPAGEMODIFY, (WORD) hwnd, 
  289.                                       MAKELONG(0,TRUE));
  290.                           hmem= GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, 
  291.                          selCount * sizeof(int));
  292.                           pIndex= (int  *) GlobalLock( hmem );
  293.                           selCount= SendDlgItemMessage(hwnd, IDC_PETLIST, 
  294.                                                    LB_GETSELITEMS, selCount, 
  295.                                                    (long) (int FAR *) pIndex );
  296.                           for(i=selCount; i > 0 ; i--)
  297.                           SendDlgItemMessage(hwnd, IDC_PETLIST, LB_DELETESTRING,
  298.                                              pIndex[i-1], 0L);
  299.                           GlobalUnlock(hmem);
  300.                           GlobalFree(hmem);
  301.                         }
  302.                     }
  303.                     break;
  304.           }
  305.             return TRUE;
  306.  
  307.       case NWA_WM_CANCLOSE:
  308.  
  309.             /*-------------------------------------------------------------
  310.             ** xfer data from dialog memory to program memory
  311.             ** so NWA_MSG_MODIFY can save it in DS if required.
  312.             */
  313.  
  314.             {
  315.             int itemCount, i;
  316.             petChange = 1;
  317.             itemCount = SendDlgItemMessage(hwnd, IDC_PETLIST, LB_GETCOUNT,
  318.                                                    0, 0L);
  319.             petDeltaCount=itemCount;
  320.             for(i=0; ( i<petDeltaCount ) && 
  321.                              ( i < MAX_PETS ) ; i++ )
  322.                 SendDlgItemMessage(hwnd, IDC_PETLIST, LB_GETTEXT,
  323.                                          i, (LPARAM) petDeltaList[i]);
  324.                     return TRUE;
  325.             }
  326.  
  327.  
  328.     }
  329.    return FALSE ;
  330. }
  331.  
  332. /*************************************************************************
  333. ** Function : ModifyUserAttrib(char *name)                                 
  334. ** Description :                                                           
  335. **  Modifys user pet values
  336. */
  337. int ModifyUserAttrib(char *name)
  338. {
  339.     NWDSContextHandle context;
  340.     int               err=0, i, j;
  341.     Buf_T             *inpBuf;
  342.     uint32            flags;
  343.     uint32            syntaxID;
  344.  
  345.     context = NWDSCreateContext();
  346.     if ( context == ERR_CONTEXT_CREATION )
  347.       return ((int) context);
  348.  
  349.     err=NWDSSetContext(context, DCK_NAME_CONTEXT, DS_ROOT_NAME);
  350.     if ( err < 0 )
  351.         {
  352.           NWDSFreeContext(context);
  353.           return err;
  354.         }
  355.  
  356.     err = NWDSGetContext(context, DCK_FLAGS, (void *)&flags);
  357.  
  358.     flags |= DCV_TYPELESS_NAMES;
  359.     flags |= DCV_XLATE_STRINGS;
  360.     flags |= DCV_DEREF_ALIASES;
  361.     flags |= DCV_DEREF_BASE_CLASS;
  362.  
  363.     err   = NWDSSetContext (context, DCK_FLAGS, (void *)&flags );
  364.  
  365.     err=NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &inpBuf);
  366.     if ( err < 0 )
  367.         {
  368.           NWDSFreeContext(context);
  369.           return err;
  370.         }
  371.  
  372.     err=NWDSInitBuf(context, DSV_MODIFY_ENTRY, inpBuf);
  373.     if ( err < 0 )
  374.         {
  375.           NWDSFreeContext(context);
  376.           NWDSFreeBuf(inpBuf);
  377.           return err;
  378.         }
  379.  
  380.     /*---------------------------------------------------------------------
  381.     ** Get syntaxID of Pets
  382.     */
  383.     err = NWDSGetSyntaxID(context, "Pets", &syntaxID);
  384.     if ( err < 0 )
  385.         {
  386.           NWDSFreeContext(context);
  387.           NWDSFreeBuf(inpBuf);
  388.           return err;
  389.         }
  390.  
  391.     /*---------------------------------------------------------------------
  392.     ** Modify Pet Attribute values
  393.     */
  394.     if ( petChange )
  395.     {
  396.       if ( petCount )
  397.       {
  398.             /*-----------------------------------------------------------------
  399.             ** There were some Pets for the object, remove them first
  400.             */
  401.     
  402.      
  403.         err = NWDSPutChange(context, inpBuf, DS_REMOVE_VALUE, "Pets");
  404.         if ( err < 0 )
  405.             {
  406.           NWDSFreeContext(context);
  407.           NWDSFreeBuf(inpBuf);
  408.           return err;
  409.             }
  410.  
  411.         for(j=0; j<petCount; j++)
  412.         {
  413.           err = NWDSPutAttrVal(context, inpBuf, syntaxID, 
  414.                                petList[j]);
  415.           if ( err < 0 )
  416.               {
  417.                 NWDSFreeContext(context);
  418.                 NWDSFreeBuf(inpBuf);
  419.                 return err;
  420.               }
  421.         }
  422.  
  423.             /*-------------------------------------------------------------------
  424.             ** Add new list of pets selected if any
  425.             */
  426.  
  427.         if ( petDeltaCount )
  428.         {
  429.           err = NWDSPutChange(context, inpBuf, DS_ADD_VALUE, "Pets");
  430.           if ( err < 0 )
  431.               {
  432.                 NWDSFreeContext(context);
  433.                 NWDSFreeBuf(inpBuf);
  434.                 return err;
  435.               }
  436.  
  437.           for(j=0; j<petDeltaCount; j++)
  438.           {
  439.             err = NWDSPutAttrVal(context, inpBuf, syntaxID, 
  440.                                petDeltaList[j]);
  441.             if ( err < 0 )
  442.                 {
  443.              NWDSFreeContext(context);
  444.              NWDSFreeBuf(inpBuf);
  445.              return err;
  446.                 }
  447.           }
  448.         }
  449.       }
  450.       else
  451.       {
  452.     /*------------------------------------------------------------------
  453.     ** There were no pets for the object to start with.
  454.     **    Add new list of pets selected ( if any )
  455.     */
  456.      
  457.      
  458.  
  459.         if ( petDeltaCount )
  460.         {
  461.           err = NWDSPutChange(context, inpBuf, DS_ADD_VALUE, "Pets");
  462.           if ( err < 0 )
  463.               {
  464.                 NWDSFreeContext(context);
  465.                 NWDSFreeBuf(inpBuf);
  466.                 return err;
  467.               }
  468.  
  469.           for(j=0; j<petDeltaCount; j++)
  470.           {
  471.             err = NWDSPutAttrVal(context, inpBuf, syntaxID, 
  472.                                petDeltaList[j]);
  473.             if ( err < 0 )
  474.                 {
  475.              NWDSFreeContext(context);
  476.              NWDSFreeBuf(inpBuf);
  477.              return err;
  478.                 }
  479.           }
  480.         }
  481.       }
  482.     }
  483.  
  484.     err= NWDSModifyObject(context, name, NULL, 0, inpBuf);
  485.  
  486.     NWDSFreeContext(context);
  487.     NWDSFreeBuf(inpBuf);
  488.     return err;
  489. }
  490.  
  491. /*************************************************************************
  492. ** Function : ReadObjectData                                               
  493. ** Description :                                                           
  494. **   Function for reading
  495. */
  496. int ReadObjectData()
  497. {
  498.     NWDSContextHandle context;
  499.     int               err=0, i, j;
  500.     int32             iterHandle=-1L;
  501.     Buf_T             *resBuf, *nameBuf;
  502.     char              *attributes[]={"Pets"};
  503.     uint32            totAttr, valCount, synID;
  504.     char              attrName[MAX_DN_CHARS+1];
  505.     uint32            flags;
  506.  
  507.     context = NWDSCreateContext();
  508.     if ( context == ERR_CONTEXT_CREATION )
  509.       return ( (int) context );
  510.  
  511.     err=NWDSSetContext(context, DCK_NAME_CONTEXT, DS_ROOT_NAME);
  512.     if ( err < 0 )
  513.         {
  514.           NWDSFreeContext(context);
  515.           return err;
  516.         }
  517.  
  518.     err = NWDSGetContext(context, DCK_FLAGS, (void *)&flags);
  519.     if ( err < 0 )
  520.         {
  521.           NWDSFreeContext(context);
  522.           return err;
  523.         }
  524.  
  525.     flags |= DCV_TYPELESS_NAMES;
  526.     flags |= DCV_XLATE_STRINGS;
  527.     flags |= DCV_DEREF_ALIASES;
  528.     flags |= DCV_DEREF_BASE_CLASS;
  529.  
  530.     err   = NWDSSetContext (context, DCK_FLAGS, (void *)&flags );
  531.     if ( err < 0 )
  532.         {
  533.           NWDSFreeContext(context);
  534.           return err;
  535.         }
  536.  
  537.  
  538.     err=NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &resBuf);
  539.     if ( err < 0 )
  540.         {
  541.           NWDSFreeContext(context);
  542.           return err;
  543.         }
  544.  
  545.     err=NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &nameBuf);
  546.     if ( err < 0 )
  547.         {
  548.           NWDSFreeContext(context);
  549.           NWDSFreeBuf(resBuf);
  550.           return err;
  551.         }
  552.  
  553.     /*----------------------------------------------------------------------
  554.     ** Initialize input buffer and check for errors. The output buffer does 
  555.     ** not need to be initialized. This buffer will be initialized to       
  556.     ** DSV_READ operations
  557.     */
  558.  
  559.     err=NWDSInitBuf(context, DSV_READ, nameBuf);
  560.     if ( err < 0 )
  561.         {
  562.           NWDSFreeContext(context);
  563.           NWDSFreeBuf(resBuf);
  564.           NWDSFreeBuf(nameBuf);
  565.           return err;
  566.         }
  567.  
  568.     /*----------------------------------------------------------------------
  569.     ** Put object attributes that are to be read into input buffer.         
  570.     ** Check for errors.
  571.     */
  572.     for(i=0; i < 1; i++)
  573.     {
  574.       err= NWDSPutAttrName(context, nameBuf, attributes[i]);
  575.       if ( err < 0 )
  576.           {
  577.         NWDSFreeContext(context);
  578.         NWDSFreeBuf(resBuf);
  579.         NWDSFreeBuf(nameBuf);
  580.         return err;
  581.           }
  582.     }
  583.  
  584.     /*----------------------------------------------------------------------
  585.     ** Read the data.
  586.     */
  587.  
  588.     iterHandle=NO_MORE_ITERATIONS;
  589.     do
  590.     {
  591.       err=NWDSRead(context, userObjectName, DS_ATTRIBUTE_VALUES,
  592.                    FALSE, nameBuf, &iterHandle, resBuf);
  593.       if ( err < 0 )
  594.           {
  595.         NWDSFreeContext(context);
  596.         NWDSFreeBuf(resBuf);
  597.         NWDSFreeBuf(nameBuf);
  598.         return err;
  599.           }
  600.   
  601.       /*--------------------------------------------------------------------
  602.       ** pull stuff out of the output buffer, checking for errors.          
  603.       ** First, get total no of attributes in the buffer.
  604.      */
  605.       err=NWDSGetAttrCount(context, resBuf, &totAttr);
  606.       if ( err < 0 )
  607.           {
  608.        NWDSFreeContext(context);
  609.        NWDSFreeBuf(resBuf);
  610.        NWDSFreeBuf(nameBuf);
  611.        return err;
  612.           }
  613.  
  614.       for(i=0; i< totAttr; i++)
  615.       {
  616.         err=NWDSGetAttrName(context, resBuf, attrName, &valCount, &synID);
  617.         if ( err < 0 )
  618.             {
  619.           NWDSFreeContext(context);
  620.           NWDSFreeBuf(resBuf);
  621.           NWDSFreeBuf(nameBuf);
  622.           return err;
  623.             }
  624.         if ( lstrcmp(attrName, "Pets") == 0 )
  625.         {
  626.            petCount = valCount;
  627.           for ( j=0; j<valCount; j++)
  628.           {
  629.             err=NWDSGetAttrVal(context, resBuf, synID, 
  630.                                petList[j]);
  631.             if ( err < 0 )
  632.                 {
  633.               NWDSFreeContext(context);
  634.               NWDSFreeBuf(resBuf);
  635.               NWDSFreeBuf(nameBuf);
  636.               return err;
  637.                 }
  638.           }
  639.         }
  640.       }
  641.     } while ( iterHandle != NO_MORE_ITERATIONS );
  642.  
  643.     NWDSFreeContext(context);
  644.     NWDSFreeBuf(resBuf);
  645.     NWDSFreeBuf(nameBuf);
  646.     return 0;
  647. }
  648.  
  649.