Files
WinDurango/dlls/kernelx/dllmain.cpp
T
Tyler Jaacks 9a88282228 Fixed WinRT.
2024-09-13 00:21:05 -05:00

75 lines
2.1 KiB
C++

// ReSharper disable CppInconsistentNaming
// ReSharper disable CppParameterMayBeConst
// ReSharper disable CppClangTidyClangDiagnosticMicrosoftCast
// ReSharper disable CppClangTidyClangDiagnosticUndefinedReinterpretCast
// ReSharper disable CppClangTidyClangDiagnosticShadow
// ReSharper disable CppClangTidyClangDiagnosticCastFunctionTypeStrict
#include "pch.h"
using namespace Microsoft::WRL;
typedef HRESULT(*DllGetActivationFactoryFunc) (HSTRING, IActivationFactory**);
DllGetActivationFactoryFunc pDllGetActivationFactory = nullptr;
HRESULT(WINAPI* TrueRoGetActivationFactory)(HSTRING classId, REFIID iid, void** factory) = RoGetActivationFactory;
HRESULT WINAPI RoGetActivationFactory_Hook(HSTRING classId, REFIID iid, void** factory)
{
auto hr = TrueRoGetActivationFactory(classId, iid, factory);
const std::wstring message = std::wstring(L"classId: ") +
WindowsGetStringRawBuffer(classId, nullptr);
if (FAILED(hr))
{
auto library = LoadPackagedLibrary(L"winrt_x.dll", 0);
if (!library) library = LoadLibraryW(L"winrt_x.dll");
if (!library) return hr;
auto error = GetLastError();
pDllGetActivationFactory = reinterpret_cast<DllGetActivationFactoryFunc>
(GetProcAddress(library, "DllGetActivationFactory"));
if (!pDllGetActivationFactory)
return hr;
ComPtr<IActivationFactory> _factory;
hr = pDllGetActivationFactory(classId, _factory.GetAddressOf());
if (FAILED(hr)) return hr;
return _factory.CopyTo(iid, factory);
}
return hr;
}
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID reserved)
{
if (DetourIsHelperProcess()) return TRUE;
if (dwReason == DLL_PROCESS_ATTACH)
{
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&reinterpret_cast<PVOID&>(TrueRoGetActivationFactory), RoGetActivationFactory_Hook);
DetourTransactionCommit();
}
else if (dwReason == DLL_PROCESS_DETACH)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&reinterpret_cast<PVOID&>(TrueRoGetActivationFactory), RoGetActivationFactory_Hook);
DetourTransactionCommit();
}
return TRUE;
}