Skip to content

Commit

Permalink
udpate
Browse files Browse the repository at this point in the history
  • Loading branch information
ALTaleX531 committed Mar 17, 2024
1 parent 2dee7d9 commit 3a7332d
Show file tree
Hide file tree
Showing 13 changed files with 363 additions and 133 deletions.
29 changes: 24 additions & 5 deletions Common/Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include "Api.hpp"

using namespace TranslucentFlyouts;
namespace TranslucentFlyouts::Api::InteractiveIO
{
std::function<void()> g_callback{};
}

bool Api::IsServiceRunning(std::wstring_view serviceName)
{
Expand Down Expand Up @@ -197,12 +201,22 @@ bool Api::InteractiveIO::OutputToConsole(

void Api::InteractiveIO::Startup()
{
if (GetConsoleWindow() || AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())
HWND hwnd{ GetConsoleWindow() };
if (!hwnd)
{
FILE* fpstdin{ stdin }, * fpstdout{ stdout };
_wfreopen_s(&fpstdin, L"CONIN$", L"r", stdin);
_wfreopen_s(&fpstdout, L"CONOUT$", L"w+t", stdout);
_wsetlocale(LC_ALL, L"");
if (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())
{
hwnd = GetConsoleWindow();
FILE* fpstdin{ stdin }, * fpstdout{ stdout };
_wfreopen_s(&fpstdin, L"CONIN$", L"r", stdin);
_wfreopen_s(&fpstdout, L"CONOUT$", L"w+t", stdout);
_wsetlocale(LC_ALL, L"");

if (g_callback)
{
g_callback();
}
}
}
}

Expand All @@ -218,6 +232,11 @@ void Api::InteractiveIO::Shutdown()
}
}

void Api::InteractiveIO::SetConsoleInitializationCallback(const std::function<void()>&& callback)
{
g_callback = callback;
}

bool Api::IsPartDisabled(std::wstring_view part)
{
return IsPartDisabledExternally(part) ||
Expand Down
2 changes: 2 additions & 0 deletions Common/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace TranslucentFlyouts::Api
{
struct ServiceInfo
{
bool serviceRunning{ false };
HWND hostWindow{ nullptr };
HWINEVENTHOOK hook{ nullptr };
};
Expand Down Expand Up @@ -43,6 +44,7 @@ namespace TranslucentFlyouts::Api
);
void Startup();
void Shutdown();
void SetConsoleInitializationCallback(const std::function<void()>&& callback);
}

bool IsPartDisabled(std::wstring_view part);
Expand Down
2 changes: 2 additions & 0 deletions Common/framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <TlHelp32.h>
#include <delayimp.h>

#include <Propvarutil.h>
#include <propkey.h>
#include <appmodel.h>
#include <oleacc.h>
#include <taskschd.h>
Expand Down
87 changes: 57 additions & 30 deletions TFMain/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ namespace TranslucentFlyouts::Application
}
}

void Application::ClearCache()
{
#ifdef _WIN64
SHDeleteKeyW(
HKEY_LOCAL_MACHINE, L"SOFTWARE\\TranslucentFlyouts_Internals"
);
SHDeleteKeyW(
HKEY_CURRENT_USER, L"SOFTWARE\\TranslucentFlyouts_Internals"
);
#else
SHDeleteKeyW(
HKEY_LOCAL_MACHINE, L"SOFTWARE\\TranslucentFlyouts_Internals(x86)"
);
SHDeleteKeyW(
HKEY_CURRENT_USER, L"SOFTWARE\\TranslucentFlyouts_Internals(x86)"
);
#endif // _WIN64
}

HRESULT Application::InstallHook()
{
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE), !Api::IsServiceRunning(g_serviceName));
Expand Down Expand Up @@ -49,7 +68,7 @@ HRESULT Application::UninstallHook()
return S_OK;
}

HRESULT Application::StartService()
HRESULT Application::StartService(HWND hWnd)
{
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_SERVICE_ALREADY_RUNNING), Api::IsServiceRunning(g_serviceName));
auto [serviceHandle, serviceInfo]
Expand All @@ -58,6 +77,24 @@ HRESULT Application::StartService()
};
RETURN_LAST_ERROR_IF_NULL(serviceInfo);

if (!hWnd)
{
serviceInfo->hostWindow = CreateWindowExW(
WS_EX_NOREDIRECTIONBITMAP | WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW,
L"Static",
nullptr,
WS_POPUP,
0, 0, 0, 0,
nullptr, nullptr, wil::GetModuleInstanceHandle(), nullptr
);
RETURN_LAST_ERROR_IF_NULL(serviceInfo->hostWindow);
}
else
{
serviceInfo->hostWindow = hWnd;
}
RETURN_IF_WIN32_BOOL_FALSE(ChangeWindowMessageFilterEx(serviceInfo->hostWindow, GetStopMsg(), MSGFLT_ALLOW, nullptr));

