我尝试通过长对象的句柄获取Windows窗口标题名称和pids.我的代码有效,但它有问题.当我应该获得10个或更多时,我只获得4个窗口标题.任何人都可以帮助并告诉我如何修复此代码?我认为问题在于我如何转换长对象(我不太了解它们,以及一般的ctypes).
from __future__ import print_function
from ctypes import *
psapi = windll.psapi
titles = []
# get window title from pid
def gwtfp():
max_array = c_ulong * 4096
pProcessIds = max_array()
pBytesReturned = c_ulong()
psapi.EnumProcesses(byref(pProcessIds),
sizeof(pProcessIds),
byref(pBytesReturned))
# get the number of returned processes
nReturned = pBytesReturned.value/sizeof(c_ulong())
pidProcessArray = [i for i in pProcessIds][:nReturned]
print(pidProcessArray)
#
EnumWindows = windll.user32.EnumWindows
EnumWindowsProc = WINFUNCTYPE(c_bool, POINTER(c_int), POINTER(c_int))
GetWindowText = windll.user32.GetWindowTextW
GetWindowTextLength = windll.user32.GetWindowTextLengthW
IsWindowVisible = windll.user32.IsWindowVisible
for process in pidProcessArray:
#print("Process PID %d" …Run Code Online (Sandbox Code Playgroud) 我正在使用 Visual Studio Community 2017 来编写 C++ 代码。当我运行以下代码时,一切正常。
#include "pch.h"
#include<Windows.h>
#include<Psapi.h>
#include <iostream>
#include <conio.h>
int main()
{
std::cout << "Really!! How do you do it?";
_getch();
}
Run Code Online (Sandbox Code Playgroud)
#include但是,如果我通过包含psapi.hbefore来更改 s 的顺序Windows.h,编译器就会变得很糟糕并向我抛出 198 个错误,其中令人惊讶的是(也许仅对我来说)包括Identifier "BOOL" is undefined。为什么会发生这种情况?
我试图使用EnumProcessesWin32 (psapi) 提供的函数枚举 Windows 进程。生活很顺利,所以我使用 Rust 而不是 C++。代码如下。
Cargo.toml
[dependencies.windows]
features = [
"Win32_Foundation",
"Win32_System_ProcessStatus"
]
Run Code Online (Sandbox Code Playgroud)
主程序.rs
use windows::Win32::System::ProcessStatus::*;
fn main() {
unsafe{
let mut cb_needed: u32 = 0;
let mut a_processes: Vec<u32> = Vec::with_capacity(1024);
let result: windows::Win32::Foundation::BOOL = EnumProcesses(a_processes.as_mut_ptr(), 1024*4, &mut cb_needed);
let mut _c_processes: u32 = 0;
_c_processes = cb_needed / 4;
println!("{:?}",_c_processes);
let mut count: u32 = 0;
while count < _c_processes {
println!("{:?}",a_processes[count as usize]);
count += 1;
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我调试代码时,变量a_processes显示长度为零。然而,变量 …
psapi ×3
windows ×3
c++ ×1
ctypes ×1
long-integer ×1
python ×1
rust ×1
winapi ×1
windows-rs ×1