home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / snipit.pak / SESSION.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  4.5 KB  |  127 lines

  1. // BDE - (C) Copyright 1995 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 = NULL;    // Handle to the session
  20.     pSESInfo   pSesInfo  = NULL;    // 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.     // Turn on trace information if the debug layer is anabled (DLLSWAP.EXE).
  38.     DbiDebugLayerOptions(DEBUGON | OUTPUTTOFILE, "SNIPIT.INF");
  39.  
  40.     // Specify where temporary files are placed.
  41.     rslt = DbiSetPrivateDir(szPrivDirectory);
  42.     ChkRslt(rslt, "SetPrivateDir");
  43.     
  44.     // Allocate memory for uNumOfSessions sessions.  These
  45.     //   will be pointers to sessions once we start them.
  46.     phSession = (phDBISes) malloc(uNumOfSessions * sizeof(hDBISes));
  47.     // Allocate memory for the session information pointers.
  48.     pSesInfo = (pSESInfo) malloc(uNumOfSessions * sizeof(SESInfo));
  49.     if ((phSession == NULL) || (pSesInfo == NULL))
  50.     {
  51.         if (phSession) free(phSession);
  52.         if (pSesInfo) free(pSesInfo);
  53.         Screen("    Error - Out of memory");
  54.         DbiDebugLayerOptions(0, NULL);
  55.         rslt = DbiExit();
  56.         ChkRslt(rslt, "Exit");              
  57.         Screen("\r\n*** End of Example ***");
  58.         return;
  59.     }
  60.  
  61.     // Acquire the initial session handle.  A session is started for you 
  62.     //   when the engine is initialized. 
  63.     Screen("    Getting session #1 (opened automatically by DbiInit)...");
  64.  
  65.     rslt = DbiGetCurrSession( &(phSession[0]) );
  66.     ChkRslt(rslt, "GetCurrSession");
  67.  
  68.     // Start new sessions and display the number of open sessions on the
  69.     //   screen. Having already opened the first one, start the second
  70.     //   session through the uNumOfSessions - 1 session.
  71.     for (i=0; i<(uNumOfSessions - 1); i++)
  72.     {
  73.         Screen("\r\n    Opening session #%d...",i+2);
  74.         rslt = DbiStartSession("", &(phSession[i+1]), NULL);
  75.         ChkRslt(rslt, "StartSession");
  76.     }
  77.  
  78.     // Now iterate through each of the sessions and get the current
  79.     //   settings.  The settings include the session number, the number of
  80.     //   open databases and the number of open cursors, among other things.
  81.     for (i=0; i<uNumOfSessions; i++)
  82.     {
  83.         Screen("\r\n    Setting session #%d as the current session...",i+1);
  84.         rslt = DbiSetCurrSession(phSession[i]);
  85.         ChkRslt(rslt, "SetCurrSession");
  86.  
  87.         Screen("    Getting session information...");
  88.         rslt = DbiGetSesInfo( &(pSesInfo[i]) );
  89.         ChkRslt(rslt, "GetSesInfo");
  90.  
  91.         Screen("    Open databases: %d, Open cursors: %d",
  92.                pSesInfo[i].iDatabases, pSesInfo[i].iCursors);
  93.     }
  94.  
  95.     // Close the sessions.
  96.     Screen("\r\n    We cannot yet close session #1 as it is the default"
  97.            " session");
  98.     Screen("    We will therefore close all other sessions and then close"
  99.            " session #1");
  100.     for (i=0; i<(uNumOfSessions-1); i++)
  101.     {
  102.         // We cannot close session #1 so we will close session #2
  103.         //   and on.  We cannot close session #1 because it is the
  104.         //   default session (created with DbiInit.)
  105.         Screen("\r\n    Closing session #%d...",i+2);
  106.  
  107.         rslt = DbiCloseSession(phSession[i+1]);
  108.         ChkRslt(rslt, "CloseSession");
  109.     }
  110.  
  111.     // Release the memory used to hold the session handles.
  112.     free(phSession);
  113.  
  114.     // Release the memory used to store information about the session.
  115.     free(pSesInfo);
  116.  
  117.     // Turn off trace information.
  118.     DbiDebugLayerOptions(0, NULL);
  119.     
  120.     Screen("\r\n    Closing session #1 (done during DbiExit)");
  121.     Screen("\r\n    Clean up IDAPI...");
  122.     rslt = DbiExit();
  123.     ChkRslt(rslt, "Exit");
  124.  
  125.     Screen("\r\n*** End of Example ***");
  126. }
  127.