Handling Multiple Sessions 

Use

You can use up to six sessions for every R/3 connection you have. The first session is always created when you establish a connection to the R/3 application server.

If SAPGUI Front is running you can only have one connection.

 

The following procedure shows how to handle multiple R/3 sessions.

Procedure

  1. Ask for a new connection to R/3, by using the It_NewConnection function.

This establishes a connection to the R/3 application server, returns a connection handle, and starts the first SAP session.

You can optionally specify that the standard SAPGUI is to be displayed. In this case, a SAPGUI and one Front are invoked.

  1. Use It_Login to log the user or the program onto the R/3 system.
  1. Register a callback function to watch for a new session being created, by using the It_SetNewSessionHook function.

The new session can be created as a result of an end user asking for it, by using the menu option SystemàCreate session, for example.

You can also create a new session programmatically in one of the following ways:

  1. Once the R/3 application server creates a new session, regardless of the reason, you need to capture the handle to the new session into a variable.
  2. You do so within the callback handler function for the It_SetNewSessionHook function.

  3. Register a callback function to watch for the deletion of any session, by using the It_SetDelSessionHook function.

You can use the ItEv_GetSessionCount function at any time to check the number of sessions that are open.

Example

The following example handles two sessions.

The main program handles the connection to R/3 and handles the first session. In the first session, the program invokes a specific screen in a specific transaction (screen 410 in transaction bibs). The session handle is kept in the hMr variable.

The newses function gets called when R/3 gets a request for a new session. In the second session, the program invokes another R/3 transaction, namely transaction se38.

The handle of the second session is kept in the hMr2 variable. If you wish to handle more than two sessions, use an array of variables, instead of the single variable hMr2.

#include <stdio.h>
#include "guilib.h"
#include <winbase.h>

static int newses(HANDLE ptr);

HANDLE hMr2;
IT_EVENT *pEvt2;
int main()
{

char *server = "myhost";
char *system = "MYSYS";
char *client = "005";

char *user = "***";
char *passwd = "***";
char *lang = "e";

HANDLE hMr;
IT_EVENT *pEvt;

hMr = It_NewConnection(server, system, SAPGUI_FRONT);

if(hMr)
{
It_SetNewSessionHook((HANDLE)hMr, newses);
}

if (hMr == NULL) {
exit(1);
}

It_GetEvent(hMr, &pEvt);

It_Login(hMr, client, user, passwd, lang);
It_GetEvent(hMr, &pEvt);

ItEv_SetOKCode(pEvt, "bibs");
It_SendEvent(hMr, &pEvt);
It_GetEvent(hMr, &pEvt);

ItEv_SetOKCode(pEvt, "410");
It_SendEvent(hMr, &pEvt);

while (1) {

It_GetEventEx(hMr, &pEvt, GV_GETFRONTEVENT);

if (pEvt->eventtype & EVT_SEND_FRONT_EVT_REQ) {
It_SendEvent(hMr, &pEvt);
}

int rc = It_GetEvent(hMr, &pEvt);
if (!rc)
break;

if (pEvt->eventtype & EVT_END_OF_SESSION) {
break;
}

Sleep (1000);
if (hMr2)
{
It_GetEvent(hMr2, &pEvt2);
ItEv_SetOKCode(pEvt2, "se38");
It_SendEvent(hMr2, &pEvt2);
It_GetEvent(hMr2, &pEvt2);
}
}

return 0;
}

static int newses(HANDLE ptr)
{
printf("new session <0x%lx>\n", ptr);
hMr2 = (HANDLE) ptr;
return TRUE;
}