gpt4 book ai didi

c++ - 从进程名称获取窗口标题

转载 作者:太空狗 更新时间:2023-10-29 21:45:16 25 4
gpt4 key购买 nike

我正在尝试编写一个程序来获取进程的窗口标题。在我描述问题之前,这里是代码:

#include <Windows.h>
#include <string>
#include <Psapi.h>
#include <algorithm>

std::string window_title;
std::string search_for;

BOOL CALLBACK EnumWindowCallback(HWND hWindow, LPARAM param)
{
if ( IsWindow( hWindow ) == TRUE )
{
DWORD pid = 0;

if ( GetWindowThreadProcessId( hWindow, &pid ) != 0 )
{
HANDLE hProcess;
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pid );
if ( hProcess != 0 )
{
std::string path;
CHAR name[MAX_PATH];
GetModuleFileNameExA( hProcess, NULL, name, sizeof(name) / sizeof(CHAR) );
path = name;
unsigned int slash = path.find_last_of('\\');
if ( slash != std::string::npos ){
std::string proc_name = path.substr( slash + 1, path.length() );
std::transform(proc_name.begin(), proc_name.end(), proc_name.begin(), ::tolower);
if ( proc_name == search_for )
{
CHAR finalTitle[MAX_PATH];
ZeroMemory( finalTitle, sizeof(finalTitle) );
SendMessageA( hWindow, WM_GETTEXT, (WPARAM)sizeof(CHAR)/sizeof(MAX_PATH), (LPARAM)finalTitle );
window_title = finalTitle;
return FALSE;
}
}
}
}
}
return TRUE;
};

const char* __stdcall GetWinTitleByProcessName( const char* title )
{
search_for = title;
std::transform(search_for.begin(), search_for.end(), search_for.begin(), ::tolower);
if ( EnumWindows( (WNDENUMPROC)EnumWindowCallback, 0 ) == FALSE )
{
return window_title.c_str();
}

return "NOTFOUND";
}

int WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
{
MessageBoxA( NULL, GetWinTitleByProcessName("chrome.exe"), "Test", MB_OK);
}

该程序到目前为止一直有效,直到我想要获得窗口的实际标题。我尝试了 GetWindowText 和 SendMessage,如此处所示。这两种方法都返回空字符串。

如何获取窗口标题?

最佳答案

以下代码适用于类似的问题。在我的例子中,我正在寻找应用程序的 Windows 句柄,以便我可以为 dll 添加父级。我通过标题识别该应用程序。它的 C++Builder 代码,所以有些部分可能不熟悉。我会评论我发现的差异。最主要的是应用程序的使用,我不确定非 Embarcadero 的等价物是什么,但每个运行的代码实例都有一个管理消息循环等的应用程序实例。我将我的 dll 的 Application->Handle 设置为调用应用程序 hWnd 以使其远离任务栏等。此代码适用于 xp、vista 32 和 win7 64。

   void HideDLL() {
if (Application->Handle == 0) {
SearchObject *so = new SearchObject();
so->Caption = L"MyCallingApp";
so->Handle = 0;
EnumWindows((WNDENUMPROC)EnumWindowsProc, (long)so);
Application->Handle = so->Handle;

delete so;
}

}
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lparam) {

bool result;
SearchObject *so = (SearchObject*)lparam;
wchar_t *caption = new wchar_t[STR_DEFAULT];

GetWindowTextW(hWnd, caption, STR_DEFAULT);
// String is an Embarcadero type representing UnicodeString
String Caption = caption;
// Pos is a wrapper around strstr I think
// the actual caption in my case is decorated with some other stuff
// I only know that it will start with the name of the app
if (Caption.Pos(so->Caption) > 0) {
so->Handle = hWnd;
result = false;
} else {
result = true;
}
delete caption;
return result;
}

希望这对您有所帮助。

关于c++ - 从进程名称获取窗口标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17950500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com