Initial
This commit is contained in:
522
EDOKA/Utils/ApplicationFileWatcher.vb
Normal file
522
EDOKA/Utils/ApplicationFileWatcher.vb
Normal file
@@ -0,0 +1,522 @@
|
||||
Imports System
|
||||
Imports System.Text
|
||||
Imports System.Runtime.InteropServices
|
||||
Imports System.Threading
|
||||
Imports System.IO
|
||||
|
||||
Public Class ApplicationFileWatcher_X
|
||||
|
||||
#Region "Deklarationen"
|
||||
Private m_isActive As Boolean
|
||||
Public Event DocumentClosed()
|
||||
|
||||
Private m_Filename As String
|
||||
Property Filename() As String
|
||||
Get
|
||||
Return m_Filename
|
||||
End Get
|
||||
Set(ByVal Value As String)
|
||||
m_Filename = Value
|
||||
End Set
|
||||
End Property
|
||||
|
||||
Private m_ApplicationType As Integer
|
||||
Property ApplicationType()
|
||||
Get
|
||||
Return m_ApplicationType
|
||||
End Get
|
||||
Set(ByVal Value)
|
||||
m_ApplicationType = Value
|
||||
End Set
|
||||
End Property
|
||||
|
||||
Private m_Applicationname As String
|
||||
Property Appname() As String
|
||||
Get
|
||||
Return m_Applicationname
|
||||
End Get
|
||||
Set(ByVal Value As String)
|
||||
m_Applicationname = Value
|
||||
End Set
|
||||
End Property
|
||||
|
||||
Private m_WindowNameDC As String
|
||||
Property WindowNameDC() As String
|
||||
Get
|
||||
Return m_WindowNameDC
|
||||
End Get
|
||||
Set(ByVal Value As String)
|
||||
m_WindowNameDC = Value
|
||||
End Set
|
||||
End Property
|
||||
|
||||
|
||||
Private m_WindowName As String
|
||||
Property WindowName() As String
|
||||
Get
|
||||
Return m_WindowName
|
||||
End Get
|
||||
Set(ByVal Value As String)
|
||||
m_WindowName = Value
|
||||
End Set
|
||||
End Property
|
||||
|
||||
'Rel. Office 2010
|
||||
Private m_WindowNameKomp As String
|
||||
Property WindowNameKompatibilitaet() As String
|
||||
Get
|
||||
Return m_WindowNameKomp
|
||||
End Get
|
||||
Set(ByVal value As String)
|
||||
m_WindowNameKomp = value
|
||||
End Set
|
||||
End Property
|
||||
|
||||
Private m_WindowNamePreview As String
|
||||
Property WindowNamePreview() As String
|
||||
Get
|
||||
Return m_WindowNamePreview
|
||||
End Get
|
||||
Set(ByVal Value As String)
|
||||
m_WindowNamePreview = Value
|
||||
End Set
|
||||
End Property
|
||||
|
||||
Dim m_processid As Long
|
||||
Property ProcessID As Long
|
||||
Get
|
||||
Return m_processid
|
||||
End Get
|
||||
Set(value As Long)
|
||||
m_processid = value
|
||||
End Set
|
||||
End Property
|
||||
|
||||
Dim m_dokumentid As String
|
||||
Property Dokumentid As String
|
||||
Get
|
||||
Return m_dokumentid
|
||||
End Get
|
||||
Set(value As String)
|
||||
m_dokumentid = value
|
||||
End Set
|
||||
End Property
|
||||
|
||||
Const STRING_BUFFER_LENGTH As Integer = 255
|
||||
Dim WindowArray As New ArrayList()
|
||||
Dim WithEvents MyTimer As New System.Timers.Timer(1000)
|
||||
|
||||
Dim Office2016Names As New List(Of String)
|
||||
|
||||
#End Region
|
||||
|
||||
#Region "Öffentliche Methoden"
|
||||
Public Sub New()
|
||||
End Sub
|
||||
|
||||
Public Sub New(ByVal typ As Integer, ByVal DocFileName As String)
|
||||
MyBase.New()
|
||||
End Sub
|
||||
|
||||
Public Sub Dispose()
|
||||
Me.WindowArray = Nothing
|
||||
End Sub
|
||||
|
||||
Public Sub Start()
|
||||
AddHandler MyTimer.Elapsed, AddressOf TimerFired
|
||||
SetWindowName()
|
||||
MyTimer.Start()
|
||||
End Sub
|
||||
|
||||
Public Sub Stopp()
|
||||
Me.MyTimer.Stop()
|
||||
End Sub
|
||||
|
||||
Public Sub BringWindowToTop()
|
||||
Dim i As Integer
|
||||
SetWindowName()
|
||||
i = Win32API.FindWindow(vbNullString, Me.WindowName)
|
||||
|
||||
If i <> 0 Then
|
||||
i = Win32API.SetForegroundWindow(i)
|
||||
i = Win32API.ShowWindow(i, Win32API.SW_RESTORE)
|
||||
End If
|
||||
|
||||
End Sub
|
||||
|
||||
Public Function doc_is_active() As Boolean
|
||||
|
||||
Dim Docfound As Boolean = False
|
||||
Try
|
||||
Dim p As Process = Process.GetProcessById(Me.ProcessID)
|
||||
If p.Id = Me.ProcessID Then
|
||||
If GetHandle(Me.Dokumentid) <> 0 Then Docfound = True
|
||||
If p.Id = 0 Then Docfound = False
|
||||
End If
|
||||
Catch ex As Exception
|
||||
Docfound = False
|
||||
End Try
|
||||
Return Docfound
|
||||
Exit Function
|
||||
|
||||
'Dim I As Integer
|
||||
'Search_List()
|
||||
'For I = 0 To Me.WindowArray.Count - 1
|
||||
' If Me.WindowArray.Item(I) = Me.WindowName Then doc_is_active = True
|
||||
'Next
|
||||
Dim I As Integer
|
||||
'20200901 - Clear WindowWarray
|
||||
Me.WindowArray.Clear()
|
||||
Search_List()
|
||||
If Globals.UseOffice2016 = False Then
|
||||
For I = 0 To Me.WindowArray.Count - 1
|
||||
If Me.WindowArray.Item(I) = Me.WindowName Then doc_is_active = True
|
||||
Next
|
||||
End If
|
||||
If Globals.UseOffice2016 = True Then
|
||||
For I = 0 To Me.WindowArray.Count - 1
|
||||
For x As Integer = 0 To Me.Office2016Names.Count - 1
|
||||
If Me.WindowArray.Item(I).ToString <> "" And Me.WindowArray.Item(I) = Me.Office2016Names.Item(x) Then
|
||||
doc_is_active = True
|
||||
Try
|
||||
If Globals.Office2016Debug = True Then
|
||||
FileOpen(7, "h:\office2016Windowlist.txt", OpenMode.Append)
|
||||
WriteLine(7, "--->Is Active:" + Me.WindowArray.Item(I).ToString + "/" + Me.Office2016Names.Item(x))
|
||||
FileClose(7)
|
||||
End If
|
||||
Catch EX As Exception
|
||||
End Try
|
||||
End If
|
||||
|
||||
Next
|
||||
Next
|
||||
End If
|
||||
|
||||
End Function
|
||||
|
||||
#End Region
|
||||
|
||||
#Region "Private Methoden"
|
||||
|
||||
Public Function GetHandle(ByVal caption As String) As String
|
||||
Try
|
||||
For i As Integer = 1 To Globals.try_count_search
|
||||
If ApplicationType = 1 Then
|
||||
For Each p As Process In Process.GetProcessesByName("winword")
|
||||
If p.MainWindowTitle.IndexOf(caption) > -1 Then
|
||||
Return p.Id.ToString
|
||||
Exit Function
|
||||
End If
|
||||
Next
|
||||
End If
|
||||
If ApplicationType = 2 Then
|
||||
For Each p As Process In Process.GetProcessesByName("excel")
|
||||
If p.MainWindowTitle.IndexOf(caption) > -1 Then
|
||||
Return p.Id.ToString
|
||||
Exit Function
|
||||
End If
|
||||
Next
|
||||
End If
|
||||
If Me.ApplicationType <> 1 And Me.ApplicationType <> 2 Then
|
||||
For Each p As Process In Process.GetProcesses
|
||||
If p.MainWindowTitle.IndexOf(caption) > -1 Then
|
||||
Return p.Id.ToString
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Next
|
||||
End If
|
||||
Thread.Sleep(500)
|
||||
Next i
|
||||
|
||||
Return 0
|
||||
Catch ex As Exception
|
||||
PerfMon.force_insert_entry(Me.Dokumentid + ": Prozess-ID konnte nicht ermittelt werden: " + ex.Message)
|
||||
End Try
|
||||
|
||||
End Function
|
||||
|
||||
Public Sub SetWindowName()
|
||||
'Select Case Me.ApplicationType
|
||||
' Case 1 'Word
|
||||
' Me.WindowName = Me.Filename + " - Microsoft Word"
|
||||
' Me.WindowNameKompatibilitaet = Me.Filename + " [Kompatibilitätsmodus] - Microsoft Word"
|
||||
' Me.WindowNamePreview = Me.Filename + " (Seitenansicht) - Microsoft Word"
|
||||
' Me.WindowNameDC = Me.Filename + " - DC"
|
||||
' Case 2 'Excel
|
||||
' Me.WindowName = "Microsoft Excel - " & Me.Filename
|
||||
' Me.WindowNameKompatibilitaet = "Microsoft Excel - " & Me.Filename + " [Kompatibilitätsmodus]"
|
||||
' Me.WindowNamePreview = "Microsoft Excel - " & Me.Filename
|
||||
' Me.WindowNameDC = Me.Filename + ""
|
||||
' Case 3
|
||||
' Me.WindowName = Me.Filename + " - Adobe Reader"
|
||||
' Me.WindowNameKompatibilitaet = Me.Filename + " - Adobe Reader"
|
||||
' Me.WindowNamePreview = Me.Filename + " - Adobe Reader"
|
||||
' Me.WindowNameDC = Me.Filename + ""
|
||||
' Case 4
|
||||
' Me.WindowName = Me.WindowName
|
||||
' Me.WindowNamePreview = Me.WindowNamePreview
|
||||
' Me.WindowNameDC = Me.Filename + ""
|
||||
' Case Else
|
||||
' 'MsgBox("Hallo")
|
||||
'End Select
|
||||
If Globals.Office2010WatchFIles.Rows.Count = 0 Then
|
||||
DivFnkt.Get_Office2016_Params()
|
||||
End If
|
||||
If Globals.UseOffice2016 = False Then
|
||||
Select Case Me.ApplicationType
|
||||
Case 1 'Word
|
||||
Me.WindowName = Me.Filename + " - Microsoft Word"
|
||||
Me.WindowNameKompatibilitaet = Me.Filename + " [Kompatibilitätsmodus] - Microsoft Word"
|
||||
Me.WindowNamePreview = Me.Filename + " (Seitenansicht) - Microsoft Word"
|
||||
Me.WindowNameDC = Me.Filename + " - DC"
|
||||
Case 2 'Excel
|
||||
Me.WindowName = "Microsoft Excel - " & Me.Filename
|
||||
Me.WindowNameKompatibilitaet = "Microsoft Excel - " & Me.Filename + " [Kompatibilitätsmodus]"
|
||||
Me.WindowNameKompatibilitaet = Me.Filename + " [Kompatibilitätsmodus] - Excel"
|
||||
Me.WindowNamePreview = "Microsoft Excel - " & Me.Filename
|
||||
Me.WindowNameDC = Me.Filename + ""
|
||||
Case 3
|
||||
Me.WindowName = Me.Filename + " - Adobe Reader"
|
||||
Me.WindowNameKompatibilitaet = Me.Filename + " - Adobe Reader"
|
||||
Me.WindowNamePreview = Me.Filename + " - Adobe Reader"
|
||||
Me.WindowNameDC = Me.Filename + ""
|
||||
Case 4
|
||||
Me.WindowName = Me.WindowName
|
||||
Me.WindowNamePreview = Me.WindowNamePreview
|
||||
Me.WindowNameDC = Me.Filename + ""
|
||||
Case Else
|
||||
'MsgBox("Hallo")
|
||||
End Select
|
||||
End If
|
||||
If Globals.UseOffice2016 = True Then
|
||||
For Each r As DataRow In Globals.Office2010WatchFIles.Rows
|
||||
Select Case ApplicationType
|
||||
Case 1 'word
|
||||
If r("applikation") = "Word" Then
|
||||
If r.Item(0) = 1 Then Me.WindowName = r.Item("WindowName").ToString.Replace("[Filename]", Me.Filename)
|
||||
Me.Office2016Names.Add(r.Item("WindowName").ToString.Replace("[Filename]", Me.Filename))
|
||||
End If
|
||||
Case 2 'Excel
|
||||
If r("applikation") = "Excel" Then
|
||||
If r.Item(0) = 10 Then Me.WindowName = r.Item("WindowName").ToString.Replace("[Filename]", Me.Filename)
|
||||
Me.Office2016Names.Add(r.Item("WindowName").ToString.Replace("[Filename]", Me.Filename))
|
||||
End If
|
||||
Case 3 'Acrobat
|
||||
If r("applikation") = "Adobe" Then
|
||||
If r.Item(0) = 20 Then Me.WindowName = r.Item("WindowName").ToString.Replace("[Filename]", Me.Filename)
|
||||
Me.Office2016Names.Add(r.Item("WindowName").ToString.Replace("[Filename]", Me.Filename))
|
||||
End If
|
||||
Case Else
|
||||
Me.Office2016Names.Add(r.Item("WindowName").ToString.Replace("[Filename]", Me.Filename))
|
||||
End Select
|
||||
|
||||
Next
|
||||
End If
|
||||
|
||||
End Sub
|
||||
Public Function getWindowName() As String
|
||||
Return Me.WindowName
|
||||
End Function
|
||||
|
||||
'Public Sub TimerFired(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles MyTimer.Elapsed
|
||||
' MyTimer.Stop()
|
||||
' 'Dim sW As New StreamWriter("D:\TRACE.LOG", True)
|
||||
' 'sW.WriteLine(Now)
|
||||
' Dim DocFound As Boolean = False
|
||||
' Dim i As Integer
|
||||
' Dim zz As Integer = 0
|
||||
' 'While zz < 3 And DocFound = False
|
||||
' Me.WindowArray.Clear()
|
||||
' Search_List()
|
||||
' For i = 0 To Me.WindowArray.Count - 1
|
||||
' 'sW.WriteLine(Me.WindowName + ":" + Me.WindowArray.Item(i))
|
||||
' 'If Me.WindowArray.Item(i) = Me.WindowName Then MsgBox("Ist aktiv")
|
||||
' If Me.WindowArray.Item(i) = Me.WindowName Then DocFound = True
|
||||
' If Me.WindowArray.Item(i) = Me.WindowNamePreview Then DocFound = True
|
||||
' If Me.WindowArray.Item(i) = Me.WindowNameDC Then DocFound = True
|
||||
' If Me.WindowArray.Item(i) = Me.WindowNameKompatibilitaet Then DocFound = True
|
||||
' Next
|
||||
' 'zz = zz + 1
|
||||
' ' Thread.Sleep(300)
|
||||
' ' 'End While
|
||||
' ' Thread.Sleep(300)
|
||||
' If Not DocFound Then
|
||||
' Dim fn As String = Globals.Applikationsdaten.Rows(Globals.AppldataRow).Item("pfad_temporaer_dokumente") + "~$" + Microsoft.VisualBasic.Right(Me.Filename, Len(Me.Filename) - 2)
|
||||
' Dim fc As New FileInfo(fn)
|
||||
' If fc.Exists Then DocFound = True
|
||||
' If Not DocFound Then
|
||||
' ' sW.WriteLine("Stop:" + Me.WindowName)
|
||||
' MyTimer.Stop()
|
||||
' RaiseEvent DocumentClosed()
|
||||
' Else
|
||||
' MyTimer.Start()
|
||||
' End If
|
||||
' Else
|
||||
' MyTimer.Start()
|
||||
' End If
|
||||
' DocFound = Nothing
|
||||
' ' sW.WriteLine(Now)
|
||||
' ' sW.Flush()
|
||||
' ' sW.Close()
|
||||
'End Sub
|
||||
|
||||
'Public Sub TimerFired1(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles MyTimer.Elapsed
|
||||
' MyTimer.Stop()
|
||||
' Dim DocFound As Boolean = False
|
||||
' Dim i As Integer
|
||||
' Dim zz As Integer = 0
|
||||
' Me.WindowArray.Clear()
|
||||
' Search_List()
|
||||
|
||||
' Try
|
||||
' If Globals.Office2016Debug = True Then FileOpen(7, "h:\office2016Windowlist.txt", OpenMode.Append)
|
||||
' Catch EX As Exception
|
||||
' End Try
|
||||
|
||||
' Try
|
||||
' For i = 0 To Me.WindowArray.Count - 1
|
||||
' If Globals.UseOffice2016 = False Then
|
||||
' If Me.WindowArray.Item(i) = Me.WindowName Then DocFound = True
|
||||
' If Me.WindowArray.Item(i) = Me.WindowNamePreview Then DocFound = True
|
||||
' If Me.WindowArray.Item(i) = Me.WindowNameDC Then DocFound = True
|
||||
' If Me.WindowArray.Item(i) = Me.WindowNameKompatibilitaet Then DocFound = True
|
||||
' End If
|
||||
' If Globals.Office2016Debug = True Then
|
||||
' WriteLine(7, "EDOKA:-----------------------------------------------")
|
||||
' End If
|
||||
|
||||
' If Globals.UseOffice2016 = True Then
|
||||
' For x As Integer = 0 To Me.Office2016Names.Count - 1
|
||||
' If Globals.Office2016Debug = True Then
|
||||
' WriteLine(7, "EDOKA:" + Me.Office2016Names.Item(x).ToString + " - " + Me.Office2016Names.Count.ToString)
|
||||
' End If
|
||||
' If Me.WindowArray.Item(i).ToString <> "" And Me.WindowArray.Item(i) = Me.Office2016Names.Item(x) Then
|
||||
' DocFound = True
|
||||
' If Globals.Office2016Debug = True Then
|
||||
' WriteLine(7, "--->DocFound:" + Me.WindowArray.Item(i).ToString + "/" + Me.Office2016Names.Item(x))
|
||||
' End If
|
||||
' Exit For
|
||||
' End If
|
||||
' Next
|
||||
' If DocFound Then Exit For
|
||||
' End If
|
||||
' Try
|
||||
' If Globals.Office2016Debug = True Then
|
||||
' WriteLine(7, Me.WindowArray.Item(i))
|
||||
' End If
|
||||
' Catch
|
||||
' End Try
|
||||
' Next
|
||||
' Catch ex As Exception
|
||||
' End Try
|
||||
|
||||
' If Globals.Office2016Debug = True Then
|
||||
' Try
|
||||
' For i = 1 To 100000
|
||||
' Application.DoEvents()
|
||||
' Next
|
||||
' FileClose(7)
|
||||
' Catch
|
||||
' End Try
|
||||
|
||||
' End If
|
||||
' If Not DocFound Then
|
||||
' Dim fn As String = Globals.Applikationsdaten.Rows(Globals.AppldataRow).Item("pfad_temporaer_dokumente") + "~$" + Microsoft.VisualBasic.Right(Me.Filename, Len(Me.Filename) - 2)
|
||||
' Dim fc As New FileInfo(fn)
|
||||
' If fc.Exists Then DocFound = True
|
||||
' If Not DocFound Then
|
||||
' ' sW.WriteLine("Stop:" + Me.WindowName)
|
||||
' MyTimer.Stop()
|
||||
' GC.Collect()
|
||||
' GC.WaitForPendingFinalizers()
|
||||
' GC.Collect()
|
||||
|
||||
' GC.WaitForPendingFinalizers()
|
||||
' RaiseEvent DocumentClosed()
|
||||
' Else
|
||||
' MyTimer.Start()
|
||||
' End If
|
||||
' Else
|
||||
' MyTimer.Start()
|
||||
' End If
|
||||
' DocFound = Nothing
|
||||
'End Sub
|
||||
|
||||
Public Sub TimerFired(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles MyTimer.Elapsed
|
||||
MyTimer.Stop()
|
||||
Dim DocFound As Boolean = False
|
||||
|
||||
Dim i As Integer = 0
|
||||
|
||||
Try
|
||||
Dim p As Process = Process.GetProcessById(Me.ProcessID)
|
||||
If p.Id = Me.ProcessID Then DocFound = True
|
||||
Catch ex As Exception
|
||||
DocFound = False
|
||||
End Try
|
||||
|
||||
|
||||
If ApplicationType = 3 Then
|
||||
If GetHandle(Me.Dokumentid) > 0 Then DocFound = True Else DocFound = False
|
||||
End If
|
||||
|
||||
If Not DocFound Then
|
||||
GC.Collect()
|
||||
GC.WaitForPendingFinalizers()
|
||||
GC.Collect()
|
||||
|
||||
GC.WaitForPendingFinalizers()
|
||||
RaiseEvent DocumentClosed()
|
||||
Else
|
||||
MyTimer.Start()
|
||||
End If
|
||||
DocFound = Nothing
|
||||
End Sub
|
||||
Private Sub Search_List()
|
||||
Win32API.EnumWindowsDllImport(New Win32API.EnumWindowsCallback(AddressOf _
|
||||
FillActiveWindowsList), 0)
|
||||
'20200901 - Wait afrer Fill
|
||||
Thread.Sleep(Globals.wait_after_searchlist)
|
||||
End Sub
|
||||
|
||||
Function FillActiveWindowsList(ByVal hWnd As Integer, ByVal lParam As Integer) As Boolean
|
||||
Dim windowText As New StringBuilder(STRING_BUFFER_LENGTH)
|
||||
Win32API.GetWindowText(hWnd, windowText, STRING_BUFFER_LENGTH)
|
||||
If ProcessIsActiveWindow(hWnd) Then
|
||||
Me.WindowArray.Add(windowText.ToString)
|
||||
End If
|
||||
Return True
|
||||
End Function
|
||||
|
||||
Function ProcessIsActiveWindow(ByVal hWnd As Integer) As Boolean
|
||||
Dim windowText As New StringBuilder(STRING_BUFFER_LENGTH)
|
||||
Dim windowIsOwned As Boolean
|
||||
Dim windowStyle As Integer
|
||||
Win32API.GetWindowText(hWnd, windowText, STRING_BUFFER_LENGTH)
|
||||
windowIsOwned = Win32API.GetWindow(hWnd, Win32API.GW_OWNER) <> 0
|
||||
windowStyle = Win32API.GetWindowLong(hWnd, Win32API.GWL_EXSTYLE)
|
||||
Return True
|
||||
|
||||
If Not Win32API.IsWindowVisible(hWnd) Then
|
||||
Return False
|
||||
End If
|
||||
If windowText.ToString.Equals("") Then
|
||||
Return False
|
||||
End If
|
||||
If Win32API.GetParent(hWnd) <> 0 Then
|
||||
Return False
|
||||
End If
|
||||
If (windowStyle And Win32API.WS_EX_TOOLWINDOW) <> 0 And Not windowIsOwned Then
|
||||
Return False
|
||||
End If
|
||||
If (windowStyle And Win32API.WS_EX_APPWINDOW) = 0 And windowIsOwned Then
|
||||
Return False
|
||||
End If
|
||||
Return True
|
||||
End Function
|
||||
|
||||
|
||||
#End Region
|
||||
|
||||
End Class
|
||||
Reference in New Issue
Block a user