Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get GuiApplication of running Sap logon vb6 to c#

Tags:

c#

vb6

sap

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 with reflection, but i can't fetch currently opened GuiSessions with it :/ here is the vb6 part of the code that gets currently opened GuiApplication with all opened GuiSessions

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

like image 355
Goran Štuc Avatar asked Dec 05 '12 18:12

Goran Štuc


3 Answers

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.

like image 65
Vincent Avatar answered Nov 12 '22 23:11

Vincent


This works for me (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;
        }
    }
}
like image 2
MiBol Avatar answered Nov 12 '22 22:11

MiBol


Assuming that SAPGUI is a COM object then you should be able to take a reference to it and create it as a new object without using reflection. i.e. Use early binding and not late binding even though the original VB6 code is using 'late binding'

Secondly, assuming late binding, shouldn't the Type.GetTypeFromProgID("SapGui.S‌criptingCtrl.1") fragment be Type.GetTypeFromProgID("SapGui") to match the original VB6? you might need to check on the object model for SAPGUI to make sure you're referencing the right object.

like image 1
Carl Onager Avatar answered Nov 12 '22 22:11

Carl Onager