locked
Monitor Launch Applications on desktop RRS feed

  • Question

  • I need to write a program that monitors applications being launch on a desktop? Does anyone know how to determine when any  program launches on a pc. I need to monitor when a user launches a program.

     

    Help?

     

    Thanks,

    Brandon

    Friday, August 24, 2007 7:36 PM

Answers

  • Beetle54,

     

    According to your demand on determine program launches on desktop, I would like to provide you the SetThreadDesktop function that assign the specified desktop to the calling thread. All subsequent operations on the desktop use the access rights granted to the desktop.

     

    The following code snippet shows the way in VB.NET:

     

    Code Snippet

    Imports System

    Imports System.Windows.Forms

    Imports System.Runtime.InteropServices

    Imports System.Threading

     

    Namespace DeleteMe

        Partial Public Class Form1

            Inherits Form

            Public Sub New()

                InitializeComponent()

            End Sub

            <DllImport("user32.dll")> _

            Private Shared Function SetThreadDesktop(ByVal hDesktop As IntPtr) As Boolean

            End Function

            <DllImport("user32.dll")> _

            Private Shared Function OpenInputDesktop(ByVal dwFlags As UInteger, ByVal fInherit As Boolean, ByVal dwDesiredAccess As Integer) As IntPtr

            End Function

            Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

                Dim t As Thread = newThread(newThreadStart(Thread_ShadowKS))

                t.Priority = ThreadPriority.AboveNormal

                t.Start()

            End Sub

     

            Private Sub Thread_ShadowKS()

                Dim x As IntPtr = OpenInputDesktop(0, True, 0)

                MessageBox.Show(x.ToString())

                Dim Success As Boolean = SetThreadDesktop(x)

                MessageBox.Show(Success.ToString())

            End Sub

        End Class

    End Namespace

     

     

    Hope that can help you.

    Tuesday, August 28, 2007 9:34 AM
  • I don't see how that could be useful either.  Unfortunately, Windows doesn't have a notification feature that allows a program to spy on other programs getting started.  To get such a notification, you would have to use an advanced technique called "API hooking".  Sadly, you can't write this kind of code in .NET, it requires an unmanaged DLL written in C/C++.  Here's a project that discusses the technique, maybe you can pick up some search terms to Google for a ready-made solution.  I however doubt you'll find one that you could use in VB.NET.
    Sunday, September 2, 2007 7:42 PM
  • Perhaps this VB.Net 2003 Code Snippet might help. This is from a sample program that I wrote that can monitor every running program that is NOT hidden from view. Similar to what is shown in Windows Task Manager but without all the fancy code that Task Manager uses.

    I call this function in a sample program at Form Load time then use a timer that updates the ListBox1 Object say every 10 to 15 seconds.

    ---

       Private Sub UpdateProcessesList()

          Dim RunningProcessesData() As Process = Process.GetProcesses()
          Dim Temp3 As String

          ListBox1.Items.Clear()

          If RunningProcessesData.Length > 0 Then
             TotalItems.Text = RunningProcessesData.Length.ToString & " Processes Running"
             Dim Cnt As Integer
             For Cnt = 0 To RunningProcessesData.Length - 1

                Temp3 = Trim(RunningProcessesData(Cnt).ProcessName.ToString)

                If Not (LCase(Temp3) = LCase("System")) And _
                   Not (LCase(Temp3) = LCase("Idle")) Then
                   Temp3 = Trim(RunningProcessesData(Cnt).MainModule.ModuleName.ToString)
                   '
                   ' Trap the exception that occasionally occurs here so that the program doesn't crash!
                   ' - Occasionally a program unloads while processing this list!
                   Try
                   Catch ex As Exception
                   End Try
                End If

                If LCase(Temp3) = LCase("Idle") Then
                   Temp3 = "*** System Idle Process ***"
                End If

                If LCase(Temp3) = LCase("System") Then
                   Temp3 = "*** System Process ***"
                End If

                ListBox1.Items.Add(LCase(Temp3)) ' & " / " & RunningProcessesData(Cnt).MachineName.ToString)

             Next Cnt
          End If

       End Sub

    ---
    Monday, September 3, 2007 3:41 PM

