Move GetModuleFileNameExW usage and create a helper function to obtain process image path

This commit is contained in:
Jesús Sanz del Rey 2024-05-15 02:48:15 +02:00
parent 011af792ec
commit 657988f24a
No known key found for this signature in database
GPG key ID: CE5EA6FC54AAB4F0
2 changed files with 32 additions and 28 deletions

View file

@ -340,6 +340,10 @@ GetVersionInfoString(IN LPCWSTR szFileName,
OUT LPWSTR szBuffer,
IN UINT cbBufLen);
BOOL GetProcessPath(IN DWORD dwProcessId,
OUT LPWSTR szBuffer,
IN DWORD cbBufLen);
class CTaskSwitchWnd :
public CComCoClass<CTaskSwitchWnd>,
public CComObjectRootEx<CComMultiThreadModelNoCS>,
@ -681,18 +685,14 @@ public:
WCHAR windowText[255];
/* Open process to retrieve the filename of the executable */
HANDLE hTaskProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, TaskGroup->dwProcessId);
if(hTaskProc != NULL) {
WCHAR ExePath[MAX_PATH] = {};
if(GetModuleFileNameExW(hTaskProc, NULL, ExePath, _countof(ExePath)) && GetVersionInfoString(ExePath, L"FileDescription", windowText, _countof(windowText))) {
WCHAR ExePath[MAX_PATH] = {};
if(GetProcessPath(TaskGroup->dwProcessId, ExePath, _countof(ExePath)))
{
if(GetVersionInfoString(ExePath, L"FileDescription", windowText, _countof(windowText)))
tbbi.pszText = windowText;
}
if (ExtractIconExW(ExePath, 0, NULL, &icon, 1) <= 0)
icon = static_cast<HICON>(LoadImageW(NULL, MAKEINTRESOURCEW(OIC_SAMPLE), IMAGE_ICON, 0, 0, LR_SHARED | LR_DEFAULTSIZE));
CloseHandle(hTaskProc);
}
tbbi.cbSize = sizeof(tbbi);
@ -1007,18 +1007,15 @@ public:
}
/* Open process to retrieve the filename of the executable */
HANDLE hTaskProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, TaskGroup->dwProcessId);
if(hTaskProc != NULL) {
WCHAR ExePath[MAX_PATH] = {};
if(GetModuleFileNameExW(hTaskProc, NULL, ExePath, _countof(ExePath)) && GetVersionInfoString(ExePath, L"FileDescription", windowText, _countof(windowText))) {
WCHAR ExePath[MAX_PATH] = {};
if(GetProcessPath(TaskGroup->dwProcessId, ExePath, _countof(ExePath)))
{
if(GetVersionInfoString(ExePath, L"FileDescription", windowText, _countof(windowText))) {
tbBtn.iString = (DWORD_PTR) windowText;
}
if (ExtractIconExW(ExePath, -1, NULL, &icon, 1) <= 0)
icon = static_cast<HICON>(LoadImageW(NULL, MAKEINTRESOURCEW(OIC_SAMPLE), IMAGE_ICON, 0, 0, LR_SHARED | LR_DEFAULTSIZE));
CloseHandle(hTaskProc);
}
TaskGroup->IconIndex = ImageList_ReplaceIcon(m_ImageList, -1, icon);
@ -1239,13 +1236,7 @@ public:
}
WCHAR ItemExePath[MAX_PATH] = {0};
HANDLE hTaskProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessId);
if (hTaskProc != NULL)
{
GetModuleFileNameExW(hTaskProc, NULL, ItemExePath, _countof(ItemExePath));
CloseHandle(hTaskProc);
}
GetProcessPath(dwProcessId, ItemExePath, _countof(ItemExePath));
/* Try to find an existing task group */
TaskGroup = m_TaskGroups;
@ -1260,12 +1251,7 @@ public:
else
{
WCHAR ExePath[MAX_PATH] = {0};
hTaskProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, TaskGroup->dwProcessId);
if (hTaskProc != NULL)
{
GetModuleFileNameExW(hTaskProc, NULL, ExePath, _countof(ExePath));
CloseHandle(hTaskProc);
}
GetProcessPath(dwProcessId, ExePath, _countof(ExePath));
if(!lstrcmpW(ExePath, ItemExePath))
{

View file

@ -1,5 +1,6 @@
#include "precomp.h"
#include <winver.h>
#include <psapi.h>
typedef struct _LANGCODEPAGE
{
@ -257,3 +258,20 @@ GetVersionInfoString(IN LPCWSTR szFileName,
return bRet;
}
BOOL GetProcessPath(IN DWORD dwProcessId,
OUT LPWSTR szBuffer,
IN DWORD cbBufLen)
{
HANDLE hTaskProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessId);
BOOL bRet = FALSE;
if(hTaskProc != NULL) {
if(GetModuleFileNameExW(hTaskProc, NULL, szBuffer, cbBufLen)) {
bRet = TRUE;
}
CloseHandle(hTaskProc);
}
return bRet;
}