自动登录
首先需要安装SapGui 客户端,然后登录到sap,开启脚本,否则无法访问。
添加引用程序集,找到com,勾中Sap Gui Scripting Api和SapROTWr 1.0 Type Libaary,然后点击确定。然后会看到 SAPFEWSELib;和SapROTWr这个程序集。
代买如下:
1 private static object _lockObj = new object(); 2 public GuiConnection Connection { get; set; } 3 public GuiSession Session { get; set; } 4 5 /// <summary> 6 /// 连接到SAPGui 7 /// </summary> 8 /// <param name="env"></param> 9 public void OpenConnection(string server) 10 { 11 lock (_lockObj) 12 { 13 var Application = SAPBridge.Instance.GetGuiSAPApp(10); 14 try 15 { 16 17 Application.OpenConnectionByConnectionString(server); 18 } 19 catch (Exception ex) 20 { 21 throw new Exception("连接异常,查看端口或者Host是否正确。 " + ex.Message); 22 } 23 var index = Application.Connections.Count - 1; 24 Connection = Application.Children.ElementAt(index) as GuiConnection; 25 index = Connection.Sessions.Count - 1; 26 if (Connection.Sessions.Count == 0) 27 { 28 throw new Exception("新会话没有发现, SAP客户端是否开启了脚本?"); 29 } 30 Session = Connection.Children.Item(index) as GuiSession; 31 } 32 } 33 34 /// <summary> 35 /// 登录 36 /// </summary> 37 /// <param name="UserName">用户名</param> 38 /// <param name="Password">密码</param> 39 /// <param name="Client">客户端</param> 40 /// <param name="Language">语言(如果SAPGui未安装该语言包将登录失败)</param> 41 /// <returns></returns> 42 public bool Login(string UserName, string Password, string Client, string Language = "") 43 { 44 Session.FindById<GuiTextField>("wnd[0]/usr/txtRSYST-BNAME").Text = UserName; 45 Session.FindById<GuiTextField>("wnd[0]/usr/pwdRSYST-BCODE").Text = Password; 46 Session.FindById<GuiTextField>("wnd[0]/usr/txtRSYST-MANDT").Text = Client; 47 Session.FindById<GuiTextField>("wnd[0]/usr/txtRSYST-LANGU").Text = Language; 48 var window = Session.FindById<GuiFrameWindow>("wnd[0]"); 49 window.SendVKey(0); 50 GuiStatusbar status = Session.FindById<GuiStatusbar>("wnd[0]/sbar"); 51 if (status != null && status.MessageType.ToLower() == "e") 52 { 53 Connection.CloseSession(Session.Id); 54 return false; 55 } 56 GuiRadioButton rb_Button = Session.FindById<GuiRadioButton>("wnd[1]/usr/radMULTI_LOGON_OPT2"); 57 if (rb_Button != null) 58 { 59 rb_Button.Select(); 60 window.SendVKey(0); 61 } 62 return true; 63 } 64 65 /// <summary> 66 /// 获取SAPGui Applicaiotn 67 /// </summary> 68 /// <param name="timeout">超时时间(秒)</param> 69 /// <returns></returns> 70 internal GuiApplication GetGuiSAPApp(int timeout = 10) 71 { 72 CSapROTWrapper sapROTWrapper = new CSapROTWrapper(); 73 return GetSAPGuiApp(sapROTWrapper, timeout); 74 } 75 76 private GuiApplication GetSAPGuiApp(CSapROTWrapper sapROTWrapper, int secondsOfTimeout) 77 { 78 object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI"); 79 if (secondsOfTimeout < 0) 80 { 81 throw new TimeoutException("获取SAPGui Application超时时间不能小于0"); 82 } 83 else 84 { 85 if (SapGuilRot == null) 86 { 87 System.Threading.Thread.Sleep(1000); 88 return GetSAPGuiApp(sapROTWrapper, secondsOfTimeout - 1); 89 } 90 else 91 { 92 object engine = SapGuilRot.GetType().InvokeMember("GetSCriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuilRot, null); 93 if (engine == null) 94 { 95 throw new NullReferenceException("SAPGui Application没有发现。"); 96 } 97 98 return engine as GuiApplication; 99 } 100 } 101 }
如果连接的Host只输入ip,默认连接3200端口,如果需要连接其余端口,server 输入”/H/IP/S/端口号“, 例如本地3201端口:”/H/127.0.0.1/S/3201“.