private static object _lockObj = new object(); public GuiConnection Connection { get; set; } public GuiSession Session { get; set; } internal GuiApplication GetGuiSAPApp(int timeOut = 10) { CSapROTWrapper sapROTWrapper = new CSapROTWrapper(); return GetSAPGuiApp(sapROTWrapper, 10); } private GuiApplication GetSAPGuiApp(CSapROTWrapper sapROTWrapper,int secondsOfTimeOut) { object SapGuiRot = sapROTWrapper.GetROTEntry("SAPGUI"); if (secondsOfTimeOut < 0) { throw new TimeoutException("获取SAPGUI Application超时时间不能小于0。"); } else { if (SapGuiRot == null) { System.Threading.Thread.Sleep(1000); return GetSAPGuiApp(sapROTWrapper, secondsOfTimeOut - 1); } else { object engine = SapGuiRot.GetType().InvokeMember("GetSCriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuiRot, null); if (engine == null) { throw new NullReferenceException("SAPGUI Application没有发现。"); } return engine as GuiApplication; } } } public void OpenConnection(string server) { lock (_lockObj) { var Application = GetGuiSAPApp(10); try { Application.OpenConnectionByConnectionString(server); } catch(Exception ex) { throw new Exception("连接异常,查看端口或者Host是否正确。"+ex.Message); } var index = Application.Connections.Count - 1; Connection = Application.Children.ElementAt(index) as GuiConnection; index = Connection.Sessions.Count - 1; if (Connection.Sessions.Count == 0) { throw new Exception("新会话没有发现,SAP客户端是否开启了脚本?"); } Session = Connection.Children.Item(index) as GuiSession; } } public bool Login(string UserName,string Password,string Client,string Language = "") { (Session.FindById("wnd[0]/usr/txtRSYST-BNAME") as GuiTextField).Text = UserName; (Session.FindById("wnd[0]/usr/pwdRSYST-BCODE") as GuiTextField).Text = Password; (Session.FindById("wnd[0]") as GuiFrameWindow).SendVKey(0); return true; }
5)实际使用
OpenConnection("server"); //
Login("usreName", "Password", "");
6)其它部分可以参照login里面的进行修改
标签:SAPscript,string,sapROTWrapper,C#,Application,Session,new,SAP,方法 From: https://www.cnblogs.com/wonder223/p/17665479.html