Skip to content

Killing popup from windows service

savan gandhi

Asked Dec 10, 2007, 7:12 PM EST1 answer
Share
LinkedInX
I have a windows service which uses powerpoint interop to read powerpoint and write the output in an xml file. Now, if this service is given a password protected PPT to read, it hangs as service cannot handle popups. This popup is the popup to ask password of the PPT. I want to kill this popup, so that my service can throw atleast an error "cannot open PPT".

Any ideas?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    =======================================================
        <DllImport("User32.dll", EntryPoint:="FindWindow")> _
        Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

        End Function
        <DllImport("user32.dll", SetLastError:=True)> _
        Private Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, _
                             ByRef lpdwProcessId As Integer) As Integer
        End Function

        'Declare Function SendMessageHM Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Integer
        <DllImport("user32.dll", EntryPoint:="SendMessageA")> _
        Private Shared Function SendMessageHM(ByVal hWnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Integer

        End Function
    ==========================================================
    Private Sub tmrPoll_Elapsed(ByVal obj As Object, ByVal e As ElapsedEventArgs)
            'strKeywords = "Password,Microsoft PowerPoint, Microsoft Excel" -- title of the popup window that you want to kill
            Dim arr() As String = strKeywords.Split(",")

            Dim i As Integer
            For i = 0 To arr.Length - 1 Step 1
                Dim hwnd As Integer = FindWindow(Nothing, arr(i))

                If hwnd <> 0 Then
                    Dim ProcessID As Integer
                    Dim ProcessName As String
                    GetWindowThreadProcessId(New System.IntPtr(hwnd), ProcessID)
                    ''Check that the window belongs to excel or powerpoint
                    ProcessName = System.Diagnostics.Process.GetProcessById(ProcessID).ProcessName
                    If (ProcessName.ToUpper.Equals("EXCEL") Or ProcessName.ToUpper.Equals("POWERPNT")) Then
                        SendMessageHM(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0)
                    End If
                End If
            Next
        End Sub
    ==========================================================
    This code works for killing popup from service. Prerequisite is that the service should have "allow service to interact with desktop" option as checked.
    I am not sure how to pass particular keys (like Alt+F4) with "SendMessageHM" function.

    Thanks...
    0