Question Using FindWindowEx to get Powerpoint Slide Show window handle

gusset25

Member
Joined
Apr 29, 2010
Messages
12
Programming Experience
5-10
Hi there. Apologies if this is posted to the wrong forum - I couldn't find an API forum.

I've spent 18 hours trying to solve this one.

I am trying to send a keystroke to a running powerpoint presentation with SendMessage or Postmessage. But I can't get as far as the handle for the presentation.

VB.NET:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
 Private Shared Function FindWindow( _
         ByVal lpClassName As String, _
         ByVal lpWindowName As String) As IntPtr
 End Function

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
 Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
          ByVal childAfter As Integer, _
          ByVal lclassName As String, _
          ByVal windowTitle As Integer) As IntPtr
 End Function

Sub DoStuff

Dim intAppHWnd As New IntPtr(0)
Dim intSlideShowHWnd As New IntPtr(0)

'get the window handle to the powerpoint application '***RETURNS HANDLE***
intAppHWnd = FindWindow("PP12FrameClass", Nothing)
'get the handle to the slide show window '***FAIL***
intSlideShowHWnd = FindWindowEx(intAppHWnd, Nothing, "paneClassDC", Nothing) 

End Sub

As I understand it, you have to use FindWindow to get the application's handle and then FindWindowEx to get the sub-window's handle. The problem is that the line below ***FAIL*** returns 0, not a handle.

Relevant material / possible answers are given in these sites but in C-type languages and I just can't see how what I'm doing is making the code fail:
RE: hide cursor for kiosk application (powerpoint)
hide cursor for kiosk application (powerpoint) | keyongtech
Passing Event to Other Application..~ - CodeGuru Forums

As ever, all help gratefully received.
 
Last edited:
I am some of the way there - have grabbed a handle of and sent a character to a sub window in Notepad with this

VB.NET:
Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal _ 
lpClassName As String, ByVal lpWindowName As String) As IntPtr

Declare Auto Function FindWindowEx Lib "User32" _
(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, _
ByVal lclassName As String, _
ByVal windowTitle As IntPtr) As IntPtr

Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByVal lParam As String) As Integer

Private Sub DoStuff

Dim lpszParentClass As String = "Notepad"
Dim lpszParentWindow As String = "Untitled - Notepad"
Dim lpszClass As String = "Edit"
Dim ParenthWnd As New IntPtr(0)
Dim hWnd As New IntPtr(0)
Dim retval As Integer

ParenthWnd = FindWindow(lpszParentClass, Nothing)

  If ParenthWnd.Equals(IntPtr.Zero) Then
        Debug.WriteLine("Notepad Not Running")
      Else
        hWnd = FindWindowEx(ParenthWnd, IntPtr.Zero, lpszClass, Nothing)
        If hWnd.Equals(IntPtr.Zero) Then
             Debug.WriteLine("Notepad doesn't have a child window")
             Else
             retval = SendMessage(hWnd, WM_CHAR, Keys.Space, Nothing)'***WORKS!***
         End If
  End If
End Sub

Logically, therefore, the Powerpoint code should be:

VB.NET:
Private Sub DoStuffInPowerPoint
        Dim lpszParentClass As String = "PP12FrameClass"
        'Dim lpszClass As String = "paneClassDC"
        Dim lpszClass As String = "Slide Class"
        Dim ParenthWnd As New IntPtr(0)
        Dim hWnd As New IntPtr(0)
        Dim retval As Integer

        ParenthWnd = FindWindow(lpszParentClass, Nothing)
        If ParenthWnd.Equals(IntPtr.Zero) Then
            Debug.WriteLine("PPT Not Running")
        Else
            hWnd = FindWindowEx(ParenthWnd, IntPtr.Zero, lpszClass, Nothing)
            hWnd = FindWindow("screenClass", Nothing)
            If hWnd.Equals(IntPtr.Zero) Then
                Debug.WriteLine("PPT doesn't have a child window")
            Else
                retval = SendMessage(hWnd, WM_CHAR, Keys.Space, nothing)
            End If
        End If

    End Sub

At the moment, the Notepad app receives the keypress but the Powerpoint app doesn't seem to.
 
Last edited:
The following code inserts " ABef" into Notepad but nothing into Powerpoint. Not sure why not. Perhaps I have the wrong window

VB.NET:
Public Class Form1
    Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

    Private Const WM_CHAR = &H102
    Private Const WM_KEYDOWN = &H100
    Private Const WM_KEYUP = &H101

    Private Declare Auto Function FindWindowEx Lib "User32" (ByVal parentHandle As IntPtr, _
             ByVal childAfter As IntPtr, _
             ByVal lclassName As String, _
             ByVal windowTitle As IntPtr) As IntPtr

    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
    (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer

    
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        FindNotepad()
        FindPowerpoint()
        End
    End Sub

    Private Sub FindNotepad()
        Dim strParentClass As String = "Notepad"
        Dim strChildClass As String = "Edit"

        Dim Parent_hWnd As New IntPtr(0)
        Dim hWnd As New IntPtr(0)
        Dim iRetval As Integer

        Parent_hWnd = FindWindow(strParentClass, Nothing)

        hWnd = FindWindowEx(Parent_hWnd, IntPtr.Zero, strChildClass, Nothing)

        iRetval = PostMessage(hWnd, WM_KEYUP, Keys.Space, 0)
        iRetval = SendMessage(hWnd, WM_CHAR, Keys.A, Nothing)
        iRetval = SendMessage(hWnd, WM_CHAR, Keys.B, 0)
        iRetval = SendMessage(hWnd, WM_KEYDOWN, Keys.C, 0)
        iRetval = SendMessage(hWnd, WM_KEYUP, Keys.D, 0)
        iRetval = PostMessage(hWnd, WM_KEYDOWN, Keys.E, 0)
        iRetval = PostMessage(hWnd, WM_KEYUP, Keys.F, 0)

    End Sub 'Notepad

    Private Sub FindPowerpoint()
        Dim strParentClass As String = "PP12FrameClass"
        Dim Parent_hWnd As New IntPtr(0)
        Dim hWnd As New IntPtr(0)
        Dim iRetval As Integer

        Parent_hWnd = FindWindow(strParentClass, Nothing)

        hWnd = FindWindow("screenClass", Nothing)


        iRetval = PostMessage(hWnd, WM_KEYUP, Keys.Space, 0)
        iRetval = SendMessage(hWnd, WM_CHAR, Keys.A, Nothing)
        iRetval = SendMessage(hWnd, WM_CHAR, Keys.B, 0)
        iRetval = SendMessage(hWnd, WM_KEYDOWN, Keys.C, 0)
        iRetval = SendMessage(hWnd, WM_KEYUP, Keys.D, 0)
        iRetval = PostMessage(hWnd, WM_KEYDOWN, Keys.E, 0)
        iRetval = PostMessage(hWnd, WM_KEYUP, Keys.F, 0)


    End Sub 'ppt

End Class
 
Last edited:
Back
Top