2

I have got Python and Visual Basic working. I am struggling to find a working solution for C# although I understand that it should be available and relatively similar to Visual Basic, right? I don't have SAP GUI DLL's on my development machine, so I can't just add reference to it.

So in Python this is working fine:

#-Begin-----------------------------------------------------------------

#-Includes--------------------------------------------------------------
import sys, win32com.client

#-Sub Main--------------------------------------------------------------
def Main():

    SapGuiAuto = win32com.client.GetObject("SAPGUI")
    if not type(SapGuiAuto) == win32com.client.CDispatch:
      return

    application = SapGuiAuto.GetScriptingEngine
    if not type(application) == win32com.client.CDispatch:
      SapGuiAuto = None
      return

    connection = application.Children(0)
    if not type(connection) == win32com.client.CDispatch:
      application = None
      SapGuiAuto = None
      return

    session = connection.Children(1)
    if not type(session) == win32com.client.CDispatch:
      connection = None
      application = None
      SapGuiAuto = None
      return

   #session.findById("wnd[0]").resizeWorkingPane 173, 36, 0
    session.findById("wnd[0]").resizeWorkingPane(173, 36, 0)

In Visual Basic similar would be:

Sub Main

  Set SapGuiAuto = GetObject("SAPGUI")
  Set Application = SapGuiAuto.GetScriptingEngine
  Set Connection = Application.openConnection("EQ2")
  Set Session = Connection.Children(0)
  Session.ActiveWindow.Iconify()
  Session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "410"
  Session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "XYZ"
  Session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "Pwd1"
  Session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "EN" 
  Session.findById("wnd[0]").SendVKey 0 

End Sub

Or this:

Sub SAP_OpenSessionFromLogon()

  Dim SapGui
  Dim Applic
  Dim connection
  Dim session

  Shell ("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapfewcp.exe")

  Set SapGui = GetObject("SAPGUI") 
  Set Applic = SapGui.GetScriptingEngine 
  Set connection = Applic.OpenConnection("QLA - ECC Project One Quality System", True) '<=== here you need to fillin your connection description

End Sub

How to do the same in C# without adding reference to SAP GUI COM DLL or is it even possible without it? I mean just to open some exe on computer, bring windows foreground and send automate GUI?

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
10101
  • 2,232
  • 3
  • 26
  • 66
  • 1
    I believe to do something similar in C# you have to use `dynamic`. In general, I would expect late binding to work better in VB than C# because it's more ingrained in the history of the language. – Craig Feb 01 '23 at 21:44

1 Answers1

1

In C# you need the package SAP.GUI.Scripting.net. Using .NET 7 (C# 11) the equivalent of your VB code would be (with <ImplicitUsings>enable</ImplicitUsings>):

using sapfewse;
using saprotwr.net;
using System.Diagnostics;
using System.Reflection;

Process.Start(@"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe");
Thread.Sleep(1000);

var SapGui = new CSapROTWrapper().GetROTEntry("SAPGUI");
var Applic = (GuiApplication)SapGui.GetType().InvokeMember("GetScriptingEngine", BindingFlags.InvokeMethod, null, SapGui, null)!;
var connection = Applic.OpenConnection("QLA - ECC Project One Quality System", true);

If using .NET SDK x64 make sure to add <RuntimeIdentifier>win-x86</RuntimeIdentifier> to your .csproj file. Or just use .NET SDK x86.

Marduk
  • 982
  • 8
  • 12