Friday, March 11, 2011

Sending a dummy message to a windows application

Hi All,

Here is my requirement: I have a virtual Windows server 2008 R2 environment, where I don't have much rights. I need to monitor some stuff in there, but since screen saver is enabled in the environment, the screen locks itself after few minutes. I did a bit of research and came up with this dummy program, which "simulates" some events, so that the environment thinks something is going on there, so it doesn't locks. But I've been thinking, there should be a better way. If anyone knows, please leave it in the comments section.

Here is the code I used :
[I will open up the windows notepad  and would type in the following in the window title text box: Untitiled - Notepad]

Imports System.Runtime.InteropServices
Imports System.Threading

Public Class frmSendString
    "user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function
    "user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As HandleRef, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function

    Declare Auto Function SetForegroundWindow Lib "USER32.DLL" _
    (ByVal hWnd As IntPtr) As Boolean
    Dim myThread As New Thread(AddressOf ThreadProcessor_Dinesh)

    Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
            ' Activate an application window.

            'If hwndNotepad <> 0 Then
            'SendMessage(dHref, New IntPtr(256), New IntPtr(96), &H1E0001)
            'SendMessage(dHref, New IntPtr(258), New IntPtr(96), &H1E0001)
            'SendMessage(dHref, New IntPtr(257), New IntPtr(96), &HC01E0001)
            'End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub StartSendingDummyEvents()
    End Sub

    Private Sub ThreadProcessor_Dinesh()
        Dim hwndApp As IntPtr
        Dim dHref As HandleRef
        Dim irandVal As Integer
        Dim myRandi As New Random()

            While True
                hwndApp = FindWindow(vbNullString, txtWindowTitle.Text)
                dHref = New HandleRef(Nothing, hwndApp)
                'TextBox1.Text = dHref.Handle.ToString()
                hwndApp = FindWindow(vbNullString, Me.Text)
                ' TextBox1.Text = dHref.Handle.ToString()
                irandVal = myRandi.Next()
                'Thread.Sleep(irandVal * 2)
            End While
        Catch ex As Exception
            hwndApp = Nothing
        End Try
    End Sub

    Private Sub frmSendString_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        myThread = Nothing
    End Sub

    Private Sub frmSendString_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

No comments: