Skip to content
This repository was archived by the owner on Jan 4, 2025. It is now read-only.

Commit 3a7332d

Browse files
committed
udpate
1 parent 2dee7d9 commit 3a7332d

13 files changed

+363
-133
lines changed

Common/Api.cpp

+24-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include "Api.hpp"
66

77
using namespace TranslucentFlyouts;
8+
namespace TranslucentFlyouts::Api::InteractiveIO
9+
{
10+
std::function<void()> g_callback{};
11+
}
812

913
bool Api::IsServiceRunning(std::wstring_view serviceName)
1014
{
@@ -197,12 +201,22 @@ bool Api::InteractiveIO::OutputToConsole(
197201

198202
void Api::InteractiveIO::Startup()
199203
{
200-
if (GetConsoleWindow() || AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())
204+
HWND hwnd{ GetConsoleWindow() };
205+
if (!hwnd)
201206
{
202-
FILE* fpstdin{ stdin }, * fpstdout{ stdout };
203-
_wfreopen_s(&fpstdin, L"CONIN$", L"r", stdin);
204-
_wfreopen_s(&fpstdout, L"CONOUT$", L"w+t", stdout);
205-
_wsetlocale(LC_ALL, L"");
207+
if (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())
208+
{
209+
hwnd = GetConsoleWindow();
210+
FILE* fpstdin{ stdin }, * fpstdout{ stdout };
211+
_wfreopen_s(&fpstdin, L"CONIN$", L"r", stdin);
212+
_wfreopen_s(&fpstdout, L"CONOUT$", L"w+t", stdout);
213+
_wsetlocale(LC_ALL, L"");
214+
215+
if (g_callback)
216+
{
217+
g_callback();
218+
}
219+
}
206220
}
207221
}
208222

@@ -218,6 +232,11 @@ void Api::InteractiveIO::Shutdown()
218232
}
219233
}
220234

235+
void Api::InteractiveIO::SetConsoleInitializationCallback(const std::function<void()>&& callback)
236+
{
237+
g_callback = callback;
238+
}
239+
221240
bool Api::IsPartDisabled(std::wstring_view part)
222241
{
223242
return IsPartDisabledExternally(part) ||

Common/Api.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace TranslucentFlyouts::Api
66
{
77
struct ServiceInfo
88
{
9+
bool serviceRunning{ false };
910
HWND hostWindow{ nullptr };
1011
HWINEVENTHOOK hook{ nullptr };
1112
};
@@ -43,6 +44,7 @@ namespace TranslucentFlyouts::Api
4344
);
4445
void Startup();
4546
void Shutdown();
47+
void SetConsoleInitializationCallback(const std::function<void()>&& callback);
4648
}
4749

4850
bool IsPartDisabled(std::wstring_view part);

Common/framework.h

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <TlHelp32.h>
1515
#include <delayimp.h>
1616

17+
#include <Propvarutil.h>
18+
#include <propkey.h>
1719
#include <appmodel.h>
1820
#include <oleacc.h>
1921
#include <taskschd.h>

TFMain/Application.cpp

+57-30
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ namespace TranslucentFlyouts::Application
1616
}
1717
}
1818

19+
void Application::ClearCache()
20+
{
21+
#ifdef _WIN64
22+
SHDeleteKeyW(
23+
HKEY_LOCAL_MACHINE, L"SOFTWARE\\TranslucentFlyouts_Internals"
24+
);
25+
SHDeleteKeyW(
26+
HKEY_CURRENT_USER, L"SOFTWARE\\TranslucentFlyouts_Internals"
27+
);
28+
#else
29+
SHDeleteKeyW(
30+
HKEY_LOCAL_MACHINE, L"SOFTWARE\\TranslucentFlyouts_Internals(x86)"
31+
);
32+
SHDeleteKeyW(
33+
HKEY_CURRENT_USER, L"SOFTWARE\\TranslucentFlyouts_Internals(x86)"
34+
);
35+
#endif // _WIN64
36+
}
37+
1938
HRESULT Application::InstallHook()
2039
{
2140
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE), !Api::IsServiceRunning(g_serviceName));
@@ -49,7 +68,7 @@ HRESULT Application::UninstallHook()
4968
return S_OK;
5069
}
5170

