private static GuiApplication getSAPGuiApp(CSapROTWrapper sapROTWrapper, int secondsOfTimeout) { object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI"); if (secondsOfTimeout < 0) { throw new TimeoutException(string.Format("Can get sap script engine in {0} seconds", secondsOfTimeout)); } else { if (SapGuilRot == null) { if (secondsOfTimeout > 0) { System.Threading.Thread.Sleep(1000); return(getSAPGuiApp(sapROTWrapper, secondsOfTimeout - 1)); } else { return(null); } } else { object engine = SapGuilRot.GetType().InvokeMember("GetSCriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuilRot, null); if (engine == null) { throw new NullReferenceException("No SAP GUI application found"); } return(engine as GuiApplication); } } }
public static GuiConnection CreateConnection(string description, string user, string password, string language) { CSapROTWrapper sapROTWrapper = new CSapROTWrapper(); object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI"); object engine = SapGuilRot.GetType().InvokeMember("GetSCriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuilRot, null); GuiApplication app = (GuiApplication)engine; GuiConnection conn; if (app.Connections.Count == 0) { conn = app.OpenConnection(description); GuiSession session = conn.Children.ElementAt(0) as GuiSession; GuiTextField name = (GuiTextField)session.FindById("wnd[0]/usr/txtRSYST-BNAME"); GuiPasswordField pass = (GuiPasswordField)session.FindById("wnd[0]/usr/pwdRSYST-BCODE"); GuiTextField lang = (GuiTextField)session.FindById("wnd[0]/usr/txtRSYST-LANGU"); GuiMainWindow mainWindows = (GuiMainWindow)session.FindById("wnd[0]"); name.Text = user; pass.Text = password; lang.Text = language; mainWindows.SendVKey(0); } else { conn = (GuiConnection)app.Connections.ElementAt(0); } return(conn); }
private GuiApplication GetApplication() { if (application == null) { CSapROTWrapper sapRotWrapper = new CSapROTWrapper(); object sapGuilRot = sapRotWrapper.GetROTEntry("SAPGUI"); if (sapGuilRot == null) { throw new NoSapGuiFoundException("No ROT object found - is SAP GUI running?"); } object engine = sapGuilRot.GetType().InvokeMember( "GetScriptingEngine", BindingFlags.InvokeMethod, null, sapGuilRot, null ); if (engine == null) { throw new NoSapGuiFoundException("No engine - is SAP GUI Scripting enabled?"); } application = (GuiApplication)engine; } return(application); }
public GuiSession GetSession() { CSapROTWrapper sapRotWrapper = new CSapROTWrapper(); object sapGuilRot = sapRotWrapper.GetROTEntry("SAPGUI"); if (sapGuilRot == null) { throw new NoSapGuiFoundException("No rot object found - is Sap Gui running?"); } object engine = sapGuilRot.GetType().InvokeMember( "GetScriptingEngine", BindingFlags.InvokeMethod, null, sapGuilRot, null); if (engine == null) { throw new NoSapGuiFoundException("no engine - wie kann das sein?"); } GuiApplication sapGuiApp = engine as GuiApplication; GuiConnection connection = sapGuiApp?.Children.Cast <GuiConnection>().ElementAt(index); if (connection == null) { throw new NoSapGuiFoundException("no connection - Are you logged into any system?"); } foreach (SAPFEWSELib.GuiSession session in connection.Sessions) { return(new SapGuiSession(session)); } return(null); }
/// <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); }
private static GuiConnection GetConnection() { CSapROTWrapper sapROTWrapper = new CSapROTWrapper(); object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI"); object engine = SapGuilRot.GetType().InvokeMember("GetSCriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuilRot, null); return((engine as GuiApplication).Connections.ElementAt(0) as GuiConnection); }
public static GuiSession GetObjSessionById(string id) { CSapROTWrapper sapROTWrapper = new CSapROTWrapper(); object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI"); object engine = SapGuilRot.GetType().InvokeMember("GetSCriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuilRot, null); GuiConnection connection = (engine as GuiApplication).Connections.ElementAt(0) as GuiConnection; GuiSession session = connection.FindById(id) as GuiSession; return(session); }
private static void ActivateSap() { //Get the Windows Running Object Table var sapROTWrapper = new CSapROTWrapper(); //Get the ROT Entry for the SAP Gui to connect to the COM object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI"); //Get the reference to the Scripting Engine var engine = SapGuilRot.GetType() .InvokeMember("GetScriptingEngine", BindingFlags.InvokeMethod, null, SapGuilRot, null); //Get the reference to the running SAP Application Window var GuiApp = (GuiApplication)engine; //Get the reference to the first open connection var connection = (GuiConnection)GuiApp.Connections.ElementAt(0); //get the first available session session = (GuiSession)connection.Children.ElementAt(0); //Get the reference to the main "Frame" in which to send virtual key commands //GuiFrameWindow frame = (GuiFrameWindow)session.FindById("wnd[0]"); }
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); }
public string login_SSO(string servername) { //Close the SAP if its opened, then open the new process KillProcess("saplogon"); //Set the Exe Path and SAP Window name string strSAPLoginPadPath = @"C:\Program Files (x86)\SAP\FrontEnd\SapGui\saplogon.exe"; FileVersionInfo fi2 = FileVersionInfo.GetVersionInfo(strSAPLoginPadPath); string strVersion = fi2.ProductVersion.Substring(0, 3); String strloginWindowName = "SAP Logon " + strVersion; //Start SAP Login Window StartProcess(strSAPLoginPadPath); //Wait for the window to be loaded. System.Threading.Thread.Sleep(1000); AutomationElement desktop = AutomationElement.RootElement; AutomationElement ele = null; while (ele is null) { try { ele = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, strloginWindowName)); } catch { } } CSapROTWrapper sapROTWrapper = new CSapROTWrapper(); object rot = sapROTWrapper.GetROTEntry("SAPGUI"); object engine = rot.GetType().InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, rot, null); GuiConnection connection = (engine as GuiApplication).OpenConnection(servername); GuiSession SapSession = connection.Children.ElementAt(0) as GuiSession; //Validate whether the login successfull or Not //If the error is "Already logged in by the same user, then click GuiStatusbar statusBar = (GuiStatusbar)SapSession.FindById("wnd[0]/sbar"); string strMsg = statusBar.Text; string strMsgType = statusBar.MessageType; if (strMsg.Contains("User already logged on at another terminal") == true || strMsg.Contains("用户已经登录到其它终端") == true)//(strMsg == "W" || strMsgType == "E" ) //'Warning or Error { //Check whether is it showing the same terminal or different terminal string strWinText = ((GuiFrameWindow)SapSession.FindById("wnd[1]")).Text; strWinText = Environment.MachineName; if (strWinText.Contains(Environment.MachineName) == true) { //This represents, the login in the same machine, then click continue with current login option ((GuiRadioButton)SapSession.FindById("wnd[1]/usr/radMULTI_LOGON_OPT1")).Select(); ((GuiButton)SapSession.FindById("wnd[1]/tbar[0]/btn[0]")).Press(); } else { //SapSession.ActiveWindow.Close(); throw new Exception("SAP Error while logging in the SAP system, Error:\n" + strMsg); } } else if (strMsg.Length > 0 && strMsgType == "E") { //SapSession.ActiveWindow.Close(); throw new Exception("SAP Error while logging in the SAP system, Error:\n" + strMsg); } return("success"); }
public GuiSession getCurrentSession(String MainWindowName = "") { ////GuiApplication appSAP = new GuiApplication(); //GuiApplication appSAP = (GuiApplication)Marshal.GetActiveObject("SAPGUISERVER"); //return appSAP.ActiveSession; String strLog; object rot; object engine; CSapROTWrapper sapROTWrapper; strLog = "GOING TO GET SAPGUI"; try { sapROTWrapper = new CSapROTWrapper(); rot = sapROTWrapper.GetROTEntry("SAPGUI"); strLog = strLog + Environment.NewLine + "GOING TO GET SCRIPT ENGINE"; engine = rot.GetType().InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, rot, null); } catch (Exception e) { throw new Exception(strLog + Environment.NewLine + e.Message + e.StackTrace); throw; } GuiConnection SapconnSession; //validate whether GuiApplication has chidren or not "Engine" if (engine is null) { throw new Exception("SAP GUI Application is null"); } else { strLog = strLog + Environment.NewLine + "SAP GUI Application is not null"; if ((engine as GuiApplication).Children is null) { strLog = strLog + Environment.NewLine + "SAP GUI Application children GUI Application is null"; } else { strLog = strLog + Environment.NewLine + "SAP GUI Application has :" + (engine as GuiApplication).Children.Count + " children"; } } try { SapconnSession = (engine as GuiApplication).Children.Item(0) as GuiConnection; } catch (Exception) { try { SapconnSession = (engine as GuiApplication).Children.Item(1) as GuiConnection; } catch (Exception e) { throw new Exception(strLog + Environment.NewLine + e.Message + e.StackTrace); } } GuiSession SapSession = null; strLog = strLog + Environment.NewLine + "Get SAP Connection session"; try { strLog = strLog + Environment.NewLine + "SAP Connection Session has :" + SapconnSession.Children + " children"; if (MainWindowName != "") { for (int i = 0; i < SapconnSession.Children.Count; i++) { string strWindowName = ((SapconnSession.Children.Item(i) as GuiSession).Children.Item(0) as GuiMainWindow).Text; //Debug.Print("Main Window Name:" + strWindowName); if (strWindowName == MainWindowName) { SapSession = SapconnSession.Children.Item(i) as GuiSession; break; } //Debug.Print((SapconnSession.Children.Item(i) as GuiSession).ActiveWindow.Text); } if (SapSession == null) { throw new Exception("Could not find the window name in the available session"); } } else { try { strLog = strLog + Environment.NewLine + "Going to get First children as GuiSession"; SapSession = SapconnSession.Children.Item(0) as GuiSession; } catch (Exception) { strLog = strLog + Environment.NewLine + "Going to get Second children as GuiSession since first failed"; SapSession = SapconnSession.Children.Item(1) as GuiSession; } } } catch (Exception e) { throw new Exception(strLog + Environment.NewLine + e.Message + e.StackTrace); } //GuiSession SapSession = (rot as GuiApplication).Connections.ElementAt(0) as GuiSession; return(SapSession); }
/// <summary> /// This function will login into the SAP with provided credentials(same as manual login) /// </summary> /// <param name="SapSession"></param> /// <param name="myclient"></param> /// <param name="mylogin"></param> /// <param name="mypass"></param> /// <param name="mylang"></param> public string login(string servername, string myclient, string mylogin, System.Security.SecureString mypass, string mylang) { //GuiApplication appSAP = (GuiApplication)Marshal.GetActiveObject("SAPGUI"); //GuiConnection connSAP = appSAP.OpenConnection(server, Sync: true); //GuiSession SapSession = (GuiSession)connSAP.Sessions.Item(0); // System.IO.StreamWriter m_fswLogFile; //DateTime m_dtLastTimeLog = DateTime.MinValue; //int m_intTimeLogInterVal; //String strFilePath = "D:\\AA_Log_SAPAUto_Metabot.txt"; //if (!System.IO.File.Exists(strFilePath)) { // System.IO.File.Create(strFilePath).Close(); //} ////Open the log file //m_fswLogFile = new System.IO.StreamWriter(strFilePath, true); //m_fswLogFile.AutoFlush = true; //m_fswLogFile.WriteLine("Parameters"); //m_fswLogFile.WriteLine(server); //m_fswLogFile.WriteLine(mylogin); //m_fswLogFile.WriteLine(mypass); //m_fswLogFile.WriteLine(mylang); //m_fswLogFile.WriteLine(SAPWindowName); //m_fswLogFile.Close(); //m_fswLogFile = null; //Close the SAP if its opened, then open the new process KillProcess("saplogon"); //Set the Exe Path and SAP Window name string strSAPLoginPadPath = @"C:\Program Files (x86)\SAP\FrontEnd\SapGui\saplogon.exe"; FileVersionInfo fi2 = FileVersionInfo.GetVersionInfo(strSAPLoginPadPath); string strVersion = fi2.ProductVersion.Substring(0, 3); String strloginWindowName = "SAP Logon " + strVersion; //Open the SAP Logon Window StartProcess(strSAPLoginPadPath); //Wait for the window to be loaded. System.Threading.Thread.Sleep(1000); AutomationElement desktop = AutomationElement.RootElement; AutomationElement ele = null; while (ele is null) { try { ele = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, strloginWindowName)); } catch { } } CSapROTWrapper sapROTWrapper = new CSapROTWrapper(); object rot = sapROTWrapper.GetROTEntry("SAPGUI"); object engine = rot.GetType().InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, rot, null); GuiConnection connection = (engine as GuiApplication).OpenConnection(servername); GuiSession SapSession = connection.Children.ElementAt(0) as GuiSession; //m_fswLogFile = new System.IO.StreamWriter(strFilePath, true); //m_fswLogFile.AutoFlush = true; //m_fswLogFile.WriteLine("Going to get the controls for login"); //m_fswLogFile.Close(); //m_fswLogFile = null; GuiTextField client = (GuiTextField)SapSession.ActiveWindow.FindByName("RSYST-MANDT", "GuiTextField"); GuiTextField login = (GuiTextField)SapSession.ActiveWindow.FindByName("RSYST-BNAME", "GuiTextField"); GuiTextField pass = (GuiTextField)SapSession.ActiveWindow.FindByName("RSYST-BCODE", "GuiPasswordField"); GuiTextField language = (GuiTextField)SapSession.ActiveWindow.FindByName("RSYST-LANGU", "GuiTextField"); //m_fswLogFile = new System.IO.StreamWriter(strFilePath, true); //m_fswLogFile.AutoFlush = true; //m_fswLogFile.WriteLine("Enter credentials for login"); //m_fswLogFile.Close(); //m_fswLogFile = null; client.SetFocus(); client.Text = myclient; login.SetFocus(); login.Text = mylogin; pass.SetFocus(); pass.Text = new System.Net.NetworkCredential(string.Empty, mypass).Password; language.SetFocus(); language.Text = mylang; //m_fswLogFile = new System.IO.StreamWriter(strFilePath, true); //m_fswLogFile.AutoFlush = true; //m_fswLogFile.WriteLine("click login"); //m_fswLogFile.Close(); //m_fswLogFile = null; //Press the green checkmark button which is about the same as the enter key GuiButton btn = (GuiButton)SapSession.FindById("/app/con[0]/ses[0]/wnd[0]/tbar[0]/btn[0]"); btn.SetFocus(); btn.Press(); //Validate whether the login successfull or Not //If the error is "Already logged in by the same user, then click GuiStatusbar statusBar = (GuiStatusbar)SapSession.FindById("wnd[0]/sbar"); string strMsg = statusBar.Text; string strMsgType = statusBar.MessageType; if (strMsg.Contains("User already logged on at another terminal") == true || strMsg.Contains("用户已经登录到其它终端") == true)//(strMsg == "W" || strMsgType == "E" ) //'Warning or Error { //Check whether is it showing the same terminal or different terminal string strWinText = ((GuiFrameWindow)SapSession.FindById("wnd[1]")).Text; strWinText = Environment.MachineName; if (strWinText.Contains(Environment.MachineName) == true) { //This represents, the login in the same machine, then click continue with current login option ((GuiRadioButton)SapSession.FindById("wnd[1]/usr/radMULTI_LOGON_OPT1")).Select(); ((GuiButton)SapSession.FindById("wnd[1]/tbar[0]/btn[0]")).Press(); } else { SapSession.ActiveWindow.Close(); return("SAP Error while logging in the SAP system, Error:\n" + strMsg); } } else if (strMsg.Length > 0 && strMsgType == "E") { SapSession.ActiveWindow.Close(); return("SAP Error while logging in the SAP system, Error:\n" + strMsg); } return("success"); }