获取数据时隐藏SAP窗口

时间:2014-10-27 07:44:23

标签: vb.net sap vb.net-2010

我正在编写一个脚本/程序来登录SAP,然后从员工那里获取一些数据,然后关闭SAP。

我已经可以这样做但是我正在努力制作一个花哨的GUI,而不是单击SAP中的许多窗口来获取它...主要是因为我很懒,不能点击一百万件事......有些人称之为创新我想:)

我一直在研究vb.net中的BackGroundWorker,但是这仍然会加载窗口,只是在运行程序时保持表单活动和响应?

我没有' admin'权限(可以创建和修改用户帐户su01,pa30等)到服务器等,所以无法登录到server \ database ...因此运行脚本来获取结果..

有没有人知道我如何记录SAP并在运行时隐藏它?

1 个答案:

答案 0 :(得分:1)

您可以查看此帖子,了解如何隐藏外部应用程序窗口。 http://www.vbforums.com/showthread.php?669210-RESOLVED-Hiding-Window-of-external-process

在这个例子中,有人试图以隐藏模式启动calc.exe。

首先在你的progect开始时插入所有插入

Imports System.Runtime.InteropServices

然后你必须为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

设置指定窗口的显示状态

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

确定指定的窗口句柄是否标识现有窗口

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

确定指定窗口是否最小化。

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

保存窗口句柄的变量(您可以选择自己的名称,但在代码的其他部分重命名)

 Private calc_hWnd As IntPtr

启动Windows计算器(在您的情况下为saplogon.exe)最小化和隐藏,同时加载表单

 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
如果找到正在运行的话,退出恢复/取消隐藏应用程序

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

但是你可以编写另一个代码并杀死saplogon.exe进程。

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