Framework::Prepare();
Api::InteractiveIO::OutputToConsole(
Api::InteractiveIO::StringType::Notification,
Expand All @@ -69,17 +106,7 @@ HRESULT Application::StartService()
);
Api::InteractiveIO::Shutdown();
RETURN_IF_FAILED(Application::InstallHook());

serviceInfo->hostWindow = CreateWindowExW(
WS_EX_NOREDIRECTIONBITMAP | WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW,
L"Static",
nullptr,
WS_POPUP,
0, 0, 0, 0,
nullptr, nullptr, wil::GetModuleInstanceHandle(), nullptr
);
RETURN_LAST_ERROR_IF_NULL(serviceInfo->hostWindow);
RETURN_IF_WIN32_BOOL_FALSE(ChangeWindowMessageFilterEx(serviceInfo->hostWindow, GetStopMsg(), MSGFLT_ALLOW, nullptr));
serviceInfo->serviceRunning = true;

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

serviceInfo->hostWindow = nullptr;

Application::UninstallHook();
LOG_IF_FAILED(Application::UninstallHook());
serviceInfo->serviceRunning = false;
return S_OK;
}

Expand All @@ -118,7 +146,7 @@ HRESULT Application::StopService()
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE), !Api::IsServiceRunning(g_serviceName));
auto serviceInfo{ Api::GetServiceInfo(g_serviceName, false) };
RETURN_LAST_ERROR_IF_NULL(serviceInfo);
RETURN_HR_IF_NULL(HRESULT_FROM_WIN32(ERROR_SERVICE_START_HANG), serviceInfo->hostWindow);
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_SERVICE_START_HANG), !serviceInfo->serviceRunning);
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_WINDOW_HANDLE), !IsWindow(serviceInfo->hostWindow));
RETURN_LAST_ERROR_IF(!SendNotifyMessageW(serviceInfo->hostWindow, GetStopMsg(), 0, 0));

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

return S_OK;
}
HRESULT Application::KillService()
{
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_SERVICE_NOT_ACTIVE), !Api::IsServiceRunning(g_serviceName));
auto serviceInfo{ Api::GetServiceInfo(g_serviceName, false) };
RETURN_LAST_ERROR_IF_NULL(serviceInfo);
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_WINDOW_HANDLE), !IsWindow(serviceInfo->hostWindow));
DWORD processId{0};
RETURN_LAST_ERROR_IF(GetWindowThreadProcessId(serviceInfo->hostWindow, &processId) == 0);
wil::unique_handle processHandle{ OpenProcess(PROCESS_TERMINATE, FALSE, processId) };
RETURN_LAST_ERROR_IF_NULL(processHandle);
RETURN_IF_WIN32_BOOL_FALSE(TerminateProcess(processHandle.get(), E_ABORT));
return S_OK;
}

HRESULT Application::InstallApp() try
{
Expand Down Expand Up @@ -288,29 +329,15 @@ HRESULT Application::UninstallApp() try
nullptr,
MB_ICONINFORMATION | MB_YESNO
) == IDYES
)
)
{
SHDeleteKeyW(
HKEY_LOCAL_MACHINE, L"SOFTWARE\\TranslucentFlyouts"
);
SHDeleteKeyW(
HKEY_CURRENT_USER, L"SOFTWARE\\TranslucentFlyouts"
);
#ifdef _WIN64
SHDeleteKeyW(
HKEY_LOCAL_MACHINE, L"SOFTWARE\\TranslucentFlyouts_Internals"
);
SHDeleteKeyW(
HKEY_CURRENT_USER, L"SOFTWARE\\TranslucentFlyouts_Internals"
);
#else
SHDeleteKeyW(
HKEY_LOCAL_MACHINE, L"SOFTWARE\\TranslucentFlyouts_Internals(x86)"
);
SHDeleteKeyW(
HKEY_CURRENT_USER, L"SOFTWARE\\TranslucentFlyouts_Internals(x86)"
);
#endif // _WIN64
ClearCache();
}

