{ session.pas } program Session; {$IfDef VER80} uses SnipTool, SnipData, SysUtils, WinTypes, WinProcs, DbiProcs, DbiTypes, DbiErrs; {$Else} uses WinTypes, WinCrt, Strings, DbiProcs, DbiTypes, DbiErrs, SnipTool, SnipData; {$EndIf} {===================================================================== Code: SessionIO(); Input: None. Return: None. Description: This example will demonstrate how to start more than one one session. The example also shows how to case between sessions. ===================================================================== } var rslt: DBIResult; { Value returned from IDAPI functions } i: UINT16; { Loop variable } const phSession: ^hDBISes = nil; { Handle to the session } pSesInf: pSESInfo = nil; { Session descriptor } uNumOfSessions: UINT16 = 4; { The number of sessions to open } begin { Inform user that the example is running } Screen('*** Session I/O Example ***'); Screen(' Initializing IDAPI...'); InitOutput; rslt := ChkRslt(DbiInit(nil), DBIERR_NONE, ' Error - Init'); if (rslt <> DBIERR_NONE) then { Check if successfull } begin Screen(''); Screen('*** End of Example ***'); CloseOutput; exit; end; { Allocate memory for the uNumOfSessions amount of sessions. These will be pointers to sessions once we start them. } GetMem(phSession, uNumOfSessions * sizeof(hDBISes)); if not Assigned (phSession) then begin Screen(' Error - Out of memory.'); ChkRslt(DbiExit, DBIERR_NONE, ' Error - Exit.'); Screen(''); Screen('*** End of Example ***'); CloseOutput; exit; end; { Allocate memory for the session info pointers. } GetMem(pSesInf, uNumOfSessions * sizeof(SESInfo)); if not Assigned (pSesInf) then begin Screen(' Error - Out of memory.'); ChkRslt(DbiExit, DBIERR_NONE, ' Error - Exit.'); Screen(''); Screen('*** End of Example ***'); CloseOutput; exit; end; { Acquire the initial session handle. A session is started for you when the engine is initialized. } Screen(' Getting session #1 (opened automatically by DbiInit)...'); ChkRslt(DbiGetCurrSession((phSession^)), DBIERR_NONE, ' Error - GetCurrSession.'); { Starts new sessions and display the number of open sessions on the screen. Having already opened the first one, start the second session through the uNumOfSessions - 1 session. } for i := 1 to (uNumOfSessions - 1) do begin Inc(phSession); Screen(''); Screen(' Opening session #'+IntToStr(i+1)); ChkRslt(DbiStartSession('', phSession^, nil), DBIERR_NONE, ' Error - StartSession.'); end; Dec(phSession,i); { Now itterate through each of the sessions and will get the current settings. The settings include the session number, the number of open databases and the number of open cursors among other things. } for i := 1 to uNumOfSessions do begin Screen(''); Screen(' Setting session #'+IntToStr(i)+ ' as the current session...'); ChkRslt(DbiSetCurrSession(phSession^), DBIERR_NONE, ' Error - SetCurrSession.'); Screen(' Getting Session information...'); ChkRslt(DbiGetSesInfo(pSesInf^), DBIERR_NONE, ' Error - GetSesInfo.'); Screen(' Open databases: '+IntToStr(pSesInf^.iDatabases)+ ', Open Cursors: '+IntToStr(pSesInf^.iCursors)); Inc(phSession); end; Dec(phSession,i); { Close all the session and then close IDAPI. } Screen(''); Screen(' We cannot yet close session #1 as it is the default'+ ' session.'); Screen(' We will therefore close all other sessions and then close'+ ' session #1.'); for i := 1 to (uNumOfSessions-1) do begin { We cannot close Session #1 so we will close session #2 and on. We cannot close session #1 because it is the default session (created with DbiInit.) } Inc(phSession); Screen(''); Screen(' Closing session #'+IntToStr(i+1)+'...'); ChkRslt(DbiCloseSession(phSession^), DBIERR_NONE, ' Error - CloseSession.'); end; { Release the memory used to hold the session handles. } FreeMem(phSession, uNumOfSessions * sizeof(hDBISes)); { Release the memory used to store information about the } FreeMem(pSesInf, uNumOfSessions * sizeof(SESInfo)); Screen(''); Screen(' Closing session #1 (done during DbiExit)'); Screen(''); Screen(' Clean up IDAPI...'); ChkRslt(DbiExit, DBIERR_NONE, ' Error - Exit.'); Screen(''); CloseOutput; Screen('*** End of Example ***'); exit; end.