Skip to content

通过进程找窗口 - FindWindowByProcessId

函数简介

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

接口名称

FindWindowByProcessId

DLL调用

long FindWindowByProcessId(long ola, long process_id, string class, string title);

参数说明

参数名类型说明
ola长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
process_id长整数型要查找的进程ID。
class字符串窗口类名,支持模糊匹配。为空字符串则匹配所有类名。
title字符串窗口标题,支持模糊匹配。为空字符串则匹配所有标题。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long handle = ola.FindWindowByProcessId(0, "value", "value");
csharp
using OLAPlug;

var ola = new OLAPlugServer();
long handle = ola.FindWindowByProcessId(0, "value", "value");
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
handle = ola.FindWindowByProcessId(0, "value", "value")
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
long handle = ola.FindWindowByProcessId(0, "value", "value");
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
handle := ola.FindWindowByProcessId(0, "value", "value")
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let handle = ola.find_window_by_process_id(0, "value", "value");
cpp
var ola = com("OlaPlug.OlaSoft")
var handle = ola.FindWindowByProcessId(0, "value", "value")
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
handle = ola.FindWindowByProcessId(0, "value", "value")
text
.局部变量 ola, OLAPlug
ola.创建 ()
handle = ola.FindWindowByProcessId(0, "value", "value")
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var handle = ola.FindWindowByProcessId(0, "value", "value");
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
长整数 handle = ola.FindWindowByProcessId(0, "value", "value")
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long handle = ola.FindWindowByProcessId(0, "value", "value");

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long ptr = FindWindowByProcessId(instance, 0, "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 FindWindowByProcessId(long ola, long process_id, string class, string title);

long instance = CreateCOLAPlugInterFace();
long ptr = FindWindowByProcessId(instance, 0, "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.FindWindowByProcessId(instance, 0, "value", "value")
if ptr:
    buf = create_string_buffer(512)
    ola.GetStringFromPtr(ptr, buf, 512)
    ola.FreeStringPtr(ptr)
    result = buf.value.decode("utf-8")

返回值

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

注意事项

项目说明
进程ID必须是当前运行的有效进程ID进程ID必须是当前运行的有效进程ID。
如果有多个匹配的窗口如果有多个匹配的窗口,返回第一个找到的窗口。