if (
Expand Down
5 changes: 4 additions & 1 deletion TFMain/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ namespace TranslucentFlyouts::Application
// Service
extern "C" bool IsServiceRunning();
HRESULT StopService();
HRESULT StartService();
HRESULT KillService();
HRESULT StartService(HWND hWnd = nullptr);
// Installer.
HRESULT InstallApp();
HRESULT UninstallApp();

void ClearCache();
}
1 change: 0 additions & 1 deletion TFMain/Framework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ void CALLBACK Framework::HandleWinEvent(
{
if (Api::IsHostProcess(Application::g_serviceName))
{
Prepare();
DoExplorerCrashCheck();
return;
}
Expand Down
11 changes: 6 additions & 5 deletions TFMain/GlobalFunctions.def
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
LIBRARY
EXPORTS
Main PRIVATE
IsServiceRunning PRIVATE
StopService PRIVATE
StartService PRIVATE
InstallApp PRIVATE
UninstallApp PRIVATE
IsServiceRunning
StopService
KillService
StartService
InstallApp
UninstallApp
2 changes: 1 addition & 1 deletion TFMain/HookDispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ namespace TranslucentFlyouts::HookHelper

hookTable[index] = std::make_pair(
*iatFunctionAddress,
moduleHandleOffset.has_value() ?
iatModuleAddress.has_value() ?
std::make_optional(*reinterpret_cast<HMODULE*>(iatModuleAddress.value())) :
std::nullopt
);
Expand Down
17 changes: 9 additions & 8 deletions TFMain/SymbolResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,24 @@ HMODULE WINAPI SymbolResolver::MyLoadLibraryExW(
DWORD dwFlags
)
{
if (_wcsicmp(lpLibFileName, std::format(L"{}\\symsrv.dll", wil::GetSystemDirectoryW<std::wstring, MAX_PATH + 1>()).c_str()) != 0)
static auto s_symsrvSysFullPath{ std::format(L"{}\\symsrv.dll", wil::GetSystemDirectoryW<std::wstring, MAX_PATH + 1>()) };
static auto s_symsrvCurFullPath{ Utils::make_current_folder_file_str(L"symsrv.dll") };
if (
_wcsicmp(lpLibFileName, s_symsrvSysFullPath.c_str()) != 0 &&
_wcsicmp(lpLibFileName, s_symsrvCurFullPath.c_str()) != 0
)
{
return LoadLibraryExW(lpLibFileName, hFile, dwFlags);
}

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

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

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

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

HRESULT SymbolResolver::Walk(std::wstring_view dllName, std::string_view mask, std::function<bool(PSYMBOL_INFO, ULONG)> callback) try
Expand Down
14 changes: 14 additions & 0 deletions TFMain/TFMain.rc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ BEGIN
IDS_STRING108 "�����Ľ���Ѿ������浽ע����С�"
IDS_STRING109 "Windows ��Դ�������ڹ�ȥ30�����������������Σ��������ƺ�������һ�����صļ��������⣡���ڰ�ȫ�������Ƿ�ǿ����ֹTranslucentFlyouts.Win32��\n�����������������֣��뿼�DZ������ϵͳ�汾�������ߣ���"
IDS_STRING110 "�ܱ�Ǹ����Ӧ���׳���δ������쳣����������������ע�⣬�������ܲ�����TranslucentFlyouts��ɵģ�Ҳ�п�����Ӧ�ó�������\n�����Ѿ���TranslucentFlyouts�����ļ��е�dumpsĿ¼������һ���Ե�ǰ������Ϊ�ؼ����������ڴ�ת���ļ������в����������κ���˽��Ϣ���������Ϊ�ñ���������TF��ɵģ�ϣ��������Github�����issue���ϴ����ļ������ܰ������Ǹ���ط��ֲ��޸�BUG��\nΪ�˷�ֹӦ�ò��ϵر������Ƿ�ǿ����ֹTranslucentFlyouts.Win32��"
IDS_STRING111 "�㿴������Ϣ��ԭ���������ڳ��԰�װ��Դ����TranslucentFlyouts.Win32���ڰ�װ��������֮ǰ���Ҿ����б�Ҫ����һЩ��Ҫ��˵����\n\nTranslucentFlyouts.Win32������ԭ����ͨ��һ�������������ȫ�ֹ��ӴӶ���������DLLע�뵽���󲿷ֵĽ���֮�У�ͨ�����������غ��ض������ϵͳ�������Ⱦ����ʵ��͸��Ч����������Ϊ����Ӧ���趨��������Ϣ�����ע�����Ŀ���ж�ȡ������ζ�ţ���Ӧ���ڲ�֧�ֵ�ϵͳ�����п��ܻ��������Ӧ���쳣�������Ӷ�������ĵ����޷�����ʹ�á�������װ����Ϊ��Ը�������Ӧ��ִ��������������Ӧ��Ȩ�ޣ���Ը��е���Ӧ�ķ��ա�\n\n��װ�����ᴴ��һ���ƻ�����ʹ��ĵ��Կ�������һ��������̣����Ҹ���dbghelp-for-tf.dll��system32�ļ���ȷ����DLL��ע�뵽����������̺�Ҳ��������ʼ����\n\n������ǡ���������װ����������񡱽���ֹ��װ����ĵ��Բ����ܵ��κθ��ġ�"
END

STRINGTABLE
BEGIN
IDS_STRING112 "��ã�"
IDS_STRING113 "ռλ��"
END

#endif // ����(���壬�й�) resources
Expand Down Expand Up @@ -92,6 +99,13 @@ BEGIN
IDS_STRING108 "The results of resolved symbols have been saved into the registry."
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!)"
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?"
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."
END

STRINGTABLE
BEGIN
IDS_STRING112 "Hello "
IDS_STRING113 "PLACEHOLDER"
END

#endif // Ӣ��(����) resources
Expand Down
Loading

0 comments on commit 3a7332d

Please sign in to comment.