主题
枚举进程窗口 - EnumWindowByProcess
函数简介
根据指定进程名以及其它条件,枚举系统中符合条件的窗口。
接口名称
EnumWindowByProcessDLL调用
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 接口释放内存。 |
