home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sysmgmt / sms / smsapi / enumobj / enumobj.cpp next >
C/C++ Source or Header  |  1996-10-15  |  10KB  |  378 lines

  1. // ====================================================================
  2. //
  3. //  File: enumobj.cpp
  4. //
  5. //  Copyright (c) 1996 Microsoft Corp.
  6. //
  7. //  Author:
  8. //      Jonathan Shuval     Microsoft Corp.
  9. //
  10. //  Sample program for illustrating use of the SMS object enumeration
  11. //  APIs.
  12. //
  13. //
  14. // ====================================================================
  15.  
  16.  
  17. // ====================================================================
  18. //
  19. //      Includes
  20. //
  21. // ====================================================================
  22. #include <afx.h>
  23. #include <smsapi.h>
  24.  
  25.  
  26. // Include the GetStatusName function.
  27. // -------------------------------------
  28. #include "..\common\status.inc"
  29.  
  30. // ====================================================================
  31. //
  32. //      Defines.
  33. //
  34. // ====================================================================
  35. #define CCH_MAXINPUT 256
  36.  
  37.  
  38. // ====================================================================
  39. //
  40. //      Prototypes.
  41. //
  42. // ====================================================================
  43.  
  44. // Display an error message with its SMS status value.
  45. // -------------------------------------------------------------
  46. void DisplaySmsError( const char *pszMessage, SMS_STATUS stat );
  47.  
  48.  
  49. // Connect to the SMS datasource.
  50. // -------------------------------------------------------------
  51. HANDLE ConnectToDatasource();
  52.  
  53.  
  54. // Do the real enumerate, recursive.
  55. // -------------------------------------------------------------
  56. void enumObj( HANDLE hConnect,
  57.               SMSBUFF *pPreds,
  58.               DWORD ctPreds,
  59.               char *pszObj,
  60.               int iIndent );
  61.  
  62.  
  63. // Prompt the user for input and return the reply.
  64. // -------------------------------------------------------------
  65. void InputString( const char *pszMessage, char *pszResult );
  66.  
  67.  
  68.  
  69. // ====================================================================
  70. //
  71. //      Globals.
  72. //
  73. // ====================================================================
  74. // None.
  75.  
  76.  
  77.  
  78.  
  79. // ====================================================================
  80. //
  81. //      Start here.
  82. //
  83. // ====================================================================
  84.  
  85. void main()
  86. {
  87.  
  88.  
  89.  
  90.     // Get and display the SMS API version.
  91.     // ------------------------------------
  92.     char *pszVersion;                   // Receives pointer to the version string.
  93.     SmsAPIVer( &pszVersion );           // Get version
  94.     printf("Using %s\n", pszVersion);   // and print it.
  95.  
  96.  
  97.     SMS_STATUS stat;
  98.     DWORD dwLoop;
  99.  
  100.  
  101.     //===========================================
  102.     // Connect to the SMS datasource.
  103.     //===========================================
  104.     HANDLE hConnect;
  105.     hConnect = ConnectToDatasource();
  106.  
  107.     if (hConnect == NULL) {
  108.         return;
  109.     }
  110.  
  111.  
  112.     // Get the first-class objects.
  113.     // ============================
  114.     SMSBUFF ObjectTypes[10];
  115.     DWORD ctObjectTypes = 10;
  116.  
  117.     stat = SmsEnumObjectTypes( hConnect, ObjectTypes, &ctObjectTypes );
  118.  
  119.     if (stat != SMS_OK) {
  120.         printf("EnumObjectTypes fails: %d\n", stat);
  121.         return;
  122.     }
  123.  
  124.     printf("There are %d object types...\n", ctObjectTypes);
  125.     for (dwLoop = 0; dwLoop < ctObjectTypes; dwLoop++) {
  126.         printf("\t%s\n", ObjectTypes[dwLoop]);
  127.     }
  128.     printf("\n\n");
  129.  
  130.  
  131.  
  132.     // Now loop for each object type
  133.     // =============================
  134.  
  135.     for (dwLoop = 0; dwLoop < ctObjectTypes; dwLoop++) {
  136.         printf("\n[%d] Enumerating %s...\n", dwLoop, ObjectTypes[dwLoop]);
  137.  
  138.  
  139.         SMSBUFF Predecessors[50];
  140.         DWORD ctPreds = 0;
  141.  
  142.         enumObj( hConnect, Predecessors, ctPreds, ObjectTypes[dwLoop], 1 );
  143.  
  144.     }
  145.  
  146.  
  147.     printf("**** All done ****\n\n");
  148.  
  149.     SmsDataSourceDisconnect( hConnect );
  150.  
  151. }
  152.  
  153.  
  154.  
  155.  
  156. // ====================================================================
  157. // enumObj
  158. //
  159. //
  160. //
  161. //
  162. // Parameters:
  163. //      HANDLE hConnect         Handle to the connection.
  164. //      SMSBUFF *pPreds         Pointer to the predecessor list.
  165. //      DWORD ctPreds           Number of entries in the predecessor list.
  166. //                              Note: this is not the size of the list,
  167. //                              it is the number of entries that are
  168. //                              filled in in the list.
  169. //      char *pszObj            Name of object to enumerate.
  170. //      int iIndent             Indent level. Just so we can display things
  171. //                              nicely.
  172. //
  173. // Returns;
  174. //      Nothing.
  175. //
  176. // NOTE: assumes that predecessor buffer is big enough.
  177. // ====================================================================
  178. void enumObj( HANDLE hConnect,
  179.               SMSBUFF *pPreds,
  180.               DWORD ctPreds,
  181.               char *pszObj,
  182.               int iIndent )
  183. {
  184.     OBJDESCRIPTOR Objects[50];      // Filled in by the API. Contains
  185.                                     // descriptions of each object.
  186.     DWORD ctObjs = 50;
  187.     SMS_STATUS stat;
  188.     char *pszObjType;               // We display the object type.
  189.  
  190.     // Leading spaces for indentation. Just so that the display looks ok.
  191.     // ------------------------------------------------------------------
  192.     char szIndent[50];
  193.     szIndent[0] = '\0';
  194.     for (int i = 0; i < iIndent; i++) {
  195.         strcat(szIndent, "    ");
  196.     }
  197.  
  198.  
  199.     stat = SmsEnumObjects( hConnect, pszObj, pPreds, ctPreds, Objects, &ctObjs );
  200.     if (stat == SMS_NO_MORE_DATA) {
  201.         return;             // Just means we've finished enumerating this one.
  202.     }
  203.     if (stat != SMS_OK) {
  204.         DisplaySmsError("Bad return from EnumObjects", stat);
  205.         return;
  206.     }
  207.  
  208.  
  209.     // Add each object from our returned buffer into the
  210.     // predecessor list and recurse.
  211.     // =================================================
  212.     for (DWORD dwLoop = 0; dwLoop < ctObjs; dwLoop++) {
  213.  
  214.         // Print out the current object.
  215.         // -----------------------------
  216.         printf("%s%-25s", szIndent, Objects[dwLoop].szName);
  217.  
  218.         // If we've got a friendly name print it.
  219.         // --------------------------------------
  220.         if (Objects[dwLoop].bGotFriendlyName) {
  221.             printf(" <%s>", Objects[dwLoop].szFriendlyName);
  222.         }
  223.  
  224.         // If this is an attribute we'll have its range of
  225.         // relational operators.
  226.         // -----------------------------------------------
  227.         if (Objects[dwLoop].bGotRelops) {
  228.             printf(" ['%s' to '%s']",
  229.                 OpName[Objects[dwLoop].dwRelopMin],
  230.                 OpName[Objects[dwLoop].dwRelopMax]);
  231.         }
  232.         printf("\n");
  233.  
  234.         // Print object type.
  235.         // ------------------
  236.         switch (Objects[dwLoop].objType) {
  237.         case OT_ARCHLIST:
  238.             pszObjType = "Architecture list";
  239.             break;
  240.  
  241.         case OT_ARCH:
  242.             pszObjType = "Architecture";
  243.             break;
  244.  
  245.         case OT_GROUP:
  246.             pszObjType = "Group";
  247.             break;
  248.  
  249.         case OT_ATTRIBUTE:
  250.             pszObjType = "Attribute";
  251.             break;
  252.  
  253.  
  254.         case OT_PLATFORMS:
  255.             pszObjType = "Platform list";
  256.             break;
  257.  
  258.         case OT_PLATFORM_PDF:
  259.             pszObjType = "PDF Platform";
  260.             break;
  261.  
  262.         case OT_PLATFORM_WINST:
  263.             pszObjType = "Winst platform";
  264.             break;
  265.  
  266.         case OT_PLATFORM_NAD:
  267.             pszObjType = "NAD platform";
  268.             break;
  269.  
  270.  
  271.         case OT_UNKNOWN:
  272.             pszObjType = "Unknown";
  273.             break;
  274.  
  275.         }
  276.         printf("%sThis is a: %s\n", szIndent, pszObjType );
  277.  
  278.  
  279.         // Recurse to get descendents.
  280.         // ---------------------------
  281.         strcpy( pPreds[ctPreds], pszObj );      // Add current object to pred list
  282.         enumObj( hConnect, pPreds, ctPreds+1, Objects[dwLoop].szName, iIndent+1 );
  283.  
  284.     }
  285.  
  286. }
  287.  
  288.  
  289.  
  290.  
  291.  
  292. // ====================================================================
  293. // InputString
  294. //
  295. // Prompt the user to input a string and return the string in the
  296. // specified buffer.
  297. //
  298. // Parameters:
  299. //      const char* pszMessage
  300. //          The user prompt to display.
  301. //
  302. //      char* pszResult
  303. //          Pointer to the buffer where the user's input will be returned.
  304. //
  305. // Returns;
  306. //      The user's input is returned via the given buffer.
  307. //
  308. // ====================================================================
  309. void InputString( const char *pszMessage, char *pszResult)
  310. {
  311.     printf("%s: ", pszMessage);
  312.     gets(pszResult);
  313. }
  314.  
  315.  
  316. // ====================================================================
  317. //
  318. // ConnectToDatasource
  319. //
  320. // Get the datasource connection information from the user and use it
  321. // to connect to the datasource.
  322. //
  323. // Parameters:  None.
  324. //
  325. // Returns:
  326. //      The connection handle or NULL if the connection failed.
  327. //
  328. // ====================================================================
  329. HANDLE ConnectToDatasource()
  330. {
  331.     // Get the information we need to connect to the
  332.     // data source from the user.
  333.     //==============================================
  334.     char szServer[CCH_MAXINPUT];
  335.     char szUser[CCH_MAXINPUT];
  336.     char szPasswd[CCH_MAXINPUT];
  337.     char szDatabase[CCH_MAXINPUT];
  338.  
  339.     printf("\n");
  340.     printf("**************************\n");
  341.     printf("* Connect to data source *\n");
  342.     printf("**************************\n");
  343.     InputString("SQL server name", szServer);
  344.     InputString("SQL database name", szDatabase);
  345.     InputString("User name for SQL server", szUser);
  346.     InputString("Password for SQL server", szPasswd);
  347.     printf("\n");
  348.  
  349.  
  350.     // Connect to a data source. SQL in this case.
  351.     // ===========================================
  352.     DATASOURCE dsParams;
  353.  
  354.     dsParams.sqlParams.ds          = DB_SQL;
  355.     dsParams.sqlParams.pszServer   = szServer;
  356.     dsParams.sqlParams.pszUserName = szUser;
  357.     dsParams.sqlParams.pszPasswd   = szPasswd;
  358.     dsParams.sqlParams.pszDbName   = szDatabase;
  359.     dsParams.sqlParams.pFunc       = NULL;         // No encryption.
  360.     dsParams.sqlParams.pszKey      = "";
  361.  
  362.     HANDLE hConnect;
  363.     SMS_STATUS stat;
  364.     stat = SmsDataSourceConnect( &dsParams, &hConnect);
  365.  
  366.     if (stat != SMS_OK) {
  367.         hConnect = NULL;
  368.         DisplaySmsError("Connect to data source failed", stat);
  369.     }
  370.  
  371.     return( hConnect );
  372. }
  373.  
  374.  
  375.  
  376. /* EOF: enumobj.cpp */
  377.  
  378.