Rainmeter.exe: Removed dependency on MSVCRT110.dll for Release builds

If the CRT libraries are missing or corrupt, a message pointing to http://rainmeter.net/dllerror is displayed.
This commit is contained in:
Birunthan Mohanathas 2012-12-23 14:53:37 +02:00
parent d2e063e795
commit 24ef81dee8
2 changed files with 99 additions and 16 deletions

View File

@ -21,23 +21,102 @@
#include <crtdbg.h> #include <crtdbg.h>
#include <Windows.h> #include <Windows.h>
#include <ShellAPI.h> #include <ShellAPI.h>
#include "../Library/Rainmeter.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);
}
WCHAR* GetCommandLineArguments()
{
LPWSTR args = GetCommandLine();
// Skip past (quoted) application path in cmdLine.
if (*args == L'"')
{
++args; // Skip leading quote.
while (*args && *args != L'"')
{
++args;
}
++args; // Skip trailing quote.
}
else
{
while (*args && *args != L' ')
{
++args;
}
}
// Skip leading whitespace (similar to CRT implementation).
while (*args && *args <= L' ')
{
++args;
}
return args;
}
/* /*
** Entry point. ** Entry point. In Release builds, the entry point is Main() since the CRT is not used.
** **
*/ */
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) #ifdef _DEBUG
int APIENTRY wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
#else
EXTERN_C int WINAPI Main()
#endif
{ {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
//_CrtSetBreakAlloc(000); //_CrtSetBreakAlloc(000);
HRSRC iconResource = FindResource(hInstance, MAKEINTRESOURCE(1), RT_ICON); HINSTANCE instance = (HINSTANCE)&__ImageBase;
WCHAR* args = GetCommandLineArguments();
HRSRC iconResource = FindResource(instance, MAKEINTRESOURCE(1), RT_ICON);
if (iconResource) if (iconResource)
{ {
// Call RainmeterMain from Rainmeter.dll. Since Rainmeter.dll is delay-loaded, this will cause // Initialize Rainmeter.
// crash if Rainmeter.dll is not found. HINSTANCE libInstance = LoadRainmeterLibrary();
return RainmeterMain(lpCmdLine); 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);
}
}
} }
else else
{ {
@ -53,12 +132,14 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
type == REG_SZ) type == REG_SZ)
{ {
SetCurrentDirectory(buffer); SetCurrentDirectory(buffer);
wcscat(buffer, L"\\Rainmeter.exe"); lstrcat(buffer, L"\\Rainmeter.exe");
ShellExecute(NULL, L"open", buffer, lpCmdLine, NULL, SW_SHOWNORMAL); ShellExecute(NULL, L"open", buffer, args, NULL, SW_SHOWNORMAL);
} }
RegCloseKey(hKey); RegCloseKey(hKey);
} }
return 0; return 0;
} }
return 1;
} }

View File

@ -21,7 +21,6 @@
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<EntryPointSymbol>wWinMainCRTStartup</EntryPointSymbol> <EntryPointSymbol>wWinMainCRTStartup</EntryPointSymbol>
<DelayLoadDLLs>Rainmeter.dll</DelayLoadDLLs>
</Link> </Link>
<PostBuildEvent> <PostBuildEvent>
<Command>if exist ..\testbench\x32\debug\Skins goto skip <Command>if exist ..\testbench\x32\debug\Skins goto skip
@ -43,7 +42,6 @@ xcopy /Q /S /Y ..\Build\Themes ..\testbench\x32\debug\Layouts
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<EntryPointSymbol>wWinMainCRTStartup</EntryPointSymbol> <EntryPointSymbol>wWinMainCRTStartup</EntryPointSymbol>
<DelayLoadDLLs>Rainmeter.dll</DelayLoadDLLs>
</Link> </Link>
<PostBuildEvent> <PostBuildEvent>
<Command>if exist ..\testbench\x64\debug\Skins goto skip <Command>if exist ..\testbench\x64\debug\Skins goto skip
@ -61,12 +59,14 @@ xcopy /Q /S /Y ..\Build\Themes ..\testbench\x64\debug\Layouts
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile> <ClCompile>
<DisableSpecificWarnings>4018;4090;4114;4267;4351;4786;4800;4996</DisableSpecificWarnings> <DisableSpecificWarnings>4018;4090;4114;4267;4351;4786;4800;4996</DisableSpecificWarnings>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<BufferSecurityCheck>false</BufferSecurityCheck>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalLibraryDirectories>..\Library\x32\Release;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>..\Library\x32\Release;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<EntryPointSymbol>wWinMainCRTStartup</EntryPointSymbol> <EntryPointSymbol>Main</EntryPointSymbol>
<DelayLoadDLLs>Rainmeter.dll</DelayLoadDLLs> <IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
</Link> </Link>
<PostBuildEvent> <PostBuildEvent>
<Command>if exist ..\testbench\x32\release\Skins goto skip <Command>if exist ..\testbench\x32\release\Skins goto skip
@ -85,12 +85,14 @@ xcopy /Q /S /Y ..\Build\Themes ..\testbench\x32\release\Layouts
<ClCompile> <ClCompile>
<AdditionalOptions>/GA %(AdditionalOptions)</AdditionalOptions> <AdditionalOptions>/GA %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>4018;4090;4114;4267;4351;4786;4800;4996</DisableSpecificWarnings> <DisableSpecificWarnings>4018;4090;4114;4267;4351;4786;4800;4996</DisableSpecificWarnings>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<BufferSecurityCheck>false</BufferSecurityCheck>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalLibraryDirectories>..\Library\x64\Release;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>..\Library\x64\Release;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<EntryPointSymbol>wWinMainCRTStartup</EntryPointSymbol> <EntryPointSymbol>Main</EntryPointSymbol>
<DelayLoadDLLs>Rainmeter.dll</DelayLoadDLLs> <IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
</Link> </Link>
<PostBuildEvent> <PostBuildEvent>
<Command>if exist ..\testbench\x64\release\Skins goto skip <Command>if exist ..\testbench\x64\release\Skins goto skip