Skip to content

Instantly share code, notes, and snippets.

@azechi
Created December 3, 2019 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azechi/af77dcd10c663be4344658be928abf48 to your computer and use it in GitHub Desktop.
Save azechi/af77dcd10c663be4344658be928abf48 to your computer and use it in GitHub Desktop.
windows api, QueryFullProcessImageName, DwmGetWindowAttribute,
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import pywintypes\n",
"import win32gui\n",
"import win32process\n",
"import win32api\n",
"import win32con\n",
" \n",
"from ctypes import *\n",
"from ctypes.wintypes import *\n",
"\n",
"kernel32 = windll.kernel32\n",
"\n",
"#kernel32.QueryFullProcessImageNameW\n",
"win32con.PROCESS_QUERY_LIMITED_INFORMATION = 0x1000\n",
"\n",
"def QueryFullProcessImageName(hprocess, flags):\n",
" buff = create_unicode_buffer(260)\n",
" length = c_ulong(260)\n",
" ret = kernel32.QueryFullProcessImageNameW(hprocess.handle, flags, buff, pointer(length))\n",
" if ret == 0:\n",
" raise pywintypes.error(ret, \"QueryFullProcessImageNameW\", \"\")\n",
"\n",
" return buff.value\n",
"\n",
"dwmapi = windll.dwmapi\n",
"\n",
"win32con.DWMWA_EXTENDED_FRAME_BOUNDS = 9\n",
"\n",
"def DwmGetWindowAttribute(hwnd, dwAtter):\n",
" rect = RECT()\n",
" dwmapi.DwmGetWindowAttribute(hwnd.handle,\n",
" dwAtter,\n",
" pointer(rect),\n",
" sizeof(rect))\n",
" return rect;\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"hwnds = []\n",
"\n",
"# enumerate top level windows\n",
"win32gui.EnumWindows(lambda hwnd, acm: hwnds.append(hwnd), hwnds)\n",
"\n",
"for hwnd in hwnds:\n",
" text = win32gui.GetWindowText(hwnd)\n",
" if not text.endswith(\"- Google Chrome\"):\n",
" continue\n",
" \n",
" _, pid = win32process.GetWindowThreadProcessId(hwnd)\n",
" \n",
" try:\n",
" hproc = win32api.OpenProcess(\n",
" win32con.PROCESS_QUERY_LIMITED_INFORMATION,\n",
" False,\n",
" pid,\n",
" )\n",
" \n",
" except pywintypes.error:\n",
" pass\n",
" \n",
" \n",
" \n",
" if text.startswith(\"target\"):\n",
" target = hwnd\n",
" break;\n",
" \n",
" \n",
" #print(pid, hwnd, text)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 0 1920 1200\n",
"(-8, -8, 1928, 1208)\n",
"(0, 0, 1918, 1208)\n"
]
}
],
"source": [
"rect = RECT()\n",
"dwmapi.DwmGetWindowAttribute(target,\n",
" 9,\n",
" pointer(rect),\n",
" sizeof(rect))\n",
"\n",
"print(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top)\n",
"\n",
"print(win32gui.GetWindowRect(target))\n",
"print(win32gui.GetClientRect(target))\n"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [],
"source": [
"win32gui.MoveWindow(target, -7, 0, 974, 607, True)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment