home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / 32SNIPIT.PAK / SESSION.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  4.2 KB  |  126 lines

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // session.c
  4. #include "snipit.h"
  5.  
  6. //=====================================================================
  7. //  Function:
  8. //          SessionIO();
  9. //
  10. //  Description:
  11. //          This example will demonstrate how to start more than one
  12. //          session.  The example also shows how to switch between
  13. //          sessions.
  14. //=====================================================================
  15. void
  16. SessionIO (void)
  17. {
  18.     DBIResult  rslt;                // Value returned from IDAPI functions
  19.     phDBISes   phSession;           // Handle to the session
  20.     pSESInfo   pSesInfo;            // Session descriptor
  21.     UINT16     i;                   // Loop variable                     
  22.     const UINT uNumOfSessions = 4 ; // The number of sessions to open
  23.  
  24.     Screen("*** Session I/O Example ***\r\n");
  25.  
  26.     BREAK_IN_DEBUGGER();
  27.  
  28.     Screen("    Initializing IDAPI...\r\n");
  29.  
  30.     rslt = DbiInit(NULL);
  31.     if (ChkRslt(rslt, "Init") != DBIERR_NONE)
  32.     {
  33.         Screen("\r\n*** End of Example ***");
  34.         return;
  35.     }
  36.  
  37.     // Allocate memory for uNumOfSessions sessions.  These
  38.     // will be pointers to sessions once we start them.
  39.     phSession = (phDBISes) malloc(uNumOfSessions * sizeof(hDBISes));
  40.     if (phSession == NULL)
  41.     {
  42.         Screen("    Error - Out of memory");
  43.  
  44.         rslt = DbiExit();
  45.         ChkRslt(rslt, "Exit");
  46.  
  47.         Screen("\r\n*** End of Example ***");
  48.         return;
  49.     }
  50.  
  51.     // Allocate memory for the session info pointers.
  52.     pSesInfo = (pSESInfo) malloc(uNumOfSessions * sizeof(SESInfo));
  53.     if (pSesInfo == NULL)
  54.     {
  55.         Screen("    Error - Out of memory");
  56.         rslt = DbiExit();
  57.         ChkRslt(rslt, "Exit");              
  58.         Screen("\r\n*** End of Example ***");
  59.         return;
  60.     }
  61.  
  62.     // Acquire the initial session handle.  A session is started for you 
  63.     // when the engine is initialized. 
  64.     Screen("    Getting session #1 (opened automatically by DbiInit)...");
  65.  
  66.     rslt = DbiGetCurrSession( &(phSession[0]) );
  67.     ChkRslt(rslt, "GetCurrSession");
  68.  
  69.     // Start new sessions and display the number of open sessions on the
  70.     // screen. Having already opened the first one, start the second
  71.     // session through the uNumOfSessions - 1 session.
  72.     for (i=0; i<(uNumOfSessions - 1); i++)
  73.     {
  74.         Screen("\r\n    Opening session #%d...",i+2);
  75.  
  76.         rslt = DbiStartSession("", &(phSession[i+1]), NULL);
  77.         ChkRslt(rslt, "StartSession");
  78.     }
  79.  
  80.     // Now iterate through each of the sessions and get the current
  81.     // settings.  The settings include the session number, the number of
  82.     // open databases and the number of open cursors, among other things.
  83.     for (i=0; i<uNumOfSessions; i++)
  84.     {
  85.         Screen("\r\n    Setting session #%d as the current session...",i+1);
  86.         rslt = DbiSetCurrSession(phSession[i]);
  87.         ChkRslt(rslt, "SetCurrSession");
  88.  
  89.         Screen("    Getting Session information...");
  90.         rslt = DbiGetSesInfo( &(pSesInfo[i]) );
  91.         ChkRslt(rslt, "GetSesInfo");
  92.  
  93.         Screen("    Open databases: %d, Open Cursors: %d",
  94.                pSesInfo[i].iDatabases, pSesInfo[i].iCursors);
  95.     }
  96.  
  97.     // Close the sessions
  98.     Screen("\r\n    We cannot yet close session #1 as it is the default"
  99.            " session");
  100.     Screen("    We will therefore close all other sessions and then close"
  101.            " session #1");
  102.     for (i=0; i<(uNumOfSessions-1); i++)
  103.     {
  104.         // We cannot close Session #1 so we will close session #2
  105.         // and on.  We cannot close session #1 because it is the
  106.         // default session (created with DbiInit.)
  107.         Screen("\r\n    Closing session #%d...",i+2);
  108.  
  109.         rslt = DbiCloseSession(phSession[i+1]);
  110.         ChkRslt(rslt, "CloseSession");
  111.     }
  112.  
  113.     // Release the memory used to hold the session handles.
  114.     free(phSession);
  115.  
  116.     // Release the memory used to store information about the session
  117.     free(pSesInfo);
  118.  
  119.     Screen("\r\n    Closing session #1 (done during DbiExit)");
  120.     Screen("\r\n    Clean up IDAPI...");
  121.     rslt = DbiExit();
  122.     ChkRslt(rslt, "Exit");
  123.  
  124.     Screen("\r\n*** End of Example ***");
  125. }
  126.