Untitled

 avatar
unknown
plain_text
2 months ago
3.2 kB
5
Indexable
using System;
using System.Runtime.InteropServices;

namespace SAPAutomation
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Connecting to SAP GUI...");

                // Attach to the SAP GUI scripting engine
                Type sapGuiType = Type.GetTypeFromProgID("SapGui.ScriptingCtrl.1");
                if (sapGuiType == null)
                {
                    throw new Exception("SAP GUI Scripting Control not found. Ensure it is registered and installed.");
                }

                dynamic sapGuiApp = Activator.CreateInstance(sapGuiType);
                if (sapGuiApp == null)
                {
                    throw new Exception("Failed to create an instance of SAP GUI scripting application.");
                }
                Console.WriteLine("Successfully connected to SAP GUI Scripting Control!");

                // List all available connections
                Console.WriteLine("Checking available connections...");
                int connectionCount = sapGuiApp.Connections.Count;
                Console.WriteLine($"Number of connections: {connectionCount}");

                if (connectionCount == 0)
                {
                    throw new Exception("No active connections found in SAP Logon. Ensure you have an open and active connection.");
                }

                for (int i = 0; i < connectionCount; i++)
                {
                    dynamic connection = sapGuiApp.Connections(i);
                    Console.WriteLine($"Connection {i}: {(connection != null ? connection.Name : "null")}");
                }

                // Attempt to get the first connection
                dynamic sapConnection = sapGuiApp.Connections(0);
                if (sapConnection == null)
                {
                    throw new Exception("No active SAP connection found. Ensure a connection is active in SAP Logon.");
                }

                Console.WriteLine("Successfully connected to the SAP connection!");

                // Check for active sessions
                Console.WriteLine("Checking for active sessions...");
                int sessionCount = sapConnection.Children.Count;
                Console.WriteLine($"Number of sessions: {sessionCount}");

                if (sessionCount == 0)
                {
                    throw new Exception("No active SAP session found. Ensure you are logged into the SAP system.");
                }

                dynamic sapSession = sapConnection.Children(0);
                if (sapSession == null)
                {
                    throw new Exception("No active SAP session found. Ensure you are logged into the SAP system.");
                }

                Console.WriteLine("Successfully connected to the SAP session!");
            }
            catch (COMException ex)
            {
                Console.WriteLine($"COM Error: {ex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}
Editor is loading...
Leave a Comment