Rainmeter.exe now uses delay-loading for Rainmeter.dll instead of manually loading it.
This commit is contained in:
Birunthan Mohanathas 2013-05-05 13:09:48 +03:00
parent bb823b798e
commit 67542f31dd
3 changed files with 39 additions and 46 deletions

View File

@ -21,28 +21,11 @@
#include <crtdbg.h>
#include <Windows.h>
#include <ShellAPI.h>
#include <delayimp.h>
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
typedef int (*RainmeterMainFunc)(WCHAR* args);
HINSTANCE LoadRainmeterLibrary()
{
const int bufferSize = MAX_PATH;
WCHAR buffer[bufferSize];
int bufferLen = GetModuleFileName(NULL, buffer, bufferSize);
if (bufferLen == 0 || bufferLen == bufferSize)
{
return NULL;
}
// Change extension from .exe to .dll.
buffer[bufferLen - 3] = L'd';
buffer[bufferLen - 2] = L'l';
buffer[bufferLen - 1] = L'l';
return LoadLibrary(buffer);
}
EXTERN_C __declspec(dllimport) int RainmeterMain(LPWSTR cmdLine);
WCHAR* GetCommandLineArguments()
{
@ -75,6 +58,34 @@ WCHAR* GetCommandLineArguments()
return args;
}
/*
** Hook to exit the process gracefully if delay-loading dependencies (i.e. Rainmeter.dll) fails.
**
*/
FARPROC WINAPI DelayLoadFailureHook(unsigned int dliNotify, DelayLoadInfo* dli)
{
if (dliNotify == dliFailLoadLib)
{
WCHAR buffer[128];
int arch = 32;
#ifdef _WIN64
arch = 64;
#endif
const WCHAR* format = L"%S (%i-bit) error %ld.\n\nDo you want to view help online?";
wsprintf(buffer, format, dli->szDll, arch, dli->dwLastError);
if (MessageBox(NULL, buffer, L"Rainmeter", MB_YESNO | MB_ICONERROR) == IDYES)
{
ShellExecute(NULL, L"open", L"http://rainmeter.net/dllerror", NULL, NULL, SW_SHOWNORMAL);
}
ExitProcess(0);
}
return nullptr;
}
EXTERN_C PfnDliHook __pfnDliFailureHook2 = DelayLoadFailureHook;
/*
** Entry point. In Release builds, the entry point is Main() since the CRT is not used.
**
@ -84,35 +95,17 @@ int APIENTRY wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
//_CrtSetBreakAlloc(000);
// Prevent system error message boxes.
UINT oldMode = SetErrorMode(0);
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS);
HINSTANCE instance = (HINSTANCE)&__ImageBase;
WCHAR* args = GetCommandLineArguments();
HRSRC iconResource = FindResource(instance, MAKEINTRESOURCE(1), RT_ICON);
if (iconResource)
{
// Initialize Rainmeter.
HINSTANCE libInstance = LoadRainmeterLibrary();
if (libInstance)
{
auto rainmeterMain = (RainmeterMainFunc)GetProcAddress(libInstance, MAKEINTRESOURCEA(1));
int result = rainmeterMain(args);
FreeLibrary(libInstance);
return result;
}
else
{
WCHAR buffer[128];
int arch = 32;
#ifdef _WIN64
arch = 64;
#endif
const WCHAR* error = L"Rainmeter.dll (%i-bit) error %i.\n\nDo you want to view help online?";
wsprintf(buffer, error, arch, GetLastError());
if (MessageBox(NULL, buffer, L"Rainmeter", MB_YESNO | MB_ICONERROR) == IDYES)
{
ShellExecute(NULL, L"open", L"http://rainmeter.net/dllerror", NULL, NULL, SW_SHOWNORMAL);
}
}
return RainmeterMain(args);
}
else
{

View File

@ -22,6 +22,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<EntryPointSymbol>wWinMainCRTStartup</EntryPointSymbol>
<DelayLoadDLLs>Rainmeter.dll</DelayLoadDLLs>
</Link>
<PostBuildEvent>
<Command>if exist ..\testbench\x32\debug\Skins goto skip
@ -43,6 +44,7 @@ xcopy /Q /S /Y ..\Build\Themes ..\testbench\x32\debug\Layouts
<Link>
<SubSystem>Windows</SubSystem>
<EntryPointSymbol>wWinMainCRTStartup</EntryPointSymbol>
<DelayLoadDLLs>Rainmeter.dll</DelayLoadDLLs>
</Link>
<PostBuildEvent>
<Command>if exist ..\testbench\x64\debug\Skins goto skip
@ -68,6 +70,7 @@ xcopy /Q /S /Y ..\Build\Themes ..\testbench\x64\debug\Layouts
<SubSystem>Windows</SubSystem>
<EntryPointSymbol>Main</EntryPointSymbol>
<IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
<DelayLoadDLLs>Rainmeter.dll</DelayLoadDLLs>
</Link>
<PostBuildEvent>
<Command>if exist ..\testbench\x32\release\Skins goto skip
@ -94,6 +97,7 @@ xcopy /Q /S /Y ..\Build\Themes ..\testbench\x32\release\Layouts
<SubSystem>Windows</SubSystem>
<EntryPointSymbol>Main</EntryPointSymbol>
<IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
<DelayLoadDLLs>Rainmeter.dll</DelayLoadDLLs>
</Link>
<PostBuildEvent>
<Command>if exist ..\testbench\x64\release\Skins goto skip

View File

@ -708,10 +708,6 @@ CRainmeter::CRainmeter() :
m_GDIplusToken(),
m_GlobalOptions()
{
// Prevent the system error message boxes.
UINT oldMode = SetErrorMode(0);
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS);
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
InitCommonControls();