/// <summary>Return an already opened sap session</summary> public static GuiSession GetActiveSession() { var rot = new CSapROTWrapper().GetROTEntry("SAPGUI"); if (rot == null) { const string Message = "The application can no connect to SAP. Make sure SAP is up and running, and then try again."; Logger.Instance.Fatal("The application can no connect to SAP"); throw new InvalidComObjectException(Message); } var app = (GuiApplication)rot.GetType().InvokeMember("GetScriptingEngine", BindingFlags.InvokeMethod, null, rot, null); var connectedSession = app.Connections.Cast <GuiConnection>() .SelectMany(x => x.Children.Cast <GuiSession>()) .Where(x => !string.IsNullOrEmpty(x.Info.User)) .FirstOrDefault(); if (connectedSession == null) { const string Message = "Could not find an opened SAP session. Make sure you are logged in, and then try again."; Logger.Instance.Fatal("Could not find an opened SAP session"); throw new InvalidComObjectException(Message); } return(connectedSession); }
public static GuiSession GetActiveSession() { var rot = new CSapROTWrapper().GetROTEntry("SAPGUI"); if (rot == null) { throw SapException.NotOpened(); } var app = (GuiApplication)rot.GetType().InvokeMember("GetScriptingEngine", BindingFlags.InvokeMethod, null, rot, null); var connectedSession = app.Connections.Cast <GuiConnection>() .SelectMany(x => x.Children.Cast <GuiSession>()) .Where(x => !string.IsNullOrEmpty(x.Info.User)) .FirstOrDefault(); if (connectedSession == null) { throw SapException.NotOpened(); } return(connectedSession); }