Skip to content

通过进程找窗口 - FindWindowByProcess

函数简介

根据进程名称、窗口类名和标题查找可见窗口。

接口名称

FindWindowByProcess

DLL调用

long FindWindowByProcess(long ola, string process_name, string class, string title);

参数说明

参数名类型说明
ola长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
process_name字符串进程名称(如 "notepad.exe"),精确匹配但不区分大小写。
class字符串窗口类名,支持模糊匹配。为空字符串则匹配所有类名。
title字符串窗口标题,支持模糊匹配。为空字符串则匹配所有标题。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long handle = ola.FindWindowByProcess("notepad.exe", "", "value");
csharp
using OLAPlug;

var ola = new OLAPlugServer();
long handle = ola.FindWindowByProcess("notepad.exe", "", "value");
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
handle = ola.FindWindowByProcess("notepad.exe", "", "value")
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
long handle = ola.FindWindowByProcess("notepad.exe", "", "value");
cpp
var ola = com("OlaPlug.OlaSoft")
var handle = ola.FindWindowByProcess("notepad.exe", "", "value")
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
handle = ola.FindWindowByProcess("notepad.exe", "", "value")
text
.局部变量 ola, OLAPlug
ola.创建 ()
handle = ola.FindWindowByProcess("notepad.exe", "", "value")
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var handle = ola.FindWindowByProcess("notepad.exe", "", "value");
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 handle = ola.FindWindowByProcess("notepad.exe", "", "value")
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long handle = ola.FindWindowByProcess("notepad.exe", "", "value");

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long ptr = FindWindowByProcess(instance, "notepad.exe", "value", "value");
if (ptr != 0) {
    char buffer[512] = {0};
    GetStringFromPtr(ptr, buffer, sizeof(buffer));
    FreeStringPtr(ptr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[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 FindWindowByProcess(long ola, string process_name, string class, string title);

long instance = CreateCOLAPlugInterFace();
long ptr = FindWindowByProcess(instance, "notepad.exe", "value", "value");
if (ptr != 0) {
    StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
    GetStringFromPtr(ptr, sb, sb.Capacity);
    FreeStringPtr(ptr);
    string result = sb.ToString();
}
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()
ptr = ola.FindWindowByProcess(instance, "notepad.exe", "value", "value")
if ptr:
    buf = create_string_buffer(512)
    ola.GetStringFromPtr(ptr, buf, 512)
    ola.FreeStringPtr(ptr)
    result = buf.value.decode("utf-8")

返回值

返回值说明
(返回值)长整数型:找到的窗口句柄。未找到返回 0。

注意事项

项目说明
进程名称必须包含扩展名(如 .exe进程名称必须包含扩展名(如 .exe),不区分大小写。
如果有多个匹配的窗口如果有多个匹配的窗口,返回第一个找到的窗口。