All replies

  • Beetle54,

     

    According to your demand on determine program launches on desktop, I would like to provide you the SetThreadDesktop function that assign the specified desktop to the calling thread. All subsequent operations on the desktop use the access rights granted to the desktop.

     

    The following code snippet shows the way in VB.NET:

     

    Code Snippet

    Imports System

    Imports System.Windows.Forms

    Imports System.Runtime.InteropServices

    Imports System.Threading

     

    Namespace DeleteMe

        Partial Public Class Form1

            Inherits Form

            Public Sub New()

                InitializeComponent()

            End Sub

            <DllImport("user32.dll")> _

            Private Shared Function SetThreadDesktop(ByVal hDesktop As IntPtr) As Boolean

            End Function

            <DllImport("user32.dll")> _

            Private Shared Function OpenInputDesktop(ByVal dwFlags As UInteger, ByVal fInherit As Boolean, ByVal dwDesiredAccess As Integer) As IntPtr

            End Function

            Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

                Dim t As Thread = newThread(newThreadStart(Thread_ShadowKS))

                t.Priority = ThreadPriority.AboveNormal

                t.Start()

            End Sub

     

            Private Sub Thread_ShadowKS()

                Dim x As IntPtr = OpenInputDesktop(0, True, 0)

                MessageBox.Show(x.ToString())

                Dim Success As Boolean = SetThreadDesktop(x)

                MessageBox.Show(Success.ToString())

            End Sub

        End Class

    End Namespace

     

     

    Hope that can help you.

    Tuesday, August 28, 2007 9:34 AM
  •  

    I'm kind of confuse on how to use this code snippet. SetThreadDesktop and OpenInputDesktop, how do they work, and is there any required code within each method? I don't want to run the application, basically I just need to monitor, like keep a timer, on how many minutes an application has been running. Does this code handles for this type of situation?
    Friday, August 31, 2007 7:14 PM
  • I don't see how that could be useful either.  Unfortunately, Windows doesn't have a notification feature that allows a program to spy on other programs getting started.  To get such a notification, you would have to use an advanced technique called "API hooking".  Sadly, you can't write this kind of code in .NET, it requires an unmanaged DLL written in C/C++.  Here's a project that discusses the technique, maybe you can pick up some search terms to Google for a ready-made solution.  I however doubt you'll find one that you could use in VB.NET.
    Sunday, September 2, 2007 7:42 PM
  • Perhaps this VB.Net 2003 Code Snippet might help. This is from a sample program that I wrote that can monitor every running program that is NOT hidden from view. Similar to what is shown in Windows Task Manager but without all the fancy code that Task Manager uses.

    I call this function in a sample program at Form Load time then use a timer that updates the ListBox1 Object say every 10 to 15 seconds.

    ---

       Private Sub UpdateProcessesList()

          Dim RunningProcessesData() As Process = Process.GetProcesses()
          Dim Temp3 As String

          ListBox1.Items.Clear()

          If RunningProcessesData.Length > 0 Then
             TotalItems.Text = RunningProcessesData.Length.ToString & " Processes Running"
             Dim Cnt As Integer
             For Cnt = 0 To RunningProcessesData.Length - 1

                Temp3 = Trim(RunningProcessesData(Cnt).ProcessName.ToString)

                If Not (LCase(Temp3) = LCase("System")) And _
                   Not (LCase(Temp3) = LCase("Idle")) Then
                   Temp3 = Trim(RunningProcessesData(Cnt).MainModule.ModuleName.ToString)
                   '
                   ' Trap the exception that occasionally occurs here so that the program doesn't crash!
                   ' - Occasionally a program unloads while processing this list!
                   Try
                   Catch ex As Exception
                   End Try
                End If

                If LCase(Temp3) = LCase("Idle") Then
                   Temp3 = "*** System Idle Process ***"
                End If

                If LCase(Temp3) = LCase("System") Then
                   Temp3 = "*** System Process ***"
                End If

                ListBox1.Items.Add(LCase(Temp3)) ' & " / " & RunningProcessesData(Cnt).MachineName.ToString)

             Next Cnt
          End If

       End Sub

    ---
    Monday, September 3, 2007 3:41 PM