52-
HRESULT Application::StartService()
71+
HRESULT Application::StartService(HWND hWnd)
5372
{
5473
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_SERVICE_ALREADY_RUNNING), Api::IsServiceRunning(g_serviceName));
5574
auto [serviceHandle, serviceInfo]
@@ -58,6 +77,24 @@ HRESULT Application::StartService()
5877
};
5978
RETURN_LAST_ERROR_IF_NULL(serviceInfo);
6079

80+
if (!hWnd)
81+
{
82+
serviceInfo->hostWindow = CreateWindowExW(
83+
WS_EX_NOREDIRECTIONBITMAP | WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW,
84+
L"Static",
85+
nullptr,
86+
WS_POPUP,
87+
0, 0, 0, 0,
88+
nullptr, nullptr, wil::GetModuleInstanceHandle(), nullptr
89+
);
90+
RETURN_LAST_ERROR_IF_NULL(serviceInfo->hostWindow);
91+
}
92+
else
93+
{
94+
serviceInfo->hostWindow = hWnd;
95+
}
96+
RETURN_IF_WIN32_BOOL_FALSE(ChangeWindowMessageFilterEx(serviceInfo->hostWindow, GetStopMsg(), MSGFLT_ALLOW, nullptr));
97+
6198
Framework::Prepare();
6299
Api::InteractiveIO::OutputToConsole(
63100
Api::InteractiveIO::StringType::Notification,
@@ -69,17 +106,7 @@ HRESULT Application::StartService()
69106
);
70107
Api::InteractiveIO::Shutdown();
71108
RETURN_IF_FAILED(Application::InstallHook());
72-
73-
serviceInfo->hostWindow = CreateWindowExW(
74-
WS_EX_NOREDIRECTIONBITMAP | WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW,
75-
L"Static",
76-
nullptr,
77-
WS_POPUP,
78-
0, 0, 0, 0,
79-
nullptr, nullptr, wil::GetModuleInstanceHandle(), nullptr
80-
);
81-
RETURN_LAST_ERROR_IF_NULL(serviceInfo->hostWindow);
82-
RETURN_IF_WIN32_BOOL_FALSE(ChangeWindowMessageFilterEx(serviceInfo->hostWindow, GetStopMsg(), MSGFLT_ALLOW, nullptr));
109+
serviceInfo->serviceRunning = true;
83110

84111
auto callback = [](HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR, DWORD_PTR) -> LRESULT
85112
{
@@ -104,7 +131,8 @@ HRESULT Application::StartService()
104131

105132
serviceInfo->hostWindow = nullptr;
106133

107-
Application::UninstallHook();
134+
LOG_IF_FAILED(Application::UninstallHook());
135+
serviceInfo->serviceRunning = false;
108136
return S_OK;
109137
}
110138

@@ -118,7 +146,7 @@ HRESULT Application::StopService()
118146
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE), !Api::IsServiceRunning(g_serviceName));
119147
auto serviceInfo{ Api::GetServiceInfo(g_serviceName, false) };
120148
RETURN_LAST_ERROR_IF_NULL(serviceInfo);
121-
RETURN_HR_IF_NULL(HRESULT_FROM_WIN32(ERROR_SERVICE_START_HANG), serviceInfo->hostWindow);
149+
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_SERVICE_START_HANG), !serviceInfo->serviceRunning);
122150
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_WINDOW_HANDLE), !IsWindow(serviceInfo->hostWindow));
123151
RETURN_LAST_ERROR_IF(!SendNotifyMessageW(serviceInfo->hostWindow, GetStopMsg(), 0, 0));
124152

@@ -135,6 +163,19 @@ HRESULT Application::StopService()
135163

