home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / dbsamp.exe / DBUSNAP / DBUSNAP.C < prev    next >
C/C++ Source or Header  |  1995-01-13  |  17KB  |  591 lines

  1. /****************************************************************************
  2. **    File:    DBUSNAP.C
  3. **
  4. **    Desc:    This snapin adds a page to the user dialog in NWAdmin to allow for
  5. **                editing the host Database Server Attribute.
  6. **
  7. **
  8. **        DISCLAIMER
  9. **
  10. **    Novell, Inc. makes no representations or warranties with respect to
  11. **    any NetWare software, and specifically disclaims any express or
  12. **    implied warranties of merchantability, title, or fitness for a
  13. **    particular purpose.
  14. **
  15. **    Distribution of any NetWare software is forbidden without the
  16. **    express written consent of Novell, Inc.  Further, Novell reserves
  17. **    the right to discontinue distribution of any NetWare software.
  18. **
  19. **    Novell is not responsible for lost profits or revenue, loss of use
  20. **    of the software, loss of data, costs of re-creating lost data, the
  21. **    cost of any substitute equipment or program, or claims by any party
  22. **    other than you.  Novell strongly recommends a backup be made before
  23. **    any software is installed.   Technical support for this software
  24. **    may be provided at the discretion of Novell.
  25. **
  26. **    Programmers:
  27. **
  28. **        Ini    Who                                Firm
  29. **        --- ----------------- ------------------------------------------------
  30. **        CRG    Calvin Gaisford        Novell Developer Support.
  31. **
  32. **    History:
  33. **
  34. **        When         Who     What
  35. **        -------- ---     ---------------------------------------------------------
  36. **        1-04-95  CRG     Modification of this code for distibution. Much of this
  37. **                  code was taken from the snapin example that comes from
  38. **                                    the Novell SDK.  In fact, I just copied and pasted a lot
  39. **                                    of the code that is the same for all snapin modules.
  40. */
  41.  
  42.  
  43.  
  44. /***************************************************************************
  45. **  Libraries Linked in for .exe
  46. **
  47. **  NWCALLS.LIB
  48. **  NWLOCALE.LIB
  49. **  NWNET.LIB
  50. **    SNAPIN.LIB
  51. **
  52. */
  53.  
  54. /****************************************************************************
  55. **    Include headers
  56. */
  57.     /*-----------------------------------------------------------------------
  58.     **    WINDOWS
  59.     */
  60.     #include <windows.h>
  61.     #include <toolhelp.h>
  62.     #include <string.h>
  63.     #include <stdio.h>
  64.  
  65.     /*-----------------------------------------------------------------------
  66.     **    NetWare
  67.     */
  68.     #include <nwnet.h>
  69.     #include <nwsnapin.h>
  70.  
  71.     /*-----------------------------------------------------------------------
  72.     **    Program
  73.     */
  74.     #include "dbusnap.h"
  75.  
  76.  
  77. /***************************************************************************
  78. **    Function Prototypes
  79. */
  80.  
  81. #ifdef __cplusplus
  82. extern "C"
  83. {
  84. #endif
  85.  
  86. N_EXTERN_LIBRARY( NWRCODE )
  87. SnapinUserProc(pnstr8 name, nuint16 msg, nparam p1, nparam p2);
  88.  
  89. BOOL _export FAR PASCAL UserDatabaseDlg(HWND hwnd, UINT message,
  90.                                                                                          WPARAM wParam, LPARAM lParam);
  91. N_EXTERN_LIBRARY( nuint32 )
  92. FNWFlatBrowserLaunchProc( nuint32 userParam, nuint16 uMsg,
  93.                                                  nparam param1, nparam param2 );
  94.  
  95. int ModifyUserObjectData(char *name);
  96. int ReadUserObjectData(char *name);
  97.  
  98. #ifdef __cplusplus
  99. }
  100. #endif
  101.  
  102.  
  103. #define NUM_USER_PAGES   1
  104.  
  105. /***************************************************************************
  106. **  Globals Used in code
  107. */
  108. NWAPageStruct userPages[NUM_USER_PAGES];
  109. HINSTANCE hDLL;
  110. HINSTANCE hCtrlDLL;
  111. static    dbNameDefined = 0;
  112. static    bChangeDBName = 0;
  113. static char    dbName[MAX_DN_CHARS];
  114. static char  dbNameChange[MAX_DN_CHARS];
  115. static char     uName[MAX_DN_CHARS];
  116. /*-------------------------------------------------------------------------*/
  117. /*    DLL Entry Point                                                      */
  118. /*-------------------------------------------------------------------------*/
  119. int FAR PASCAL LibMain (HINSTANCE hInstance, WORD wDataSeg,
  120.                                                 WORD cbHeapSize, LPSTR lpCmdLine)
  121. {
  122.      hDLL = hInstance;
  123.      if(cbHeapSize != 0)
  124.             UnlockData (0);
  125.      return 1;
  126. };
  127.  
  128. /*-------------------------------------------------------------------------*/
  129. /* Function : InitSnapin(void)                                             */
  130. /* Description :                                                           */
  131. /*    Every Snapin DLL must provide this function. In this function,       */
  132. /*    snapin menu items (under Tools) are registered. Also, object procs   */
  133. /*    are registered.                                                      */
  134. /*                                                                         */
  135. /*-------------------------------------------------------------------------*/
  136. int _export FAR PASCAL InitSnapin()
  137. {
  138.      NWARegisterObjectProc(NWA_DS_OBJECT_TYPE,
  139.                                                  "User",
  140.                                                  "Novell, Inc. (C) All rights reserved.",
  141.                                                     hDLL,
  142.                                                     (NWASnapinObjectProc) SnapinUserProc,
  143.                                                     NWA_SNAPIN_VERSION);
  144.      return NWA_RET_SUCCESS;
  145. }
  146.  
  147.  
  148. /*-------------------------------------------------------------------------*/
  149. /* Function : SnapinUserProc()                                     */
  150. /* Description :                                                           */
  151. /*   Add on User Snapin Page                                         */
  152. /*-------------------------------------------------------------------------*/
  153. N_GLOBAL_LIBRARY( NWRCODE ) _export
  154. SnapinUserProc(pnstr8 name, nuint16 msg, nparam p1, nparam p2)
  155. {
  156.     switch(msg)
  157.     {
  158.         case NWA_MSG_INITSNAPIN:
  159.                 {
  160.                  /*-------------------------------------------------*/
  161.                  /*           initialize pages                      */
  162.                  /*-------------------------------------------------*/
  163.                  userPages[0].dlgProc = UserDatabaseDlg;
  164.                  userPages[0].resName = "USER_DATABASE";
  165.                  userPages[0].pageTitle = "User Database";
  166.                  return NWA_RET_SUCCESS;
  167.                 }
  168.  
  169.             case NWA_MSG_GETPAGECOUNT:
  170.                      {
  171.                          /*-----------------------------*/
  172.                          /* Initialize User Database Data    */
  173.                          /*-----------------------------*/
  174.                          lstrcpy(uName, name);
  175.                          dbName[0]='\0';
  176.                          dbNameChange[0]='\0';
  177.                          ReadUserObjectData(uName);
  178.                          return NUM_USER_PAGES;
  179.                      }
  180.  
  181.             case NWA_MSG_REGISTERPAGE:
  182.                      {
  183.                          NWAPageStruct  *pageInfo =   (NWAPageStruct *)p2;
  184.                          pageInfo->dlgProc    = userPages[p1].dlgProc;
  185.                          pageInfo->resName    = userPages[p1].resName;
  186.                          pageInfo->pageTitle  = userPages[p1].pageTitle;
  187.                          pageInfo->hDLL = hDLL;
  188.                          pageInfo->initParam = 0;
  189.                          return NWA_RET_SUCCESS;
  190.                      }
  191.  
  192.             case NWA_MSG_MODIFY:
  193.                      {
  194.                          ModifyUserObjectData(uName);
  195.                          return NWA_RET_SUCCESS;
  196.                      }
  197.  
  198.             case NWA_MSG_RENAME:
  199.                      {
  200.                          return NWA_RET_DODEFAULT;
  201.                      }
  202.  
  203.             case NWA_MSG_QUERYDELETE:
  204.                      {
  205.                          return TRUE;
  206.                      }
  207.  
  208.             case NWA_MSG_CLOSESNAPIN:
  209.                      {
  210.                          return NWA_RET_SUCCESS;
  211.                      }
  212.             default:
  213.                      break;
  214.      }
  215.      return NWA_RET_SUCCESS;
  216. }
  217.  
  218.  
  219. /*-------------------------------------------------------------------------*/
  220. /* Function : UserDatabaseDlg                                    */
  221. /* Description :                                                           */
  222. /*  Add on User Database Page Dialog                        */
  223. /*-------------------------------------------------------------------------*/
  224. BOOL _export FAR PASCAL UserDatabaseDlg(HWND hwnd, UINT message,
  225.                                                                                 WPARAM wParam, LPARAM lParam)
  226. {
  227.      NWDSContextHandle context;
  228.      int               err=0;
  229.      char contextStr[256];
  230.  
  231.      switch(message)
  232.      {
  233.             case WM_INITDIALOG:
  234.                 {
  235.                     SetDlgItemText(hwnd, DEFAULT_DATABASE_NAME, dbName);
  236.                     bChangeDBName=0;
  237.                     return TRUE;
  238.                 }
  239.  
  240.             case NWA_WM_F1HELP:
  241.                 {
  242.                     MessageBox(NULL, "Description:\n\tA text field to hold a\n\tdescription of the Server.\nHost NetWare Server:\n\tHolds the distinguished\n\tname (DN)of the NetWare\n\tServer the Database is\n\tloaded on.", "Database Server Help", MB_OK);
  243.                     return TRUE;
  244.                 }
  245.  
  246.             case WM_COMMAND:
  247.             switch (wParam)
  248.             {
  249.                  case DEFAULT_DATABASE_NAME:
  250.                             if(HIWORD(lParam) == EN_CHANGE)
  251.                             {
  252.                                 bChangeDBName = 1;
  253.                                 //GetDlgItemText(hwnd, HOST_NETWARE_NAME, dbServerObjData.hnwsname, MAX_DN_CHARS);
  254.                                 SendMessage(hwnd, NWA_WM_SETPAGEMODIFY, (WORD)hwnd, MAKELONG(0,TRUE));
  255.                             }
  256.                             break;
  257.  
  258.  
  259.                      case IDC_FIND_DATABASE:
  260.                             context = NWDSCreateContext();
  261.                             if ( (int) context == ERR_CONTEXT_CREATION )
  262.                                 break;
  263.                             err = NWDSGetContext(context, DCK_NAME_CONTEXT, contextStr);
  264.                             if ( err < 0 )
  265.                             {
  266.                                 NWDSFreeContext(context);
  267.                                 break;
  268.                             }
  269.                             NWALaunchDSFlatBrowser(hwnd,
  270.                                          &hwnd,
  271.                                          contextStr,
  272.                                          NULL,
  273.                                          NULL,
  274.                                          "Selected Object",
  275.                                          NWA_FB_SINGLE_SELECT,
  276.                                          FNWFlatBrowserLaunchProc);
  277.                             break;
  278.  
  279.                         default:
  280.                                 break;
  281.             }
  282.             break;
  283.             case NWA_WM_CANCLOSE:
  284.             {
  285.                 // xfer data to program memory
  286.                 GetDlgItemText(hwnd, DEFAULT_DATABASE_NAME, dbNameChange, 1024);
  287.                 return TRUE;
  288.             }
  289.      }
  290.      return FALSE ;
  291. }
  292.  
  293.  
  294.  
  295. /*-------------------------------------------------------------------------*/
  296. /* Function : ModifyUserObjectData(void)                                       */
  297. /* Description :                                                           */
  298. /*  Modifys a Users Database Atribute Value                              */
  299. /*-------------------------------------------------------------------------*/
  300. int ModifyUserObjectData(char *name)
  301. {
  302.     NWDSContextHandle context;
  303.     int               err=0;
  304.     Buf_T             *inpBuf;
  305.     uint32            flags;
  306.  
  307.     context = NWDSCreateContext();
  308.     if ( (int) context == ERR_CONTEXT_CREATION )
  309.         return ((int) context);
  310.  
  311.     err=NWDSSetContext(context, DCK_NAME_CONTEXT, DS_ROOT_NAME);
  312.     if ( err < 0 )
  313.     {
  314.         NWDSFreeContext(context);
  315.         return err;
  316.     }
  317.  
  318.     err = NWDSGetContext(context, DCK_FLAGS, (void *)&flags);
  319.  
  320.     flags |= DCV_TYPELESS_NAMES;
  321.     flags |= DCV_XLATE_STRINGS;
  322.     flags |= DCV_DEREF_ALIASES;
  323.     flags |= DCV_DEREF_BASE_CLASS;
  324.  
  325.     err   = NWDSSetContext (context, DCK_FLAGS, (void *)&flags );
  326.  
  327.     err=NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &inpBuf);
  328.     if ( err < 0 )
  329.     {
  330.         NWDSFreeContext(context);
  331.         return err;
  332.     }
  333.  
  334.     err=NWDSInitBuf(context, DSV_MODIFY_ENTRY, inpBuf);
  335.     if ( err < 0 )
  336.     {
  337.         NWDSFreeContext(context);
  338.         NWDSFreeBuf(inpBuf);
  339.         return err;
  340.     }
  341.  
  342.     /*---------------------------------------------------------------*/
  343.     /* Modify DBName Attribute                                  */
  344.     /*---------------------------------------------------------------*/
  345.     if ( bChangeDBName )
  346.     {
  347.         if ( dbNameDefined )
  348.         {
  349.             err = NWDSPutChange(context, inpBuf, DS_REMOVE_VALUE, "DBName");
  350.             if ( err < 0 )
  351.             {
  352.                 NWDSFreeContext(context);
  353.                 NWDSFreeBuf(inpBuf);
  354.                 return err;
  355.             }
  356.             err = NWDSPutAttrVal(context, inpBuf, SYN_DIST_NAME, dbName);
  357.             if ( err < 0 )
  358.             {
  359.                 NWDSFreeContext(context);
  360.                 NWDSFreeBuf(inpBuf);
  361.                 return err;
  362.             }
  363.         }
  364.         if(strlen(dbNameChange) > 0)
  365.         {
  366.             err = NWDSPutChange(context, inpBuf, DS_ADD_VALUE, "DBName");
  367.             if ( err < 0 )
  368.             {
  369.                 NWDSFreeContext(context);
  370.                 NWDSFreeBuf(inpBuf);
  371.                 return err;
  372.             }
  373.             err = NWDSPutAttrVal(context, inpBuf, SYN_DIST_NAME, dbNameChange);
  374.             if ( err < 0 )
  375.             {
  376.                 NWDSFreeContext(context);
  377.                 NWDSFreeBuf(inpBuf);
  378.                 return err;
  379.             }
  380.         }
  381.     }
  382.     err= NWDSModifyObject(context, name, NULL, 0, inpBuf);
  383.     if( err < 0 )
  384.         MessageBox(NULL, "Error Making Modification", "DBUSnap Error", MB_OK);
  385.  
  386.     NWDSFreeContext(context);
  387.     NWDSFreeBuf(inpBuf);
  388.     return err;
  389. }
  390.  
  391.  
  392. /*-------------------------------------------------------------------------*/
  393. /* Function : ReadUserObjectData                                           */
  394. /* Description :                                                           */
  395. /*   Function for reading object data in DS                    */
  396. /*-------------------------------------------------------------------------*/
  397. int ReadUserObjectData(char *name)
  398. {
  399.     NWDSContextHandle context;
  400.     int               err=0;
  401.     int32             iterHandle=-1L;
  402.     Buf_T             *resBuf, *nameBuf;
  403.     uint32            totAttr, valCount, synID;
  404.     char              attrName[MAX_DN_CHARS+1];
  405.     uint32            flags;
  406.  
  407.     context = NWDSCreateContext();
  408.     if ( (int) context == ERR_CONTEXT_CREATION )
  409.         return ( (int) context );
  410.  
  411.     err=NWDSSetContext(context, DCK_NAME_CONTEXT, DS_ROOT_NAME);
  412.     if ( err < 0 )
  413.     {
  414.         NWDSFreeContext(context);
  415.         return err;
  416.     }
  417.  
  418.     err = NWDSGetContext(context, DCK_FLAGS, (void *)&flags);
  419.     if ( err < 0 )
  420.     {
  421.         NWDSFreeContext(context);
  422.         return err;
  423.     }
  424.  
  425.     flags |= DCV_TYPELESS_NAMES;
  426.     flags |= DCV_XLATE_STRINGS;
  427.     flags |= DCV_DEREF_ALIASES;
  428.     flags |= DCV_DEREF_BASE_CLASS;
  429.  
  430.     err   = NWDSSetContext (context, DCK_FLAGS, (void *)&flags );
  431.     if ( err < 0 )
  432.     {
  433.         NWDSFreeContext(context);
  434.         return err;
  435.     }
  436.  
  437.     err=NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &resBuf);
  438.     if ( err < 0 )
  439.     {
  440.         NWDSFreeContext(context);
  441.         return err;
  442.     }
  443.  
  444.     err=NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &nameBuf);
  445.     if ( err < 0 )
  446.     {
  447.         NWDSFreeContext(context);
  448.         NWDSFreeBuf(resBuf);
  449.         return err;
  450.     }
  451.  
  452.     /*----------------------------------------------------------------------*/
  453.     /* Initialize input buffer and check for errors. The output buffer does */
  454.     /* not need to be initialized. This buffer will be initialized to       */
  455.     /* DSV_READ operations                                                  */
  456.     /*----------------------------------------------------------------------*/
  457.  
  458.     err=NWDSInitBuf(context, DSV_READ, nameBuf);
  459.     if ( err < 0 )
  460.     {
  461.         NWDSFreeContext(context);
  462.         NWDSFreeBuf(resBuf);
  463.         NWDSFreeBuf(nameBuf);
  464.         return err;
  465.     }
  466.  
  467.     /*----------------------------------------------------------------------*/
  468.     /* Put object attributes that are to be read into input buffer.         */
  469.     /* Check for errors.                                                    */
  470.     /*----------------------------------------------------------------------*/
  471.     err= NWDSPutAttrName(context, nameBuf, "DBName");
  472.     if ( err < 0 )
  473.     {
  474.         NWDSFreeContext(context);
  475.         NWDSFreeBuf(resBuf);
  476.         NWDSFreeBuf(nameBuf);
  477.         return err;
  478.     }
  479.  
  480.     /*----------------------------------------------------------------------*/
  481.     /* Read the data.                                                       */
  482.     /*----------------------------------------------------------------------*/
  483.  
  484.     dbNameDefined=0;
  485.     iterHandle=NO_MORE_ITERATIONS;
  486.     do
  487.     {
  488.         err=NWDSRead(context, name, DS_ATTRIBUTE_VALUES,
  489.                                  FALSE, nameBuf, &iterHandle, resBuf);
  490.         if ( err < 0 )
  491.         {
  492.             NWDSFreeContext(context);
  493.             NWDSFreeBuf(resBuf);
  494.             NWDSFreeBuf(nameBuf);
  495.             return err;
  496.         }
  497.  
  498.         /*--------------------------------------------------------------------*/
  499.         /* pull stuff out of the output buffer, checking for errors.          */
  500.         /* First, get total no of attributes in the buffer.                   */
  501.         /*--------------------------------------------------------------------*/
  502.         err=NWDSGetAttrCount(context, resBuf, &totAttr);
  503.         if ( err < 0 )
  504.         {
  505.             NWDSFreeContext(context);
  506.             NWDSFreeBuf(resBuf);
  507.             NWDSFreeBuf(nameBuf);
  508.             return err;
  509.         }
  510.  
  511.         err=NWDSGetAttrName(context, resBuf, attrName, &valCount, &synID);
  512.         if ( err < 0 )
  513.         {
  514.             NWDSFreeContext(context);
  515.             NWDSFreeBuf(resBuf);
  516.             NWDSFreeBuf(nameBuf);
  517.             return err;
  518.         }
  519.         if(!lstrcmp(attrName,"DBName"))
  520.         {
  521.             dbNameDefined=1;
  522.             err=NWDSGetAttrVal(context, resBuf, synID, dbName);
  523.             if ( err < 0 )
  524.             {
  525.                 NWDSFreeContext(context);
  526.                 NWDSFreeBuf(resBuf);
  527.                 NWDSFreeBuf(nameBuf);
  528.                 return err;
  529.             }
  530.         }
  531.     } while ( iterHandle != NO_MORE_ITERATIONS );
  532.  
  533.     NWDSFreeContext(context);
  534.     NWDSFreeBuf(resBuf);
  535.     NWDSFreeBuf(nameBuf);
  536.     return 0;
  537. }
  538.  
  539.  
  540. /*-------------------------------------------------------------------------*/
  541. /* Function : FNWFlatBrowserLaunchProc                                      */
  542. /* Description :                                                           */
  543. /*  DSFlat Browser Callback Proc                                           */
  544. /*-------------------------------------------------------------------------*/
  545. N_EXTERN_LIBRARY( nuint32 ) _export
  546. FNWFlatBrowserLaunchProc
  547. (
  548.     nuint32 userParam,
  549.     nuint16 msg,
  550.     nparam param1,
  551.     nparam param2
  552. )
  553. {
  554.      int fltCount;
  555.      NWASelObject *pObj;
  556.      HWND *puData;
  557.  
  558.      fltCount=1;
  559.  
  560.      switch( msg )
  561.      {
  562.          case NWA_MSG_FBFILTER_COUNT :
  563.              {
  564.                  *((int *)param1) = fltCount;
  565.                  break;
  566.              }
  567.  
  568.          case NWA_MSG_FBFILTER_VALUE :
  569.              {
  570.                  switch( param1 )
  571.                  {
  572.                      case 0L:
  573.                          strcpy( (char *) param2, "Database");
  574.                          break;
  575.                  }
  576.                  break;
  577.              }
  578.  
  579.          case NWA_MSG_FBOBJECT_VALUE :
  580.              {
  581.                  pObj= ( NWASelObject * ) param2;
  582.                  puData = (HWND *) userParam;
  583.  
  584.                  SetDlgItemText(*puData, DEFAULT_DATABASE_NAME, pObj->objName);
  585.                  break;
  586.              }
  587.      }
  588.      return (0);
  589. }
  590.  
  591.