1

Searching through many SO post like this , I was able to come up with following code to access Windows File Open dialog box. Basically I need to specify a file in this dialog box which opened by another app (No option to use tKinter, not a web app). I am getting error as following

EnumChildWindows(currentHwnd,_windowEnumerationHandler,childWindows) ctypes.ArgumentError: argument 2: : Don't know how to convert parameter 2

What am I missing here? Is there any other way to achieve this functionality?

import ctypes
import win32gui

EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
SendMessage = ctypes.windll.user32.SendMessageW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible
EnumChildWindows = ctypes.windll.user32.EnumChildWindows

WM_SETTEXT = 0x000C

def _windowEnumerationHandler(hwnd, resultList):
    #Pass to win32gui.EnumWindows() to generate list of window handle,
   # window text, window class tuples.
    resultList.append((hwnd, win32gui.GetClassName(hwnd)))

def searchChildWindows(currentHwnd,
                       wantedText=None,
                       wantedClass=None,
                       selectionFunction=None):
    childWindows = []
    result =[]
    EnumChildWindows(currentHwnd,_windowEnumerationHandler,childWindows)
    for childHwnd, windowText, windowClass in childWindows:
        descendentMatchingHwnds = searchChildWindows(childHwnd)
        if wantedClass == windowClass:
            result.append(childHwnd)

    return childHwnd

def getEditBox(hwnd):
    children = list(set(searchChildWindows(hwnd, 'ComboBoxEx32')))
    for addr_child in children:
        if (win32gui.GetClassName(addr_child) == 'Edit'):
            print(f"found TextBox")
            SendMessage(hwnd, WM_SETTEXT, 0, "Can I change this title?")

def foreach_window_child(hwnd, lParam):
    if IsWindowVisible(hwnd):
        length = GetWindowTextLength(hwnd)
        buff = ctypes.create_unicode_buffer(length + 1)
        GetWindowText(hwnd, buff, length + 1)
        if (buff.value == "Open"):  # This is the window label
            print(f"foreach_window_child found it")
            #SendMessage(hwnd, WM_SETTEXT, 0, "Do u see me ") # Changes the title of dlg box
            getEditBox(hwnd)
    return True

EnumWindows(EnumWindowsProc(foreach_window_child), 0)
BeHappy
  • 138
  • 2
  • 17

1 Answers1

1

Dumb mistake

EnumChildWindows(currentHwnd,_windowEnumerationHandler,childWindows)

to

 EnumChildWindows(currentHwnd[0],_windowEnumerationHandler,childWindows)

fixed the problem

BeHappy
  • 138
  • 2
  • 17