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 / newjob / newjob.cpp < prev    next >
C/C++ Source or Header  |  1996-10-15  |  27KB  |  882 lines

  1. // ====================================================================
  2. //
  3. //  File: newjob.cpp
  4. //
  5. //  Copyright (C) 1994, 1995 by Microsoft Corporation.
  6. //
  7. //  Author:
  8. //      Jonathan Shuval     Microsoft Corp.
  9. //
  10. //
  11. //  This program illustrates the use of the SMS APIs to view, create
  12. //  and delete jobs.
  13. //
  14. //  See the readme.txt file in this directory for more information.
  15. //
  16. //
  17. // ====================================================================
  18.  
  19.  
  20.  
  21. // ====================================================================
  22. //  Includes.
  23. // ====================================================================
  24. #include <afx.h>
  25. #include <smsapi.h>                 // Header for the APIs.
  26. #include <time.h>                   // for time functions.
  27.  
  28. // Include the GetStatusName function.
  29. // -------------------------------------
  30. #include "..\common\status.inc"
  31.  
  32.  
  33.  
  34. // ====================================================================
  35. //
  36. //  Defines.
  37. //
  38. // ====================================================================
  39. #define CCH_MAXINPUT 256
  40.  
  41.  
  42.  
  43. // ====================================================================
  44. //  Local prototypes.
  45. // ====================================================================
  46.  
  47.  
  48. // Display the folder's scalars.
  49. // -------------------------------------------------------------
  50. void DisplayScalars( HANDLE hFolder );
  51.  
  52.  
  53. // Display a folder.
  54. // -------------------------------------------------------------
  55. void ViewFolder( HANDLE hFolder );
  56.  
  57.  
  58. // Create a new folder.
  59. // -------------------------------------------------------------
  60. void CreateNewFolder( HANDLE hParent );
  61.  
  62.  
  63. // Convert date string from "dd\mm\yy hh:mm" to time_t.
  64. // -------------------------------------------------------------
  65. time_t ReadTime( char szTime[] );
  66.  
  67.  
  68. // Prompt the user for input and return the reply.
  69. //------------------------------------------------
  70. void InputString(const char* pszMessage, char* pszResult);
  71.  
  72.  
  73. // Connect to the SMS datasource.
  74. //------------------------------
  75. HANDLE ConnectToDatasource();
  76.  
  77.  
  78.  
  79.  
  80. // Display the help message.
  81. //=========================
  82. void DisplayHelp();
  83.  
  84. // Display the greeting.
  85. //======================
  86. void DisplayGreeting();
  87.  
  88.  
  89. // Check to see if there was a request for help
  90. // on the command line.
  91. //==============================================
  92. BOOL DidRequestHelp(int argc, char** argv);
  93.  
  94.  
  95.  
  96. // Display an error message with its SMS status value.
  97. //====================================================
  98. void DisplaySmsError(const char* pszMessage, SMS_STATUS stat);
  99.  
  100. // Get the string equivallent for an SMS status code.
  101. //===================================================
  102. const char* GetStatusName(SMS_STATUS stat);
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. // ====================================================================
  110. //  The work starts here.
  111. // ====================================================================
  112. void main(int argc, char** argv)
  113. {
  114.     // Check to see if this is a request to display the help
  115.     // screen.  If so, display it. Otherwise, display a greeting
  116.     // banner.
  117.     //=========================================================
  118.     if (DidRequestHelp(argc, argv)) {
  119.         DisplayHelp();
  120.         return;
  121.     }
  122.     else {
  123.         DisplayGreeting();
  124.     }
  125.  
  126.  
  127.  
  128.  
  129.     SMS_STATUS stat;
  130.     HANDLE hConnect;
  131.     HANDLE hContainer;
  132.  
  133.  
  134.     //===========================================
  135.     // Connect to the SMS datasource.
  136.     //===========================================
  137.     hConnect = ConnectToDatasource();
  138.     if (hConnect == NULL) {
  139.         return;
  140.     }
  141.  
  142.     // Open job container.
  143.     // ===================
  144.     stat = SmsOpenContainer( C_JOB, hConnect, &hContainer );
  145.     if (stat != SMS_OK) {
  146.         DisplaySmsError("SmsOpenContainer failed", stat);
  147.         SmsDataSourceDisconnect( hConnect );
  148.         return;
  149.     }
  150.  
  151.  
  152.     // Set filters.
  153.     // ============
  154.     // None.
  155.  
  156.  
  157.     // Select all folders matching our filters.
  158.     // ========================================
  159.     stat = SmsPopulate( hContainer, POP_SYNC, NULL );
  160.     if (stat != SMS_OK && stat != SMS_EMPTY) {
  161.         DisplaySmsError("Bad return from SmsPopulate", stat);
  162.         SmsCloseContainer( hContainer );
  163.         SmsDataSourceDisconnect( hConnect );
  164.         return;
  165.     }
  166.  
  167.  
  168.  
  169.     // Retrieve all the folders from the container and save them
  170.     // away in a list. SmsGetFolderCount tells us how many folders
  171.     // are in the container, use this value to allocate space for
  172.     // the handles.
  173.     // Don't close the container because it will be needed if we
  174.     // want to create a job.
  175.     // ===========================================================
  176.     DWORD  numFolders;
  177.     DWORD  dwI;
  178.     HANDLE *phFolders;
  179.     HANDLE hFolder;
  180.  
  181.     SmsGetFolderCount( hContainer, F_ANY, &numFolders);
  182.     printf("==========  Container has %d folders  =========\n\n", numFolders );
  183.  
  184.  
  185.     // Allocate memory for the top-level folders.
  186.     // ------------------------------------------
  187.     phFolders = (HANDLE *)malloc(numFolders * sizeof(HANDLE));
  188.  
  189.     // Open all top-level folders.
  190.     // ---------------------------
  191.     for (dwI = 0; dwI < numFolders; dwI++) {
  192.  
  193.         stat = SmsGetNextFolder( hContainer, F_ANY, &hFolder );
  194.         if (stat != SMS_OK) {
  195.             DisplaySmsError("Error in retrieving a folder", stat);
  196.             break;
  197.         }
  198.  
  199.         phFolders[dwI] = hFolder;
  200.     }
  201.  
  202.  
  203.     // Now we enter a loop asking the user whether they want to
  204.     // view the next folder, delete the folder just displayed,
  205.     // or create a new folder.
  206.     // =========================================================
  207.     BOOL  done = FALSE;
  208.     char  reply[10];
  209.     DWORD dwIndex = 0;
  210.     char  szFolderID[SMS_DATA_BUFF_SIZE+1];
  211.  
  212.     // Display the first one (if it exists). That way we always
  213.     // have a current folder.
  214.     // If we don't have any then that's ok because the Delete
  215.     // will fail like it's supposed to.
  216.     // ========================================================
  217.     if (numFolders > 0) {
  218.         ViewFolder( phFolders[dwIndex] );
  219.     }
  220.  
  221.     printf("You may now do one of the following:\n");
  222.     printf("View the Next folder, Delete the current folder, Create a new folder\n");
  223.     printf("Type X to exit\n");
  224.  
  225.     while (!done) {
  226.  
  227.         printf("[NDCX]: ");
  228.         gets(reply);
  229.  
  230.         switch (reply[0]) {
  231.         case 0:
  232.         case 'N': case 'n':
  233.             // =========== View next folder ====================
  234.             dwIndex++;
  235.             if (dwIndex < numFolders) {
  236.                 ViewFolder( phFolders[dwIndex] );
  237.             } else {
  238.                 printf("No more folders to view\n");
  239.             }
  240.             break;
  241.  
  242.         case 'D': case 'd':
  243.             // =========== Delete current folder ===============
  244.             // Check we have a folder to delete.
  245.             if (dwIndex >= numFolders) {
  246.                 printf("No folder to delete\n");
  247.                 break;
  248.             }
  249.  
  250.             SmsGetFolderID( phFolders[dwIndex], szFolderID );
  251.             printf("Confirm deletion of folder \"%s\"? [y/n] ", szFolderID);
  252.             gets(reply);
  253.             if (reply[0] != 'y') {
  254.                 printf("Delete cancelled\n");
  255.                 break;
  256.             }
  257.  
  258.             stat = SmsUnlinkFolder( phFolders[dwIndex] );
  259.             if (stat != SMS_OK) {
  260.                 DisplaySmsError( "SmsUnlinkFolder failed", stat);
  261.                 break;
  262.             }
  263.             stat = SmsCommitFolder( phFolders[dwIndex] );
  264.             if (stat != SMS_OK) {
  265.                 DisplaySmsError("SmsCommitFolder error", stat);
  266.                 break;
  267.             }
  268.  
  269.             printf("Folder deleted\n");
  270.             break;
  271.  
  272.         case 'C': case 'c':
  273.             // =========== Create new folder ===================
  274.             CreateNewFolder( hContainer );
  275.             break;
  276.  
  277.         case 'X': case 'x':
  278.             // Terminate.
  279.             done = TRUE;
  280.             break;
  281.  
  282.         default:
  283.             // print the message again.
  284.             printf("Actions are: N(ext folder), D(elete folder), C(reate folder)\n");
  285.             printf("Type X to exit\n");
  286.             break;
  287.         }
  288.     }
  289.  
  290.  
  291.  
  292.  
  293.  
  294.     // Close the container, and disconnect from datasource.
  295.     // ----------------------------------------------------
  296.     SmsCloseContainer( hContainer );
  297.     stat = SmsDataSourceDisconnect( hConnect );
  298.  
  299.  
  300.     // Deallocate memory used for folder handles.
  301.     // ------------------------------------------
  302.     free( phFolders );
  303.  
  304. }  /* main */
  305.  
  306.  
  307.  
  308.  
  309.  
  310. // ====================================================================
  311. // Display the folder contents.
  312. //
  313. //  This version only displays the folder name and type, number of
  314. //  scalars, and the scalars. Other information not relevant.
  315. //
  316. //  NOTE: no error checking.
  317. //
  318. // ====================================================================
  319. void ViewFolder( HANDLE hFolder )
  320. {
  321.     char szFolderID[SMS_DATA_BUFF_SIZE+1];              // This folder's ID.
  322.     char szfType[SMS_DATA_BUFF_SIZE+1];                 // Folder's tag.
  323.     DWORD fType;                                        // Folder's type.
  324.     DWORD ctScalars;                                    // How many scalars in this folder.
  325.     SMS_STATUS stat;
  326.  
  327.     // Get folder ID and type.
  328.     // -----------------------
  329.     stat = SmsGetFolderID( hFolder, szFolderID );
  330.     // Test return. If this is a newly created folder that hasn't been
  331.     // inserted into the datasource it will not yet have a valid ID.
  332.     if (stat != SMS_OK) {
  333.         strcpy(szFolderID, "<New Job>");
  334.     }
  335.     stat = SmsGetFolderType( hFolder, &fType, szfType );
  336.  
  337.     // Rewind the folder, just in case.
  338.     // This allows us to re-examine a folder (ie call this multiple times).
  339.     // ====================================================================
  340.     SmsRewind( hFolder, RW_ALL );
  341.  
  342.     // Get count of scalars.
  343.     // ---------------------
  344.     stat = SmsGetScalarCount( hFolder, &ctScalars );
  345.     printf("%s (%s) contains %d scalars\n", szFolderID, szfType, ctScalars);
  346.  
  347.     // Display the scalars.
  348.     // --------------------
  349.     DisplayScalars( hFolder );
  350.  
  351.     printf("============== %s ===========\n", szFolderID);
  352.  
  353. }  /* ViewFolder */
  354.  
  355.  
  356.  
  357. // ====================================================================
  358. //
  359. //  Create a new folder.
  360. //
  361. //  This is an example of how to create a folder.
  362. //
  363. //  There are several types of job folder. This function can create:
  364. //      a run command on workstation job
  365. //      a share package on server job
  366. //      a remove package from server job.
  367. //
  368. //
  369. // ====================================================================
  370.  
  371. void CreateNewFolder( HANDLE hParent )
  372. {
  373.     printf("This function allows the creation of one of:\n");
  374.     printf("\t\"Run command on Workstation\" job (w)\n");
  375.     printf("\t\"Share package on Server\" job    (s)\n");
  376.     printf("\t\"Remove package from Server\" job (r)\n");
  377.  
  378.     DWORD dwJobType;
  379.     char szType[10];
  380.     BOOL done = FALSE;
  381.  
  382.     while (!done) {
  383.         done = TRUE;
  384.  
  385.         printf("Enter one of [wsr]: ");
  386.         gets(szType);
  387.  
  388.         switch (szType[0]) {
  389.         case 'w': case 'W':
  390.             dwJobType = F_INSTALLJOB;
  391.             break;
  392.  
  393.         case 's': case 'S':
  394.             dwJobType = F_SRVINSTALLJOB;
  395.             break;
  396.  
  397.         case 'r': case 'R':
  398.             dwJobType = F_REMPKGJOB;
  399.             break;
  400.  
  401.         default:
  402.             done = FALSE;
  403.             printf("Invalid reply\n");
  404.         }
  405.     }
  406.  
  407.     HANDLE hNewFolder;
  408.     SMS_STATUS stat;
  409.  
  410.     // Note the id of "<New Job>", this is because for jobs, SMS assigns
  411.     // the job id itself.
  412.  
  413.     stat = SmsCreateFolder( hParent, dwJobType, "<New Job>", &hNewFolder );
  414.     if (stat != SMS_OK) {
  415.         DisplaySmsError("SmsCreateFolder fails", stat);
  416.         return;
  417.     }
  418.  
  419.     // Use an objectity engine API to get information about the
  420.     // folder and its scalars.
  421.     // ---------------------------------------------------------
  422.     FOLDER_INFO *pFInfo;
  423.     stat = SmsDescribeFolder( T_FOLDER, dwJobType, &pFInfo );
  424.  
  425.     SCALAR_INFO *pSInfo;        // The info structure contains a pointer
  426.                                 // to an array of SCALAR_INFOs. Each of these
  427.                                 // describes a scalar.
  428.     SCALAR sc;                  // We create scalars using a SCALAR.
  429.  
  430.     // Data for retrieving information from user for scalars.
  431.     char szValue[SMS_DATA_BUFF_SIZE+1];     // User enters value here.
  432.     time_t time;                            // Times are converted to t_time.
  433.  
  434.  
  435.     // Loop through all the scalars, ask the user to supply a
  436.     // value, but only if the scalar is modifiable.
  437.     // =========================================================
  438.  
  439.     for (DWORD dwI = 0; dwI < pFInfo->ctScalars; dwI++) {
  440.  
  441.         pSInfo = &pFInfo->pScalars[dwI];
  442.  
  443.         // Is scalar modifiable?
  444.         if (pSInfo->fAccess == ACCESS_CREATE || pSInfo->fAccess == ACCESS_MODIFY) {
  445.  
  446.             // Set up common fields in the scalar.
  447.             // -----------------------------------
  448.             sc.pszName = pSInfo->szName;
  449.             sc.scType  = pSInfo->scType;
  450.  
  451.             char *pszType;
  452.  
  453.             switch (sc.scType) {
  454.             case SCALAR_STRING:
  455.                 pszType = "string";     break;
  456.  
  457.             case SCALAR_INT:
  458.                 pszType = "integer";    break;
  459.  
  460.             case SCALAR_TIME:
  461.                 pszType = "time";       break;
  462.  
  463.             case SCALAR_BINARY:
  464.                 pszType = "binary";     break;
  465.             }
  466.             printf("Enter (%s) value for '%s': ", pszType, pSInfo->szName);
  467.  
  468.             // Now we need to know what type this is (string, int etc)
  469.             if (pSInfo->scType == SCALAR_STRING) {
  470.                 gets( szValue );
  471.                 // store it directly
  472.                 sc.dwLen = sizeof(szValue)-1;   // -1 for terminating NULL.
  473.                 sc.pszValue = szValue;
  474.  
  475.                 stat = SmsSetScalar( hNewFolder, &sc );
  476.  
  477.  
  478.             } else if (pSInfo->scType == SCALAR_INT) {
  479.                 gets( szValue );
  480.                 // convert to int
  481.                 sc.dwValue = atol( szValue );
  482.  
  483.                 stat = SmsSetScalar( hNewFolder, &sc );
  484.  
  485.  
  486.             } else if (pSInfo->scType == SCALAR_TIME) {
  487.                 printf("Enter time in the form mm/dd/yy hh:mm ");
  488.                 gets( szValue );
  489.                 // Only primitive checking
  490.                 time = ReadTime( szValue );
  491.                 sc.tValue = time;
  492.  
  493.                 stat = SmsSetScalar( hNewFolder, &sc );
  494.  
  495.  
  496.             } else if (pSInfo->scType == SCALAR_BINARY) {
  497.                 // Be simplistic about this.
  498.                 gets( szValue );
  499.                 memcpy( sc.pValue, szValue, strlen(szValue) );
  500.                 stat = SmsSetScalar( hNewFolder, &sc );
  501.             }
  502.         }
  503.  
  504.     }
  505.  
  506.  
  507.     // Now display it and ask user for confirmation of creation.
  508.     // =========================================================
  509.     printf("Newly created folder:\n");
  510.     ViewFolder( hNewFolder );
  511.     printf("OK to link folder into parent [y/n]? ");
  512.     char reply[10];
  513.     gets(reply);
  514.     if (reply[0] == 'y') {
  515.         // link it in to parent.
  516.         stat = SmsLinkFolder( hNewFolder );
  517.         if (stat != SMS_OK) {
  518.             DisplaySmsError("SmsLinkFolder error", stat);
  519.             return;
  520.         }
  521.  
  522.         // write it back to data source.
  523.         stat = SmsCommitFolder( hNewFolder );
  524.         if (stat != SMS_OK) {
  525.             DisplaySmsError("SmsCommitFolder error", stat);
  526.             return;
  527.         }
  528.         SmsGetFolderID( hNewFolder, szValue );
  529.  
  530.         char *pszType;
  531.         switch (dwJobType) {
  532.         case F_INSTALLJOB:
  533.             pszType = "Workstation install job";
  534.             break;
  535.  
  536.         case F_SRVINSTALLJOB:
  537.             pszType = "Server share job";
  538.             break;
  539.  
  540.         case F_REMPKGJOB:
  541.             pszType = "Remove package job";
  542.             break;
  543.  
  544.         }
  545.  
  546.         printf("*****  %s %s created *****\n\n", pszType, szValue);
  547.  
  548.         printf("View of new folder from the datasource...\n");
  549.         ViewFolder( hNewFolder );
  550.         printf("\n\n");
  551.  
  552.     } else {
  553.         printf("Folder creation cancelled\n");
  554.     }
  555.  
  556. }
  557.  
  558.  
  559.  
  560. // ====================================================================
  561. //
  562. //  Display all the scalars for the folder.
  563. //
  564. //  This example also shows how the scalar can detect that a buffer is
  565. //  too small.
  566. // ====================================================================
  567. void DisplayScalars( HANDLE hFolder )
  568. {
  569.     SMS_STATUS stat = SMS_OK;
  570.     SCALAR scalar;
  571.     char szName[SMS_DATA_BUFF_SIZE+1];       // buffer for name
  572.     char szValue[SMS_DATA_BUFF_SIZE+1];      // buffer for value
  573.     char *pszTime;          // For conversion of time scalars.
  574.  
  575.  
  576.     scalar.pszName  = szName;
  577.     scalar.pszValue = szValue;
  578.  
  579.  
  580.     while (1) {
  581.  
  582.         scalar.dwLen = sizeof(szValue)-1;       // must tell him the size
  583.  
  584.         stat = SmsGetNextScalar( hFolder, &scalar);
  585.         if (stat != SMS_OK && stat != SMS_MORE_DATA) {
  586.             break;
  587.         }
  588.  
  589.         if (stat == SMS_MORE_DATA) {
  590.             printf("Receive buffer too small, should be %d. Data truncated\n",
  591.                             scalar.dwLen);
  592.         }
  593.  
  594.         // Check scalar type, display accordingly.
  595.         // ---------------------------------------
  596.         switch (scalar.scType) {
  597.         case SCALAR_STRING:
  598.             printf("\t%25s: %s\n", scalar.pszName, scalar.pszValue);
  599.             break;
  600.  
  601.         case SCALAR_INT:
  602.             printf("\t%25s: %ld\n", scalar.pszName, scalar.dwValue);
  603.             break;
  604.  
  605.         case SCALAR_TIME:
  606.             // If there is a string equivalence use it.
  607.             if (scalar.bStringEquivalence) {
  608.                 printf("\t%25s: %s\n", scalar.pszName, scalar.pszValue);
  609.             } else {
  610.                 pszTime = ctime( &scalar.tValue );
  611.                 printf("\t%25s: %s", scalar.pszName, pszTime);
  612.             }
  613.             break;
  614.  
  615.         case SCALAR_BINARY:
  616.             // Got binary data.
  617.             // Just tell the user how much data there is.
  618.             printf("\t%25s: Binary data %d bytes of data\n",
  619.                     scalar.pszName, scalar.dwLen);
  620.             break;
  621.         }
  622.     }
  623.  
  624.     // Why did we exit (other than no more scalars)?
  625.     // ---------------------------------------------
  626.     if (stat != SMS_NO_MORE_DATA) {
  627.         DisplaySmsError("Bad return from Scalar access", stat);
  628.     }
  629.  
  630. }
  631.  
  632.  
  633.  
  634. // ********************************************************************
  635. //      Helper functions.
  636. // ********************************************************************
  637.  
  638.  
  639. // ====================================================================
  640. //  Convert time string to time_t. Primitive checking only.
  641. //  If there are any errors we print a wanring and set the date/time to
  642. //  today.
  643. // ====================================================================
  644. time_t ReadTime( char szTime[] )
  645. {
  646.     int mon, day, year, hour, min;
  647.     time_t today;
  648.     time( &today );
  649.  
  650.     // Get the month (1..12)
  651.     mon = (szTime[0] - '0') * 10 + (szTime[1]) - '0';
  652.     mon--;
  653.     if (mon < 0 || mon > 11) {
  654.         printf("Month wrong (valid 01-12)\n");
  655.         return(today);
  656.     }
  657.  
  658.     // Day (1..31)
  659.     day = (szTime[3] - '0') * 10 + (szTime[4] - '0');
  660.     if (day < 1 || day > 31) {
  661.         printf("Day wrong (Valid 01-07)\n");
  662.         return(today);
  663.     }
  664.  
  665.     // Year
  666.     year = (szTime[6] - '0') * 10 + (szTime[7] - '0');
  667.     if (year < 90 || year > 99) {
  668.         printf("Year wrong (Valid 90-99)\n");
  669.         return(today);
  670.     }
  671.  
  672.     // Hour (0..23)
  673.     hour = (szTime[9] - '0') * 10 + (szTime[10] - '0');
  674.     if (hour < 0 || hour > 23) {
  675.         printf("Hour wrong (Valid 00-23)\n");
  676.         return(today);
  677.     }
  678.  
  679.     // Minute (0..59)
  680.     min = (szTime[12] - '0') * 10 + (szTime[13] - '0');
  681.     if (min < 0 || min > 59) {
  682.         printf("Minute wrong (Valid 00-59)\n");
  683.         return(today);
  684.     }
  685.  
  686.     // Format the time, we want a time_t.
  687.     struct tm theTime;
  688.     theTime.tm_mon   = mon;
  689.     theTime.tm_mday  = day;
  690.     theTime.tm_year  = year;
  691.     theTime.tm_hour  = hour;
  692.     theTime.tm_min   = min;
  693.     theTime.tm_sec   = 0;
  694.     theTime.tm_isdst = -1;
  695.  
  696.     time_t time = mktime( &theTime );
  697.     return(time);
  698.  
  699. }
  700.  
  701.  
  702. //**************************************************************************
  703. // InputString
  704. //
  705. // Prompt the user to input a string and return the string in the
  706. // specified buffer.
  707. //
  708. // Parameters:
  709. //      const char* pszMessage
  710. //          The user prompt to display.
  711. //
  712. //      char* pszResult
  713. //          Pointer to the buffer where the user's input will be returned.
  714. //
  715. // Returns;
  716. //      The user's input is returned via the given buffer.
  717. //***************************************************************************
  718. void InputString(const char* pszMessage, char* pszResult)
  719. {
  720.     printf("%s: ", pszMessage);
  721.     gets(pszResult);
  722. }
  723.  
  724.  
  725.  
  726.  
  727. //**************************************************************************
  728. // ConnectToDatasource
  729. //
  730. // Get the datasource connection information from the user and use it
  731. // to connect to the datasource.
  732. //
  733. // Parameters:  None.
  734. //
  735. // Returns:
  736. //      The connection handle or NULL if the connection failed.
  737. //***************************************************************************
  738. HANDLE ConnectToDatasource()
  739. {
  740.     // Get the information we need to connect to the
  741.     // data source from the user.
  742.     //==============================================
  743.     char szServer[CCH_MAXINPUT];
  744.     char szUser[CCH_MAXINPUT];
  745.     char szPasswd[CCH_MAXINPUT];
  746.     char szDatabase[CCH_MAXINPUT];
  747.  
  748.     printf("\n");
  749.     printf("**************************\n");
  750.     printf("* Connect to data source *\n");
  751.     printf("**************************\n");
  752.     InputString("SQL server name", szServer);
  753.     InputString("SQL database name", szDatabase);
  754.     InputString("User name for SQL server", szUser);
  755.     InputString("Password for SQL server", szPasswd);
  756.     printf("\n");
  757.  
  758.  
  759.     // Connect to a data source. SQL in this case.
  760.     // ===========================================
  761.     DATASOURCE dsParams;
  762.  
  763.     dsParams.sqlParams.ds          = DB_SQL;
  764.     dsParams.sqlParams.pszServer   = szServer;
  765.     dsParams.sqlParams.pszUserName = szUser;
  766.     dsParams.sqlParams.pszPasswd   = szPasswd;
  767.     dsParams.sqlParams.pszDbName   = szDatabase;
  768.     dsParams.sqlParams.pFunc       = NULL;         // No encryption.
  769.     dsParams.sqlParams.pszKey      = "";
  770.  
  771.     HANDLE hConnect;
  772.     SMS_STATUS stat;
  773.     stat = SmsDataSourceConnect( &dsParams, &hConnect);
  774.  
  775.     if (stat != SMS_OK) {
  776.         hConnect = NULL;
  777.         DisplaySmsError("Connect to data source failed", stat);
  778.     }
  779.  
  780.     return( hConnect );
  781. }
  782.  
  783.  
  784.  
  785.  
  786. //**********************************************************
  787. // DidRequestHelp
  788. //
  789. // Check the program's arguments to see if the user asked for
  790. // the help screen to be displayed.
  791. //
  792. // Parameters:
  793. //      int argc
  794. //          The argc value from main(argc, argv)
  795. //
  796. //      char** argv
  797. //          The argv value from main(argc, argv)
  798. //
  799. // Returns:
  800. //      TRUE if command line parameters included a request
  801. //      for help to be displayed.
  802. //
  803. //***********************************************************
  804. BOOL DidRequestHelp(int argc, char** argv)
  805. {
  806.     const char* pszCommand = argv[1];
  807.     if (argc==2  && (strcmp((const char*) argv[1], "-help")==0)) {
  808.         return(TRUE);
  809.     }
  810.     else {
  811.         return(FALSE);
  812.     }
  813. }
  814.  
  815.  
  816.  
  817.  
  818. //**********************************************************************
  819. // DisplayHelp
  820. //
  821. // This function displays the samples help screen.
  822. //
  823. // Parameters:
  824. //      None
  825. //
  826. // Returns:
  827. //      Nothing.
  828. //
  829. //*********************************************************************
  830. void DisplayHelp()
  831. {
  832.     printf("\n\n");
  833.     printf("***********************************************************\n");
  834.     printf("* newjob.exe:                                             *\n");
  835.     printf("*                                                         *\n");
  836.     printf("* SMS API Example: view, create, and delete job folders.  *\n");
  837.     printf("*                                                         *\n");
  838.     printf("* This sample illustrates the use of the SMS APIs in      *\n");
  839.     printf("* order to view create, and delete job folders.           *\n");
  840.     printf("* There are four types of job folders:                    *\n");
  841.     printf("*         Run command on Workstation job                  *\n");
  842.     printf("*         Share package on Server job                     *\n");
  843.     printf("*         Remove package from Server job                  *\n");
  844.     printf("*         System job                                      *\n");
  845.     printf("* All of these except system jobs can be created and      *\n");
  846.     printf("* deleted.                                                *\n");
  847.     printf("*                                                         *\n");
  848.     printf("* Syntax:                                                 *\n");
  849.     printf("*     newjob.exe [-help]                                  *\n");
  850.     printf("*                                                         *\n");
  851.     printf("* Switches:                                               *\n");
  852.     printf("*     -help       Display this help message.              *\n");
  853.     printf("*                                                         *\n");
  854.     printf("***********************************************************\n");
  855.     printf("\n");
  856. }
  857.  
  858.  
  859.  
  860. //*******************************************************************
  861. // DisplayGreeting
  862. //
  863. // Display the initial greeting banner.
  864. //
  865. // Parameters:
  866. //     None.
  867. //
  868. // Returns:
  869. //     Nothing.
  870. //******************************************************************
  871. void DisplayGreeting()
  872. {
  873.     // For this sample, the greeting is identical to the help screen.
  874.     //===============================================================
  875.     DisplayHelp();
  876. }
  877.  
  878.  
  879.  
  880.  
  881. /* EOF: newjob.cpp */
  882.