Search code examples
c#vb6sap-gui

get GuiApplication of running Sap logon vb6 to c#


I have to migrate a vb6 program to C# .net 3.5 the user starts SAP logon and authenticates, then he can use the tool to fetch and insert the data using the tool.

The problem: I can create a new GuiApplication object with reflection, but I can't fetch currently opened GuiSession object with it :/

Here is the vb6 part of the code that gets currently opened GuiApplication object with all opened GuiSession objects.

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

I tried it with reflection:

 GuiApplication Application = (GuiApplication)System.Activator.CreateInstance(Type.GetTypeFromProgID("SapGui.S‌​criptingCtrl.1"));

I got an instance but no existing sessions.


Solution

  • 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);
    
    
            }
    

    Use This method, you have to reference SapROTWr.DLL which is in the sapgui folder of your SAP installation.