S
S
Sant Ferrand2021-10-25 16:12:49
Windows
Sant Ferrand, 2021-10-25 16:12:49

Getting the number of open windows via WinApi?

I am writing a project for conducting exams in one educational institution. I would like to be able to get the number of open windows of other programs, to protect academic integrity (stop the test if I opened a second application, calculator or browser, etc.).

I tried to implement this on this stackoverflow question
https://stackoverflow.com/questions/10246444/how-c...
but this method doesn't work correctly, also showing the number and hidden windows.

The answer can be in C / C ++ or Rust (at the extreme Python)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
antares4045, 2021-10-25
@FontSepten

Considering that the python code is accepted "on the edge", I'm not sure that this can be considered a full-fledged answer: there is such a wonderful lib in python: psutil with which I periodically write all sorts of dirt based on the template

import psutil
import win32gui
import win32process


def enum_window_callback(hwnd, pid):
        tid, current_pid = win32process.GetWindowThreadProcessId(hwnd)
        if pid == current_pid and win32gui.IsWindowVisible(hwnd):
            windows.append(hwnd)

for process in psutil.process_iter():
    pid = process.pid
    windows = []
    win32gui.EnumWindows(enum_window_callback, pid)
    windownames = [win32gui.GetWindowText(item) for item in windows]
    
    if len(windownames):
        exeName = process.name()
        for windowName in windownames:
            hwnd = win32gui.FindWindow(None, windowName)
            print(f'"{exeName}" - "{windowName}" - "{hwnd}"')

Conclusion:
65860" "explorer.exe" - "Program Manager" - "131380" "python.exe" - "Jupyter Notebook" - "1639536" "chrome.exe" - "Getting the number of open windows via WinApi? - Habr Q&A - Google Chrome" - "1442268"
At the output, we have a list of format running executable - window name - pid
for your task if you don't like any executable - you can ban.
but there is a problem with translation to pluses: psutil (I tried to figure it out a couple of years ago) does some black magic under the hood, so the only thing I can offer you is to compile this script into a separate executable and call it with a subprocess (well, or ignore my answer).
True, if you know a way (suddenly) how to get a list of all pids under Windows, then share it - I will be very happy.
UPD:
could not let go and made the implementation entirely on winapi
import win32gui
import win32process
import win32api
import win32process
import win32con


def enum_window_callback(hwnd, _):
        if win32gui.IsWindowVisible(hwnd):
            tid, current_pid = win32process.GetWindowThreadProcessId(hwnd)
            handle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, False, current_pid)
            proc_name = win32process.GetModuleFileNameEx(handle, 0)
            windows.append({
                "hwnd":hwnd, 
                "pid" : current_pid, 
                "tid" : tid,
                "window title" : win32gui.GetWindowText(hwnd),
                "process name" : proc_name
                })

windows = []

win32gui.EnumWindows(enum_window_callback, None)

for window in windows:
    print(f'"{window["process name"]}"', window['pid'], f'"{window["window title"]}"', window['hwnd'], sep=' - ')

Conclusion:
Getting the number of open windows via WinApi? — Habr Q&A - Google Chrome" - 1442268
"C:\Anaconda3\python.exe" - 9912 - "Jupyter Notebook" - 1639536
"C:\Program Files (x86)\Battle.net\Battle.net.exe" - 6396 - "Battle.net" - 132384
" C:\Windows\explorer.exe" - 8084 - "D:\" - 263030
"C:\Windows\SystemApps\MicrosoftWindows.Client.CBS_cw5n1h2txyewy\InputApp\TextInputHost.exe" - 12956 - "Microsoft Text Input Application" - 131658
"C:\Program Files\NVIDIA Corporation\NVIDIA GeForce Experience\NVIDIA Share.exe" - 5488 - "NVIDIA GeForce Overlay" - 66250
"C:\Windows\explorer.exe" - 8084 - "" - 65924
"C:\ Windows\explorer.exe" - 8084 - "" - 65932
"C:\Windows\explorer.exe" - 8084 - ""- 65892
"C:\Windows\explorer.exe" - 8084 - "" - 65890
"C:\Windows\explorer.exe" - 8084 - "Program Manager" - 131380

When transferring to the pros, I encountered the fact that I don’t remember how to normally import all these vinusers - if you write, throw it off in the comments, but I’m tired)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question