Skip to content

枚举进程窗口 - EnumWindowByProcess

函数简介

根据指定进程名以及其它条件,枚举系统中符合条件的窗口。

接口名称

EnumWindowByProcess

DLL调用

long EnumWindowByProcess(long ola, string process_name, string title, string class_name, int filter);

参数说明

参数名类型说明
ola长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
process_name字符串进程名(如 "notepad.exe"),精确匹配但不区分大小写。
title字符串窗口标题,支持模糊匹配。为空字符串则不匹配标题。
class_name字符串窗口类名,支持模糊匹配。为空字符串则不匹配类名。
filter整数型过滤条件,可组合使用(值相加)。

filter 过滤条件

说明
1匹配窗口标题(参数title有效)。
2匹配窗口类名(参数class_name有效)。
4只匹配指定进程的第一个窗口。
8匹配顶级窗口(所有者窗口为0)。
16匹配可见窗口。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
string hwnds = ola.EnumWindowByProcess("notepad.exe", "记事本", "", 1 + 8 + 16);
// 返回 "hwnd1,hwnd2,hwnd3",空串表示未找到
csharp
using OLAPlug;

var ola = new OLAPlugServer();
string hwnds = ola.EnumWindowByProcess("notepad.exe", "记事本", "", 1 + 8 + 16);
// 返回 "hwnd1,hwnd2,hwnd3",空串表示未找到
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
hwnds = ola.EnumWindowByProcess("notepad.exe", "记事本", "", 1 + 8 + 16)
# 返回 "hwnd1,hwnd2,hwnd3",空串表示未找到
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
String hwnds = ola.EnumWindowByProcess("notepad.exe", "记事本", "", 1 + 8 + 16);
// 返回 "hwnd1,hwnd2,hwnd3",空串表示未找到
cpp
var ola = com("OlaPlug.OlaSoft")
var hwnds = ola.EnumWindowByProcess("notepad.exe", "记事本", "", 1 + 8 + 16)
// 返回 "hwnd1,hwnd2,hwnd3",空串表示未找到
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
hwnds = ola.EnumWindowByProcess("notepad.exe", "记事本", "", 1 + 8 + 16)
' 返回 "hwnd1,hwnd2,hwnd3",空串表示未找到
text
.局部变量 ola, OLAPlug
ola.创建 ()
hwnds = ola.EnumWindowByProcess(“notepad.exe“, “记事本“, ““, 1 + 8 + 16)
' 返回 "hwnd1,hwnd2,hwnd3",空串表示未找到
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var hwnds = ola.EnumWindowByProcess("notepad.exe", "记事本", "", 1 + 8 + 16);
// 返回 "hwnd1,hwnd2,hwnd3",空串表示未找到
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
文本型 hwnds = ola.EnumWindowByProcess("notepad.exe", "记事本", "", 1 + 8 + 16)
// 返回 "hwnd1,hwnd2,hwnd3",空串表示未找到
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
string hwnds = ola.EnumWindowByProcess("notepad.exe", "记事本", "", 1 + 8 + 16);
// 返回 "hwnd1,hwnd2,hwnd3",空串表示未找到

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long hwndsPtr = EnumWindowByProcess(instance, "notepad.exe", "记事本", "", 1 + 8 + 16);
if (hwndsPtr != 0) {
    char hwnds[512] = {0};
    GetStringFromPtr(hwndsPtr, hwnds, sizeof(hwnds));
    FreeStringPtr(hwndsPtr);
}
// 返回 "hwnd1,hwnd2,hwnd3"
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();

long instance = CreateCOLAPlugInterFace();
long hwndsPtr = EnumWindowByProcess(instance, "notepad.exe", "记事本", "", 1 + 8 + 16);
if (hwndsPtr != 0) {
    StringBuilder hwnds = new StringBuilder(GetStringSize(hwndsPtr) + 1);
    GetStringFromPtr(hwndsPtr, hwnds, hwnds.Capacity);
    FreeStringPtr(hwndsPtr);
    string hwndsStr = hwnds.ToString();
}
// 返回 "hwnd1,hwnd2,hwnd3"
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
hwndsPtr = EnumWindowByProcess(instance, "notepad.exe", "记事本", "", 1 + 8 + 16)
if hwndsPtr:
    buf = create_string_buffer(512)
    ola.GetStringFromPtr(hwndsPtr, buf, 512)
    ola.FreeStringPtr(hwndsPtr)
    hwnds = buf.value.decode("utf-8")
# 返回 "hwnd1,hwnd2,hwnd3"

返回值

返回值说明
(返回值)字符串指针地址,包含所有匹配的窗口句柄,格式为 "hwnd1,hwnd2,hwnd3"

注意事项

项目说明
释放内存DLL调用返回字符串指针地址,需要调用 FreeStringPtr 接口释放内存。