diff --git a/src/back/backsessionclasses.cpp b/src/back/backsessionclasses.cpp index 1bfc37f..aafe147 100644 --- a/src/back/backsessionclasses.cpp +++ b/src/back/backsessionclasses.cpp @@ -129,12 +129,15 @@ Session::Session(Endpoint* ep, IAudioSessionControl2* sessionControl, size_t idx if (!wcscmp(sessionDisplayName, L"")) { std::wstring exePath; if (getExePath(pid, &exePath)) { + this->sessionName = exePath; if (fetchName(exePath, pid)) goto nameFound; } if (fetchNameViaWindowName(pid, &this->sessionName)) goto nameFound; - } else + } else { this->sessionName = std::wstring(sessionDisplayName); - + goto nameFound; + } + nameFound: CoTaskMemFree(sessionDisplayName); } @@ -199,37 +202,21 @@ void Session::setMute(NGuid guid, bool muted) { } bool Session::getExePath(DWORD pid, std::wstring *exePath) { - /* - * https://learn.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot - * https://stackoverflow.com/questions/11843368/how-to-get-process-description - * https://notes.indezine.com/2018/05/microsoft-locale-ids.html#:~:text=Wait%2C%201033%20is%20the%20decimal,ID%20for%20English%20%E2%80%93%20United%20States. - * https://stackoverflow.com/questions/64321036/c-win32-getting-app-name-using-pid-and-executable-path - */ - //std::wstring msixName; - - HANDLE processList = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid); - if (processList == INVALID_HANDLE_VALUE) { + HANDLE processHandle; + wchar_t fileName[UNICODE_STRING_MAX_CHARS]; + processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); + if (processHandle != NULL) { + if (GetModuleFileNameEx(processHandle, NULL, fileName, UNICODE_STRING_MAX_CHARS) == 0) { + CloseHandle(processHandle); + return false; + } + } else { log_wdebugcpp(L"aye no procname. -> " + std::to_wstring(GetLastError())); return false; } - MODULEENTRY32W me32w; - me32w.dwSize = sizeof(MODULEENTRY32W); - if(Module32FirstW(processList, &me32w)) { - do { - if (me32w.th32ProcessID == pid) { - *exePath = std::wstring(me32w.szExePath); - break; - /* - * However, if the calling process is a 32-bit process, you must call the - * QueryFullProcessImageName function to retrieve the full path of the - * executable file for a 64-bit process. - */ - } - } while(Module32NextW(processList, &me32w)); - } - CloseHandle(processList); + *exePath = std::wstring(fileName); return true; } @@ -362,8 +349,8 @@ bool Session::fetchNameViaWindowName(DWORD pid, std::wstring *sessionName) { auto pParams = (std::pair*)(lParam); DWORD processId; - //&& GetWindow(hwnd, GW_OWNER) == 0 - if (IsWindowVisible(hwnd) && GetWindowThreadProcessId(hwnd, &processId) && processId == pParams->second) { + //IsWindowVisible(hwnd) &&&& GetWindow(hwnd, GW_OWNER) == 0 + if ( GetWindowThreadProcessId(hwnd, &processId) && processId == pParams->second) { int length = GetWindowTextLength(hwnd); if (!length) return TRUE; diff --git a/src/back/msinclude.h b/src/back/msinclude.h index 633e079..52fccbd 100644 --- a/src/back/msinclude.h +++ b/src/back/msinclude.h @@ -17,6 +17,7 @@ #include #include #include +#include //#include #include