7

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.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Goran Štuc
  • 581
  • 4
  • 15

5 Answers5

7
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.

Vincent
  • 86
  • 1
  • 2
2

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;
        }
    }
}
MiBol
  • 1,985
  • 10
  • 37
  • 64
1

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.

Carl Onager
  • 4,112
  • 2
  • 38
  • 66
  • the problem is that i don't want to create a new object, i want to get the currently running saplogon.exe's Sessions as for Type.GetTypeFromProgID("SapGui") i don't even get GuiApplication (i get null), with SapGui.ScriptingCtrl.1 i get the saplogon.exe binded but not the sessions :/ – Goran Štuc Dec 10 '12 at 21:37
  • Surely the GetScriptingEngine call in the VB6 connects to the currently running instance? There's nothing else in the VB6 code that could equate to it. Thus you would use the same call in your .NET code. Or is there something else that's not shown in your question? – Carl Onager Dec 11 '12 at 14:35
  • i call that vb6 code atm and it gives me the guiapplication with all connections and sessions if i do the same(reflection + get scripting engine and allow system messages) i only get guiapplication without the connections and without the sessions stored in the connections , after i start it i have access to the scripting engine but that is not enough for me sadly( requirements for the applications :/) – Goran Štuc Dec 11 '12 at 18:01
0

the only solution that i found to work with running sessions is to load that code in a dll and access it via c#

Goran Štuc
  • 581
  • 4
  • 15
0

SAP released SAP .NET connector to provide standartized way to interact with SAP system from within of .NET application. Look at http://service.sap.com/connectors, you must be SAP partner to be able access to the page

Anton Semenov
  • 6,227
  • 5
  • 41
  • 69
  • so, if i open sessions in sap logon and fill all the data, when i use sap .net connector i can access the data? – Goran Štuc Dec 14 '12 at 08:06
  • 1
    @Goran Štuc if the data will be saved to DB - yes. If it will be only on screen - no. With help of .NET Connector you can easily call functional module from SAP side, but cant' acces saplogon screen. In this case you will be responsible for creating whole guiT – Anton Semenov Dec 14 '12 at 09:33