2

I am writing a script / program to log into SAP GUI then just grab a few bits of data from a employee then close SAP.

This I can do already but am trying to make a fancy GUI just for that instead of clicking many windows in SAP GUI to get it...mainly because I'm lazy cant be bothered clicking a million things... Some call this innovation I think :)

I have been researching BackGroundWorker in vb.net but wouldn't this still load the window and just keep the form active and responsive while running the program?

I don't have 'admin' rights (can create and modify user accounts su01, pa30, etc.) to the server etc. so cant log into the server\database... hence running script to obtain result..

Does anyone know how I can log into SAP GUI and have it hidden while running?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Pavle Stojanovic
  • 525
  • 2
  • 8
  • 30

1 Answers1

1

You can check this post about how to hide an external application window. http://www.vbforums.com/showthread.php?669210-RESOLVED-Hiding-Window-of-external-process

In this example, guys trying to start calc.exe in hidden mode.

First off all insert at the beggining of your progect

Imports System.Runtime.InteropServices

Then you have to enum flags for ShowWindow (http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx

Private Enum ShowWindowCommand As Integer
    Hide = 0
    Show = 5
    Minimize = 6
    Restore = 9
End Enum

Set a specified window's show state

 <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
 Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As ShowWindowCommand) As Boolean
 End Function

Determine whether the specified window handle identifies an existing window

 <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
 Private Shared Function IsWindow(ByVal hWnd As IntPtr) As Boolean
 End Function

Determines whether a specified window is minimized.

 Private Declare Auto Function IsIconic Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean

variable to save window handle (you can choise your own name, but rename in other part of code)

 Private calc_hWnd As IntPtr

launch windows calculator (in your case saplogon.exe) minimized and hidden, while loading form

 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim test As New Process
    Try
        ' file to launch            
        test.StartInfo.FileName = "calc.exe" ' note: full path not needed for windows calc.
        ' Note: This next line has no effect on WinXP "calc.exe" and some other apps like FireFox.
        'test.StartInfo.WindowStyle = ProcessWindowStyle.Minimized

        ' start the app
        test.Start()
        ' wait until app is in idle state
        test.WaitForInputIdle(-1)



        ' get app main window handle
        Dim tmp_hWnd As IntPtr = test.MainWindowHandle
        ' make sure handle is valid (non zero)


        ' try up to 10 times within one second
        ' do a re-try loop that runs for a second or two
        For i As Integer = 1 To 10
            tmp_hWnd = test.MainWindowHandle
            If Not tmp_hWnd.Equals(IntPtr.Zero) Then Exit For ' got handle so exit loop
            Threading.Thread.Sleep(100) ' wait 100ms
        Next '- try again 




        If Not tmp_hWnd.Equals(IntPtr.Zero) Then
            ' use ShowWindow to change app window state (minimize and hide it).
            ShowWindow(tmp_hWnd, ShowWindowCommand.Minimize)
            ShowWindow(tmp_hWnd, ShowWindowCommand.Hide)
            ' save handle for later use.
            calc_hWnd = tmp_hWnd
        Else
            ' no window handle?
            MessageBox.Show("Unable to get a window handle!")
        End If
    Catch ex As Exception
        ' error !
        MessageBox.Show(ex.Message)
    End Try
End Sub

on exit restore/unhide app if found running.

Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    ' is our variable set to non-zero?
    If Not calc_hWnd.Equals(IntPtr.Zero) Then
        ' is app window found?
        If IsWindow(calc_hWnd) = True Then
            ' if app is minimized then restore it
            If IsIconic(calc_hWnd) Then
                ShowWindow(calc_hWnd, ShowWindowCommand.Restore)
            End If
            ' make sure window is seen incase it was hidden.
            ShowWindow(calc_hWnd, ShowWindowCommand.Show)
        End If
    End If
End Sub

But you can write another code in and kill saplogon.exe process.

Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    For Each p As Process In System.Diagnostics.Process.GetProcessesByName("saplogon.exe")
        Try
            p.Kill()
            ' possibly with a timeout
            p.WaitForExit()
            ' process has already exited - might be able to let this one go

        Catch ex As Exception
            MessageBox.Show(ex.toString)
       End Try
    Next
End Sub
Maksym Sadovnychyy
  • 175
  • 1
  • 4
  • 17