我必须将 vb6 程序迁移到 C# .net 3.5用户启动 SAP 登录并进行身份验证,然后,他可以使用该工具使用该工具获取和插入数据。
问题:我可以使用反射创建一个新的GuiApplication
对象,但我无法使用它获取当前打开GuiSession
对象:/
这是代码的 vb6 部分,该代码当前打开GuiApplication
对象以及所有打开的GuiSession
对象。
Dim obj As Object
Set obj = CreateObject("SAPGUI")
Set obj = obj.GetScriptingEngine
If TypeName(obj) = "GuiApplication" Then
Set SapAutomationObject = obj
SapAutomationObject.AllowSystemMessages = False
Debug.Print "SAP Automation OK"
End If
我用反思尝试了一下:
GuiApplication Application = (GuiApplication)System.Activator.CreateInstance(Type.GetTypeFromProgID("SapGui.ScriptingCtrl.1"));
我有一个实例,但没有现有会话。
public static void testConnection()
{
SapROTWr.CSapROTWrapper sapROTWrapper = new SapROTWr.CSapROTWrapper();
object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI");
object engine = SapGuilRot.GetType().InvokeMember("GetSCriptingEngine", System.Reflection.BindingFlags.InvokeMethod,
null, SapGuilRot, null);
SAPconnection.sapGuiApp = engine as GuiApplication;
GuiConnection connection = sapGuiApp.Connections.ElementAt(0) as GuiConnection;
GuiSession session = connection.Children.ElementAt(0) as GuiSession;
MessageBox.Show(session.Info.User + " !!||!! " + session.Info.Transaction);
}
使用此方法,您必须引用 SapROTWr.DLL它位于 SAP 安装的 sapgui 文件夹中。
这对我有用(SAP 730/Win7):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPFEWSELib;
using SapROTWr;
namespace FIT.SapHelper
{
public static class stcSapHelper
{
public static void testConnection()
{
SapROTWr.CSapROTWrapper sapROTWrapper = new SapROTWr.CSapROTWrapper();
object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI");
object engine = SapGuilRot.GetType().InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuilRot, null);
GuiConnection connection = (engine as GuiApplication).OpenConnection("BOX DESCRIPTION");
GuiSession session = connection.Children.ElementAt(0) as GuiSession;
}
}
}
假设 SAPGUI 是一个 COM 对象,那么您应该能够引用它并将其创建为新对象,而无需使用反射。 即使用早期绑定而不是后期绑定,即使原始 VB6 代码使用"后期绑定"
其次,假设绑定较晚,难道不应该Type.GetTypeFromProgID("SapGui")
Type.GetTypeFromProgID("SapGui.ScriptingCtrl.1")
片段以匹配原始 VB6 吗? 您可能需要检查 SAPGUI 的对象模型,以确保引用了正确的对象。
我发现使用正在运行的会话的唯一解决方案是在DLL中加载该代码,并通过C#<</p>
发布了 SAP .NET 连接器,以提供从 .NET 应用程序内部与 SAP 系统交互的标准化方式。查看 http://service.sap.com/connectors,您必须是 SAP 合作伙伴才能访问该页面