136164
return S_OK;
137165
}
166+
HRESULT Application::KillService()
167+
{
168+
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE), !Api::IsServiceRunning(g_serviceName));
169+
auto serviceInfo{ Api::GetServiceInfo(g_serviceName, false) };
170+
RETURN_LAST_ERROR_IF_NULL(serviceInfo);
171+
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_WINDOW_HANDLE), !IsWindow(serviceInfo->hostWindow));
172+
DWORD processId{0};
173+
RETURN_LAST_ERROR_IF(GetWindowThreadProcessId(serviceInfo->hostWindow, &processId) == 0);
174+
wil::unique_handle processHandle{ OpenProcess(PROCESS_TERMINATE, FALSE, processId) };
175+
RETURN_LAST_ERROR_IF_NULL(processHandle);
176+
RETURN_IF_WIN32_BOOL_FALSE(TerminateProcess(processHandle.get(), E_ABORT));
177+
return S_OK;
178+
}
138179

139180
HRESULT Application::InstallApp() try
140181
{
@@ -288,29 +329,15 @@ HRESULT Application::UninstallApp() try
288329
nullptr,
289330
MB_ICONINFORMATION | MB_YESNO
290331
) == IDYES
291-
)
332+
)
292333
{
293334
SHDeleteKeyW(
294335
HKEY_LOCAL_MACHINE, L"SOFTWARE\\TranslucentFlyouts"
295336
);
296337
SHDeleteKeyW(
297338
HKEY_CURRENT_USER, L"SOFTWARE\\TranslucentFlyouts"
298339
);
299-
#ifdef _WIN64
300-
SHDeleteKeyW(
301-
HKEY_LOCAL_MACHINE, L"SOFTWARE\\TranslucentFlyouts_Internals"
302-
);
303-
SHDeleteKeyW(
304-
HKEY_CURRENT_USER, L"SOFTWARE\\TranslucentFlyouts_Internals"
305-
);
306-
#else
307-
SHDeleteKeyW(
308-
HKEY_LOCAL_MACHINE, L"SOFTWARE\\TranslucentFlyouts_Internals(x86)"
309-
);
310-
SHDeleteKeyW(
311-
HKEY_CURRENT_USER, L"SOFTWARE\\TranslucentFlyouts_Internals(x86)"
312-
);
313-
#endif // _WIN64
340+
ClearCache();
314341
}
315342

316343
if (

TFMain/Application.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ namespace TranslucentFlyouts::Application
1717
// Service
1818
extern "C" bool IsServiceRunning();
1919
HRESULT StopService();
20-
HRESULT StartService();
20+
HRESULT KillService();
21+
HRESULT StartService(HWND hWnd = nullptr);
2122
// Installer.
2223
HRESULT InstallApp();
2324
HRESULT UninstallApp();
25+
26+
void ClearCache();
2427
}

