Skip to content

Commit 9ef5061

Browse files
authored
Add RealGetWindowClass (#2299)
1 parent 44c2cc9 commit 9ef5061

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Coming in build 307, as yet unreleased
1515
--------------------------------------
1616

1717
### pywin32
18+
* Add RealGetWindowClass (#2299, @CristiFati)
1819
* Make it compile on Python 3.13 (#2260, @clin1234)
1920
* Fixed accidentally trying to raise a `str` instead of an `Exception` in (#2270, @Avasam)
2021
* `Pythonwin/pywin/debugger/debugger.py`

win32/src/win32gui.i

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5998,26 +5998,49 @@ PyGetScrollInfo (PyObject *self, PyObject *args)
59985998
%native (GetScrollInfo) PyGetScrollInfo;
59995999

60006000
%{
6001+
#define MAX_CHARS 0x100
6002+
60016003
// @pyswig string|GetClassName|Retrieves the name of the class to which the specified window belongs.
60026004
static PyObject *
60036005
PyGetClassName(PyObject *self, PyObject *args)
60046006
{
6005-
HWND hwnd;
6006-
PyObject *obhwnd;
6007-
TCHAR buf[256];
6008-
// @pyparm <o PyHANDLE>|hwnd||The handle to the window
6009-
if (!PyArg_ParseTuple(args, "O:GetClassName", &obhwnd))
6010-
return NULL;
6011-
if (!PyWinObject_AsHANDLE(obhwnd, (HANDLE *)&hwnd))
6012-
return NULL;
6013-
// don't bother with lock - no callback possible.
6014-
int nchars = GetClassName(hwnd, buf, sizeof buf/sizeof buf[0]);
6015-
if (nchars==0)
6016-
return PyWin_SetAPIError("GetClassName");
6017-
return PyWinObject_FromTCHAR(buf, nchars);
6007+
HWND hwnd;
6008+
PyObject *obhwnd;
6009+
TCHAR buf[MAX_CHARS];
6010+
// @pyparm <o PyHANDLE>|hwnd||The handle to the window
6011+
if (!PyArg_ParseTuple(args, "O:GetClassName", &obhwnd))
6012+
return NULL;
6013+
if (!PyWinObject_AsHANDLE(obhwnd, (HANDLE*)&hwnd))
6014+
return NULL;
6015+
// don't bother with lock - no callback possible.
6016+
int nchars = GetClassName(hwnd, buf, MAX_CHARS);
6017+
if (nchars == 0)
6018+
return PyWin_SetAPIErrorOrReturnNone("GetClassName");
6019+
return PyWinObject_FromTCHAR(buf, nchars);
60186020
}
6021+
6022+
// @pyswig string|RealGetWindowClass|Retrieves the name of the class to which the specified window belongs.
6023+
static PyObject *
6024+
PyRealGetWindowClass(PyObject *self, PyObject *args)
6025+
{
6026+
HWND hwnd;
6027+
PyObject *obhwnd;
6028+
TCHAR buf[MAX_CHARS];
6029+
// @pyparm <o PyHANDLE>|hwnd||The handle to the window
6030+
if (!PyArg_ParseTuple(args, "O:RealGetWindowClass", &obhwnd))
6031+
return NULL;
6032+
if (!PyWinObject_AsHANDLE(obhwnd, (HANDLE*)&hwnd))
6033+
return NULL;
6034+
// don't bother with lock - no callback possible.
6035+
UINT nchars = RealGetWindowClass(hwnd, buf, MAX_CHARS);
6036+
if (nchars == 0)
6037+
return PyWin_SetAPIErrorOrReturnNone("RealGetWindowClass");
6038+
return PyWinObject_FromTCHAR(buf, nchars);
6039+
}
6040+
60196041
%}
60206042
%native (GetClassName) PyGetClassName;
6043+
%native (RealGetWindowClass) PyRealGetWindowClass;
60216044

60226045
// @pyswig int|WindowFromPoint|Retrieves a handle to the window that contains the specified point.
60236046
// @pyparm (int, int)|point||The point.

win32/test/test_win32gui.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,5 +209,20 @@ def test_enumdesktopwindows(self):
209209
)
210210

211211

212+
class TestWindowProperties(unittest.TestCase):
213+
def setUp(self):
214+
self.class_functions = (
215+
win32gui.GetClassName,
216+
win32gui.RealGetWindowClass,
217+
)
218+
219+
def test_classname(self):
220+
for func in self.class_functions:
221+
self.assertRaises(pywintypes.error, func, 0)
222+
wnd = win32gui.GetDesktopWindow()
223+
for func in self.class_functions:
224+
self.assertTrue(func(wnd))
225+
226+
212227
if __name__ == "__main__":
213228
unittest.main()

0 commit comments

Comments
 (0)