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

View file

@ -1,5 +1,6 @@
#include "precomp.h" #include "precomp.h"
#include <winver.h> #include <winver.h>
#include <psapi.h>
typedef struct _LANGCODEPAGE typedef struct _LANGCODEPAGE
{ {
@ -257,3 +258,20 @@ GetVersionInfoString(IN LPCWSTR szFileName,
return bRet; 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;
}