cancel
Showing results for 
Search instead for 
Did you mean: 

Starting SAP GUI using C#

ptuga
Explorer
0 Kudos

Hello,

I have been provided with this code previously on another ticket I have raised.

It works fine for VB.NET:

SapGuiAuto = CreateObject("Sapgui.ScriptingCtrl.1")
app = SapGuiAuto.GetScriptingEngine
app.HistoryEnabled = False
Dim connName = "......................................"
connection = app.OpenConnection(connName, True)
If connection.DisabledByServer = True Then
   Exit Sub
End If
session = connection.Children(0)
If session.Info.IsLowSpeedConnection = True Then
   Exit Sub
End If

For reference: https://answers.sap.com/questions/13346150/beginner-create-a-visual-studio-application-consol.html?c...

The idea now is to find a way to use C# to do the same task: If SAP GUI is not running, to start the GUI and login using a known connection.

I have added the line:

using Microsoft.VisualBasic;

And added the VisualBasic Reference to my app

I have also set my app to 32 bit (Platform target as x86).

I was able to do this:

SapGuiAuto = Microsoft.VisualBasic.Interaction.CreateObject("Sapgui.ScriptingCtrl.1");
app = InvokeMethod(SapGuiAuto, "GetScriptingEngine", new object[0]);
SetProperty(app, "HistoryEnabled", new object[1] { false });

From here I don't know how to manage to get the connection and session objects (among all the other lines in the VB.NET example).

Please support.

Thank you.

Accepted Solutions (0)

Answers (1)

Answers (1)

ptuga
Explorer
0 Kudos

Hello everyone,

After a few attempts and looking at the different ways of setting properties, getting the properties and calling the methods, I have managed to making it work.

My code below:

SapGuiAuto = Microsoft.VisualBasic.Interaction.CreateObject("Sapgui.ScriptingCtrl.1");
app = InvokeMethod(SapGuiAuto, "GetScriptingEngine");
SetProperty(app, "HistoryEnabled", new object[1] { false });

connection = InvokeMethod(app, "OpenConnection", new object[2] { " ... MY CONNECTION NAME ...", true });
if (GetProperty(connection, "DisabledByServer") == true)
{
   return;
}

session = GetProperty(connection, "Children", new object[1] { 0 });
object info = GetProperty(session, "Info");
if (GetProperty(info, "IsLowSpeedConnection") == true)
{
   return;
}
ptuga
Explorer
0 Kudos

The objects will need to be defined and released at the end:

object SapGuiAuto = null;
object app = null;
object connection = null;
object session = null;

FreeObject(session);
FreeObject(connection);
FreeObject(app);
FreeObject(SapGuiAuto);
ptuga
Explorer
0 Kudos

And methods will need to be defined:

static dynamic InvokeMethod(object obj, string methodName, object[] methodParams = null)
{
	return obj.GetType().InvokeMember(methodName, BindingFlags.InvokeMethod, null, obj, methodParams);
}

static dynamic GetProperty(object obj, string propertyName, object[] propertyParams = null)
{
	return obj.GetType().InvokeMember(propertyName, BindingFlags.GetProperty, null, obj, propertyParams);
}

static dynamic SetProperty(object obj, string propertyName, object[] propertyParams = null)
{
	return obj.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, obj, propertyParams);
}

static void FreeObject(object obj)
{
	Marshal.ReleaseComObject(obj);
}
ptuga
Explorer
0 Kudos

When the code runs, SAP GUI opens and a connection is established.

When the code finishes, it closes the connection and SAP GUI if you have a console application.

If your app is a Windows form, the session stays open until the form is closed.

I hope it helps anyone trying to achieve the same goal.