You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过ppadb/ADB获取BlueStacks安卓模拟器的窗口名、PID及hwnd

获取BlueStacks模拟器的窗口名或PID

问题概述

  • 调用device.get_pid返回绑定方法而非实际PID:
    <bound method Utils.get_pid of <ppadb.device.Device object at 0x0000011591F9AF10>>
    
  • 执行adb shell getprop | grep "ro.product.nickname"无返回,其他设备名与创建实例时的自定义昵称不匹配
  • 当前仅能获取设备IP(device.serial)、ADB设备名称(如emulator-5545)及ADB设备对象

解决方案

一、正确获取ADB对应的进程PID

1. 修正ppadb的get_pid调用

你之前的问题是未执行方法,仅引用了方法对象,正确调用方式:

pid = device.get_pid()
print(pid)

该方法会返回ADB连接对应的模拟器进程PID。

2. 通过端口关联进程PID

BlueStacks每个实例对应独立ADB端口,可通过端口定位PID(Windows系统):

# 替换为你的模拟器ADB端口,比如5555
netstat -ano | findstr :5555

输出结果中的最后一列即为进程PID,可通过该PID匹配BlueStacks进程。

二、获取自定义实例昵称

1. 读取BlueStacks配置文件

BlueStacks实例的自定义昵称存储在配置文件中,路径通常为:
C:\ProgramData\BlueStacks_nxt\Engine\Instances\<实例ID>\config.json
打开文件后,查找nickname字段即可获取创建时设置的昵称。

2. 使用BlueStacks命令行工具查询

执行BlueStacks自带的进程工具命令,直接列出所有实例信息:

HD-Player.exe --list-instances

输出内容包含实例昵称、状态、端口等信息,可解析后与ADB连接的端口/IP匹配。

三、获取窗口名与句柄(Windows系统)

使用pywin32库遍历窗口,通过PID匹配BlueStacks窗口:

import win32gui
import win32process

def get_bluestacks_window(pid):
    window_info = []
    def enum_callback(hwnd, extra):
        _, window_pid = win32process.GetWindowThreadProcessId(hwnd)
        if window_pid == pid and win32gui.IsWindowVisible(hwnd):
            extra.append({
                "hwnd": hwnd,
                "window_name": win32gui.GetWindowText(hwnd)
            })
    win32gui.EnumWindows(enum_callback, window_info)
    return window_info[0] if window_info else None

# 传入获取到的PID
result = get_bluestacks_window(target_pid)
if result:
    print(f"窗口名:{result['window_name']},句柄:{result['hwnd']}")

内容的提问来源于stack exchange,提问作者User3872

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 14:25:07