TFMain/Framework.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ void CALLBACK Framework::HandleWinEvent(
111111
{
112112
if (Api::IsHostProcess(Application::g_serviceName))
113113
{
114-
Prepare();
115114
DoExplorerCrashCheck();
116115
return;
117116
}

TFMain/GlobalFunctions.def

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
LIBRARY
22
EXPORTS
33
Main PRIVATE
4-
IsServiceRunning PRIVATE
5-
StopService PRIVATE
6-
StartService PRIVATE
7-
InstallApp PRIVATE
8-
UninstallApp PRIVATE
4+
IsServiceRunning
5+
StopService
6+
KillService
7+
StartService
8+
InstallApp
9+
UninstallApp

TFMain/HookDispatcher.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ namespace TranslucentFlyouts::HookHelper
135135

136136
hookTable[index] = std::make_pair(
137137
*iatFunctionAddress,
138-
moduleHandleOffset.has_value() ?
138+
iatModuleAddress.has_value() ?
139139
std::make_optional(*reinterpret_cast<HMODULE*>(iatModuleAddress.value())) :
140140
std::nullopt
141141
);

TFMain/SymbolResolver.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,24 @@ HMODULE WINAPI SymbolResolver::MyLoadLibraryExW(
6363
DWORD dwFlags
6464
)
6565
{
66-
if (_wcsicmp(lpLibFileName, std::format(L"{}\\symsrv.dll", wil::GetSystemDirectoryW<std::wstring, MAX_PATH + 1>()).c_str()) != 0)
66+
static auto s_symsrvSysFullPath{ std::format(L"{}\\symsrv.dll", wil::GetSystemDirectoryW<std::wstring, MAX_PATH + 1>()) };
67+
static auto s_symsrvCurFullPath{ Utils::make_current_folder_file_str(L"symsrv.dll") };
68+
if (
69+
_wcsicmp(lpLibFileName, s_symsrvSysFullPath.c_str()) != 0 &&
70+
_wcsicmp(lpLibFileName, s_symsrvCurFullPath.c_str()) != 0
71+
)
6772
{
6873
return LoadLibraryExW(lpLibFileName, hFile, dwFlags);
6974
}
7075

71-
WCHAR curDir[MAX_PATH + 1]{};
72-
THROW_LAST_ERROR_IF(GetModuleFileName(wil::GetModuleInstanceHandle(), curDir, MAX_PATH) == 0);
73-
THROW_IF_FAILED(PathCchRemoveFileSpec(curDir, MAX_PATH));
74-
THROW_IF_FAILED(PathCchAppend(curDir, MAX_PATH, L"symsrv.dll"));
75-
return LoadLibraryW(curDir);
76+
return LoadLibraryW(Utils::make_current_folder_file_str(L"symsrv-for-tf.dll").c_str());
7677
}
7778

7879
SymbolResolver::SymbolResolver(std::wstring_view sessionName) : m_sessionName{ sessionName }
7980
{
8081
try
8182
{
82-
m_LoadLibraryExW_Org = HookHelper::WriteIAT(GetModuleHandleW(L"dbghelp.dll"), "api-ms-win-core-libraryloader-l1-1-0.dll", "LoadLibraryExW", MyLoadLibraryExW);
83+
m_LoadLibraryExW_Org = HookHelper::WriteIAT(GetModuleHandleW(L"dbghelp-for-tf.dll"), "api-ms-win-core-libraryloader-l1-1-0.dll", "LoadLibraryExW", MyLoadLibraryExW);
8384

8485
THROW_IF_WIN32_BOOL_FALSE(SymInitialize(GetCurrentProcess(), nullptr, FALSE));
8586

@@ -103,7 +104,7 @@ SymbolResolver::SymbolResolver(std::wstring_view sessionName) : m_sessionName{ s
103104
SymbolResolver::~SymbolResolver() noexcept
104105
{
105106
SymCleanup(GetCurrentProcess());
106-
m_LoadLibraryExW_Org = HookHelper::WriteIAT(GetModuleHandleW(L"dbghelp.dll"), "api-ms-win-core-libraryloader-l1-1-0.dll", "LoadLibraryExW", m_LoadLibraryExW_Org);
107+
m_LoadLibraryExW_Org = HookHelper::WriteIAT(GetModuleHandleW(L"dbghelp-for-tf.dll"), "api-ms-win-core-libraryloader-l1-1-0.dll", "LoadLibraryExW", m_LoadLibraryExW_Org);
107108
}
108109

109110
HRESULT SymbolResolver::Walk(std::wstring_view dllName, std::string_view mask, std::function<bool(PSYMBOL_INFO, ULONG)> callback) try

TFMain/TFMain.rc

+14
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ BEGIN
6262
IDS_STRING108 "�����Ľ���Ѿ������浽ע����С�"
6363
IDS_STRING109 "Windows ��Դ�������ڹ�ȥ30�����������������Σ��������ƺ�������һ�����صļ��������⣡���ڰ�ȫ�������Ƿ�ǿ����ֹTranslucentFlyouts.Win32��\n�����������������֣��뿼�DZ������ϵͳ�汾�������ߣ���"
6464
IDS_STRING110 "�ܱ�Ǹ����Ӧ���׳���δ������쳣����������������ע�⣬�������ܲ�����TranslucentFlyouts��ɵģ�Ҳ�п�����Ӧ�ó�������\n�����Ѿ���TranslucentFlyouts�����ļ��е�dumpsĿ¼������һ���Ե�ǰ������Ϊ�ؼ����������ڴ�ת���ļ������в����������κ���˽��Ϣ���������Ϊ�ñ���������TF��ɵģ�ϣ��������Github�����issue���ϴ����ļ������ܰ������Ǹ���ط��ֲ��޸�BUG��\nΪ�˷�ֹӦ�ò��ϵر������Ƿ�ǿ����ֹTranslucentFlyouts.Win32��"
65+
IDS_STRING111 "�㿴������Ϣ��ԭ���������ڳ��԰�װ��Դ����TranslucentFlyouts.Win32���ڰ�װ��������֮ǰ���Ҿ����б�Ҫ����һЩ��Ҫ��˵����\n\nTranslucentFlyouts.Win32������ԭ����ͨ��һ�������������ȫ�ֹ��ӴӶ���������DLLע�뵽���󲿷ֵĽ���֮�У�ͨ�����������غ��ض������ϵͳ�������Ⱦ����ʵ��͸��Ч����������Ϊ����Ӧ���趨��������Ϣ�����ע�����Ŀ���ж�ȡ������ζ�ţ���Ӧ���ڲ�֧�ֵ�ϵͳ�����п��ܻ��������Ӧ���쳣�������Ӷ�������ĵ����޷�����ʹ�á�������װ����Ϊ��Ը�������Ӧ��ִ��������������Ӧ��Ȩ�ޣ���Ը��е���Ӧ�ķ��ա�\n\n��װ�����ᴴ��һ���ƻ�����ʹ��ĵ��Կ�������һ��������̣����Ҹ���dbghelp-for-tf.dll��system32�ļ���ȷ����DLL��ע�뵽����������̺�Ҳ��������ʼ����\n\n������ǡ���������װ����������񡱽���ֹ��װ����ĵ��Բ����ܵ��κθ��ġ�"
66+
END
67+
68+
STRINGTABLE
69+
BEGIN
70+
IDS_STRING112 "��ã�"
71+
IDS_STRING113 "ռλ��"
6572
END
6673

6774
#endif // ����(���壬�й�) resources
@@ -92,6 +99,13 @@ BEGIN
9299
IDS_STRING108 "The results of resolved symbols have been saved into the registry."
93100
IDS_STRING109 "Windows Explorer crashed twice in the last 30 seconds! It seems like there is a severe compatibility issue! For security reasons, do you want to terminate TranslucentFlyouts.Win32 now?\n(PS: Please consider reporting your OS information to the developer if you encounter this situation again!)"
94101
IDS_STRING110 "Sorry, this application has thrown an uncaught exception and is about to crash. Note, however, that the crash may not be caused by TranslucentFlyouts, or it may be the application itself.\nWe have generated a memory dump file in the dumps directory of the folder where TranslucentFlyouts is located named after the current process name as a keyword, which does not contain any of your private information. If you think the crash issue is caused by TF, then we hope you can raise an issue on Github and upload the file, which will help us find and fix the bug quicker.\nTo prevent the app from crashing constantly, do you want to terminate TranslucentFlyouts.Win32 now?"
102+
IDS_STRING111 "The reason you are seeing this message is that you are attempting to install the open-source software TranslucentFlyouts.Win32. Before proceeding with the installation, I feel it is necessary to provide some clarification.\n\nTranslucentFlyouts.Win32 works by setting global hooks through a service process to inject its own DLLs into most of the processes. It achieves transparency effects by analyzing, intercepting, and redirecting the rendering code of relevant system components, which also includes reading registry items in response to the configuration information you set. This means that running this application on unsupported system may cause other applications to crash, making your computer unusable. Proceeding with the installation will be considered as granting the application the permissions to perform the above operations at your own risk.\n\nThe installation will create a scheduled task to run a service process when your computer starts, and it will copy dbghelp-for-tf.dll to the system32 folder to ensure that the DLL can properly initialize even after being injected into some special processes.\n\nClicking �Yes� will continue the installation, while clicking �No� will abort the installation without introducing any changes to your computer."
103+
END
104+
105+
STRINGTABLE
106+
BEGIN
107+
IDS_STRING112 "Hello "
108+
IDS_STRING113 "PLACEHOLDER"
95109
END
96110

97111
#endif // Ӣ��(����) resources

0 commit comments

Comments
 (0)