- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试创建一个功能类似于任务管理器以及许多其他功能的程序。目前,我无法使用当前的枚举函数找到所有顶级窗口。出于某种原因,它会正确枚举并填写某些应用程序的 HWND(例如 Google Chrome、命令提示符、Code::Blocks),但不适用于某些游戏(例如 Roblox(我只测试过一个))。我试着看看 FindWindow() 是否也会失败,但它在那种情况下工作得很好。这意味着 EnumWindows() 显然应该找到它,但显然我做错了什么或者我在文档中读到了错误的东西。我真的不想不得不使用 FindWindow( ),因为我可能无论如何都不知道大多数窗口的标题。
枚举函数:
BOOL CALLBACK FindWindows( HWND handle, LPARAM option )
{
DWORD window_process_id = 0;
GetWindowThreadProcessId( handle, &window_process_id );
process_list * p1 = NULL;
switch ( option )
{
case FIND_WINDOW_HANDLE :
if ( IsWindowEnabled( handle ) )
for ( p1 = head_copy; p1; p1 = p1->next )
if ( p1->pid == window_process_id )
p1->window_handle = handle;
break;
default :
SetLastError( ERROR_INVALID_PARAMETER );
return 0;
break;
}
return TRUE;
}
完整来源:
/* Preprocessor directives */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>
#define TARGET_PROCESS "chrome.exe"
/* Structures */
typedef struct process_list
{
char * process_name;
DWORD pid;
HANDLE process_handle;
HWND window_handle;
int process_name_sz;
struct process_list * next;
} process_list;
typedef struct drawing_data
{
RECT window_pos;
} drawing_data;
/* Enums ( Global integer constants ) */
enum
{
FIND_WINDOW_HANDLE
};
enum
{
TIMER_START,
TIMER_STOP,
TIMER_GETDIFF
};
typedef struct t_timer
{
clock_t start_time;
clock_t end_time;
} t_timer;
/* Global variables */
process_list * head_copy = NULL;
/* ***************************************************************** */
/* Time functions */
clock_t timer( int command, t_timer * timer1 )
{
switch ( command )
{
case TIMER_START :
timer1->start_time = clock( );
break;
case TIMER_STOP :
timer1->end_time = clock( );
break;
case TIMER_GETDIFF :
return ( ( timer1->end_time - timer1->start_time ) / ( CLOCKS_PER_SEC / 1000 ));
break;
default : break;
}
return -1;
}
/* ***************************************************************** */
/* Windows error functions */
void show_error( char * user_string, BOOL exit )
{
char buffer[BUFSIZ] = { 0 };
DWORD error_code = GetLastError( );
FormatMessage
(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) buffer,
BUFSIZ,
NULL
);
printf( "%s : %s", user_string, buffer );
if ( exit ) ExitProcess( error_code );
return;
}
/* ***************************************************************** */
void win_error( char * message, BOOL exit )
{
char buffer[BUFSIZ] = { 0 };
DWORD error_code = GetLastError( );
FormatMessage
(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) buffer,
BUFSIZ,
NULL
);
MessageBox(NULL, buffer, "Error from System :", MB_ICONWARNING | MB_OK );
if ( exit ) ExitProcess( error_code );
return;
}
/* ***************************************************************** */
/* Linked list functions */
process_list * create( )
{
process_list * temp = NULL;
if ( !( temp = malloc( sizeof( process_list ) ) ) )
{
perror("Malloc");
exit( 1 );
}
return temp;
}
/* ***************************************************************** */
process_list * add( process_list * head, HANDLE process_handle, PROCESSENTRY32 * process_structure )
{
process_list * temp = NULL;
if ( !head )
{
head = create( );
head->pid = process_structure->th32ParentProcessID;
head->process_handle = process_handle;
head->process_name_sz = strlen( process_structure->szExeFile ) + 1;
head->process_name = malloc( head->process_name_sz );
if ( !head->process_name )
{
perror( "Malloc" );
exit( 1 );
}
strcpy( head->process_name, process_structure->szExeFile );
head->next = NULL;
} else
{
temp = create( );
temp->next = head;
head = temp;
head->pid = process_structure->th32ParentProcessID;
head->process_handle = process_handle;
head->process_name_sz = strlen( process_structure->szExeFile ) + 1;
head->process_name = malloc( head->process_name_sz );
if ( !head->process_name )
{
perror( "Malloc" );
exit( 1 );
}
strcpy( head->process_name, process_structure->szExeFile );
}
return head;
}
/* ***************************************************************** */
void print_list( process_list * head )
{
process_list * p1 = NULL;
for ( p1 = head; p1; p1 = p1->next )
{
printf(
"-------------------------------------------------\n"
"node.process_name\t=\t%s\n"
"node.process_id\t\t=\t%d\n"
"\nCan terminate process : %s\n\n"
"node.window_handle\t=\t0x%p\n"
"node.next\t\t=\t%s\n",
p1->process_name,
( int )p1->pid,
p1->process_handle == INVALID_HANDLE_VALUE ? "NO" : "YES",
( void * )p1->window_handle,
p1->next ? "(node address)\n" : "NULL"
);
}
}
/* ***************************************************************** */
void print_node( process_list * node )
{
printf(
"node.process_name\t=\t%s\n"
"node.process_id\t\t=\t%d\n"
"\nCan terminate process : %s\n\n"
"node.window_handle\t=\t0x%p\n"
"node.next\t\t=\t%s\n",
node->process_name,
( int )node->pid,
node->process_handle == INVALID_HANDLE_VALUE ? "NO" : "YES",
( void * )node->window_handle,
node->next ? "(node address)\n" : "NULL"
);
return;
}
/* ***************************************************************** */
process_list * delete( process_list * head, process_list * node )
{
process_list * p1 = head;
process_list * p2 = NULL;
if ( !p1 )
return NULL;
else if ( p1 == node )
{
if ( !p1->next )
{
free( p1->process_name );
if ( p1->process_handle != INVALID_HANDLE_VALUE )
CloseHandle( p1->process_handle );
if ( p1->window_handle )
CloseHandle( p1->window_handle );
free( p1 );
}
else
{
free( p1->process_name );
if ( p1->process_handle != INVALID_HANDLE_VALUE )
CloseHandle( p1->process_handle );
if ( p1->window_handle )
CloseHandle( p1->window_handle );
p2 = head->next;
free( p1 );
return p2;
}
return NULL;
}
for ( ; p1 && p1 != node; p2 = p1, p1 = p1->next );
if ( !p1 )
return NULL;
else
{
free( p1->process_name );
if ( p1->process_handle != INVALID_HANDLE_VALUE )
CloseHandle( p1->process_handle );
if ( p1->window_handle )
CloseHandle( p1->window_handle );
p2->next = p1->next;
free( p1 );
}
return head;
}
/* ***************************************************************** */
void free_list( process_list * head )
{
process_list * p1 = head;
process_list * p2 = NULL;
while ( p1 )
{
free( p1->process_name );
if ( p1->process_handle != INVALID_HANDLE_VALUE )
CloseHandle( p1->process_handle );
if ( p1->window_handle )
CloseHandle( p1->window_handle );
p2 = p1;
p1 = p1->next;
free( p2 );
}
return;
}
/* ***************************************************************** */
process_list * find_process_and_copy_node( process_list * head, const char * process_name )
{
BOOL is_match = FALSE;
process_list * p1 = NULL;
process_list * new_node = NULL;
for ( p1 = head; p1; p1 = p1->next )
{
if ( !strcmp( p1->process_name, process_name ) )
{
is_match = TRUE;
break;
}
}
if ( is_match )
{
new_node = create( );
new_node->pid = p1->pid;
new_node->process_handle = p1->process_handle;
if ( !( new_node->process_name = malloc( p1->process_name_sz ) ) )
{
perror( "Malloc" );
free( new_node );
free_list( head );
exit( 1 );
}
new_node->process_name = strcpy( new_node->process_name, p1->process_name );
new_node->process_name_sz = p1->process_name_sz;
new_node->window_handle = p1->window_handle;
new_node->next = NULL;
return new_node;
}
else return NULL;
}
/* ***************************************************************** */
/* WinAPI functions */
BOOL CALLBACK FindWindows( HWND handle, LPARAM option )
{
DWORD window_process_id = 0;
GetWindowThreadProcessId( handle, &window_process_id );
process_list * p1 = NULL;
switch ( option )
{
case FIND_WINDOW_HANDLE :
if ( IsWindowEnabled( handle ) )
for ( p1 = head_copy; p1; p1 = p1->next )
if ( p1->pid == window_process_id )
p1->window_handle = handle;
break;
default :
SetLastError( ERROR_INVALID_PARAMETER );
return 0;
break;
}
return TRUE;
}
/* ***************************************************************** */
process_list * get_process_list( process_list * head )
{
HANDLE h_process_snap;
HANDLE h_process;
PROCESSENTRY32 process_structure;
h_process_snap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( h_process_snap == INVALID_HANDLE_VALUE )
{
show_error( "CreateToolhelp32Snapshot", FALSE );
return NULL;
}
process_structure.dwSize = sizeof( PROCESSENTRY32 );
if( !Process32First( h_process_snap, &process_structure ) )
{
show_error( "Process32First", FALSE );
CloseHandle( h_process_snap );
return NULL;
}
do
{
h_process = OpenProcess( PROCESS_TERMINATE, FALSE, process_structure.th32ProcessID );
if ( h_process )
head = add( head, h_process, &process_structure );
else
head = add( head, INVALID_HANDLE_VALUE, &process_structure );
} while( Process32Next( h_process_snap, &process_structure ) );
CloseHandle( h_process_snap );
return head;
}
/* ***************************************************************** */
process_list * find_process( const char * process_name )
{
process_list * head = NULL;
process_list * target_process = NULL;
if ( !( head = get_process_list( head ) ) )
exit( 1 );
head_copy = head;
if ( !EnumWindows( FindWindows, FIND_WINDOW_HANDLE ) )
win_error( "EnumWindows", FALSE );
target_process = find_process_and_copy_node( head, TARGET_PROCESS );
free_list( head );
return target_process;
}
/* ***************************************************************** */
int main( )
{
t_timer program_run_time;
memset( &program_run_time, 0, sizeof( t_timer ) );
timer( TIMER_START, &program_run_time );
process_list * target_process = NULL;
printf("Searching for target process...\n");
while ( !( target_process = find_process( TARGET_PROCESS ) ) )
Sleep( 100 );
printf("Found!\n\n");
print_node( target_process );
timer( TIMER_STOP, &program_run_time );
printf( "\n\n\t--\tProgram run time : %d milliseconds\t--\t\n\n", ( int )timer( TIMER_GETDIFF, &program_run_time ) );
free( target_process->process_name );
free( target_process );
return 0;
}
最佳答案
如果您可以在 Spy++ 中看到它,那么您应该可以通过 EnumWindows 获得它。
但是如果您的查询是特定于 ROBLOX 的,并且您想要的只是窗口句柄,那么您可以这样做
foreach (Process p in Process.GetProcesses())
{
if (p.MainWindowTitle == "Roblox - [Place1]")
{
rbxProc = p;
Console.WriteLine("FOUND ROBLOX Process");
}
}
然后你可以像这样得到主句柄:
rbxProc.MainWindowHandle
关于c++ - EnumWindows( ) 没有找到一些顶层窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19529780/
https://github.com/mattdiamond/Recorderjs/blob/master/recorder.js中的代码 我不明白 JavaScript 语法,比如 (functio
在 iOS 7 及更早版本中,如果我们想在应用程序中找到 topMostWindow,我们通常使用以下代码行 [[[UIApplication sharedApplication] windows]
我已经尝试解决这个问题很长一段时间了:我无法访问窗口的 url,因为它位于另一个域上..有一些解决方案吗? function login() { var cb = window.ope
是否可以将 FFMPEG 视频流传递到 C# 窗口?现在它在新窗口中作为新进程打开,我只是想将它传递给我自己的 SessionWindow。 此时我像这样执行ffplay: public void E
我有一个名为 x 的矩阵看起来像这样: pTime Close 1 1275087600 1.2268 2 1275264000 1.2264 3 1275264300 1.2
在编译时,发生搜索,grep搜索等,Emacs会在单独的窗口中创建一个新的缓冲区来显示结果,有没有自动跳转到那个窗口的方法?这很有用,因为我可以使用 n 和 p 而不是 M-g n 和 M-g p 移
我有一个启动 PowerShell 脚本的批处理文件。 批处理文件: START Powershell -executionpolicy RemoteSigned -noexit -file "MyS
我有一个基于菜单栏的应用程序,单击图标时会显示一个窗口。在 Mac OS X Lion 上一切正常,但由于某种原因,在 Snow Leopard 和早期版本的 Mac OS X 上会出现错误。任何时候
在 macOS 中,如何在 Xcode 和/或 Interface Builder 中创建带有“集成标题栏和工具栏”的窗口? 这是“宽标题栏”类型的窗口,已添加到 OS X 10.10 Yosemit
在浏览器 (Chrome) 中 JavaScript: var DataModler = { Data: { Something: 'value' }, Process: functi
我有 3 个 html 页面。第 1 页链接到第 2 页,第 2 页链接到第 3 页(为了简单起见)。 我希望页面 2 中的链接打开页面 3 并关闭页面 1(选项卡 1)。 据我了解,您无法使用 Ja
当点击“创建节点”按钮时,如何打开一个新的框架或窗口?我希望新框架包含一个文本字段和下拉菜单,以便用户可以选择一个选项。 Create node Search node
我有一个用户控件,用于编辑应用程序中的某些对象。 我最近遇到一个实例,我想弹出一个新的对话框(窗口)来托管此用户控件。 如何实例化新窗口并将需要设置的任何属性从窗口传递到用户控件? 感谢您的宝贵时间。
我有一个Observable,它发出许多对象,我想使用window或buffer操作对这些对象进行分组。但是,我不想指定count参数来确定窗口中应包含多少个对象,而是希望能够使用自定义条件。 例如,
我有以下代码,它打开一个新的 JavaFX 阶段(我们称之为窗口)。 openAlertBox.setOnAction(e -> { AlertBox alert = AlertBox
我要添加一个“在新窗口中打开”上下文菜单项,该菜单项将以新的UIScene打开我的应用程序文档之一。当然,我只想在实际上支持多个场景的设备上显示该菜单项。 目前,我只是在检查设备是否是使用旧设备的iP
我正在尝试创建一个 AIR 应用程序来记录应用程序的使用情况,使用 AIR 从系统获取信息的唯一简单方法是使用命令行工具和抓取 标准输出 . 我知道像 这样的工具顶部 和 ps 对于 OS X,但它们
所以我有这个简单的 turtle 螺旋制作器,我想知道是否有一种方法可以打印出由该程序创建的我的设计副本。 代码: import turtle x= float(input("Angle: ")) y
我正在编写一个 C# WPF 程序,它将文本消息发送到另一个程序的窗口。我有一个宏程序作为我的键盘驱动程序 (Logitech g15) 的一部分,它已经这样做了,尽管它不会将击键直接发送到进程,而是
我尝试使用以下代码通过 UDP 发送,但得到了奇怪的结果。 if((sendto(newSocket, sendBuf, totalLength, 0, (SOCKADDR *)&sendAd
我是一名优秀的程序员,十分优秀!