2009-02-10 18:37:48 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2001 Kimmo Pekkola
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2012-01-23 06:36:15 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-02-10 18:37:48 +00:00
|
|
|
*/
|
|
|
|
|
2009-10-07 16:45:14 +00:00
|
|
|
#define _CRTDBG_MAP_ALLOC
|
2012-02-14 17:00:07 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2011-09-29 06:04:20 +00:00
|
|
|
#include <crtdbg.h>
|
2012-02-14 17:00:07 +00:00
|
|
|
#include <Windows.h>
|
2012-06-11 13:15:03 +00:00
|
|
|
#include <ShellAPI.h>
|
2013-10-19 13:49:48 +00:00
|
|
|
#include <Shlwapi.h>
|
2012-12-23 12:53:37 +00:00
|
|
|
|
|
|
|
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
|
|
|
|
|
2013-10-19 13:49:48 +00:00
|
|
|
typedef int (*RainmeterMainFunc)(LPWSTR cmdLine);
|
2012-12-23 12:53:37 +00:00
|
|
|
|
|
|
|
WCHAR* GetCommandLineArguments()
|
|
|
|
{
|
2012-12-23 15:00:14 +00:00
|
|
|
WCHAR* args = GetCommandLine();
|
2012-12-23 12:53:37 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2013-05-05 10:09:48 +00:00
|
|
|
/*
|
2013-10-19 13:49:48 +00:00
|
|
|
** Attempts to load Rainmeter.dll. If it fails, retries after loading our own copies of the CRT
|
|
|
|
** DLLs in the Runtime directory.
|
2013-05-05 10:09:48 +00:00
|
|
|
*/
|
2013-10-19 13:49:48 +00:00
|
|
|
HINSTANCE LoadRainmeterLibrary()
|
2013-05-05 10:09:48 +00:00
|
|
|
{
|
2013-10-19 13:49:48 +00:00
|
|
|
HINSTANCE rmDll = LoadLibrary(L"Rainmeter.dll");
|
|
|
|
if (!rmDll)
|
2013-05-05 10:09:48 +00:00
|
|
|
{
|
2013-10-19 13:49:48 +00:00
|
|
|
WCHAR path[MAX_PATH];
|
|
|
|
if (GetModuleFileName(nullptr, path, MAX_PATH) > 0)
|
2013-05-05 10:09:48 +00:00
|
|
|
{
|
2013-10-19 13:49:48 +00:00
|
|
|
PathRemoveFileSpec(path);
|
|
|
|
PathAppend(path, L"Runtime");
|
|
|
|
SetDllDirectory(path);
|
|
|
|
PathAppend(path, L"msvcp110.dll");
|
|
|
|
|
|
|
|
// Loading msvcpNNN.dll will load msvcrNNN.dll as well.
|
|
|
|
HINSTANCE msvcrDll = LoadLibrary(path);
|
|
|
|
SetDllDirectory(L"");
|
2013-05-05 10:09:48 +00:00
|
|
|
|
2013-10-19 13:49:48 +00:00
|
|
|
if (msvcrDll)
|
|
|
|
{
|
|
|
|
rmDll = LoadLibrary(L"Rainmeter.dll");
|
|
|
|
FreeLibrary(msvcrDll);
|
|
|
|
}
|
|
|
|
}
|
2013-05-05 10:09:48 +00:00
|
|
|
}
|
|
|
|
|
2013-10-19 13:49:48 +00:00
|
|
|
return rmDll;
|
2013-05-05 10:09:48 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:04:20 +00:00
|
|
|
/*
|
2012-12-23 12:53:37 +00:00
|
|
|
** Entry point. In Release builds, the entry point is Main() since the CRT is not used.
|
2009-02-10 18:37:48 +00:00
|
|
|
**
|
|
|
|
*/
|
2012-12-23 12:53:37 +00:00
|
|
|
int APIENTRY wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-02-14 17:58:03 +00:00
|
|
|
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
|
|
|
|
//_CrtSetBreakAlloc(000);
|
|
|
|
|
2013-05-05 10:09:48 +00:00
|
|
|
// Prevent system error message boxes.
|
|
|
|
UINT oldMode = SetErrorMode(0);
|
|
|
|
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS);
|
|
|
|
|
2012-12-23 12:53:37 +00:00
|
|
|
HINSTANCE instance = (HINSTANCE)&__ImageBase;
|
|
|
|
WCHAR* args = GetCommandLineArguments();
|
|
|
|
|
|
|
|
HRSRC iconResource = FindResource(instance, MAKEINTRESOURCE(1), RT_ICON);
|
2012-06-11 13:15:03 +00:00
|
|
|
if (iconResource)
|
|
|
|
{
|
2013-10-19 13:49:48 +00:00
|
|
|
HINSTANCE rmDll = LoadRainmeterLibrary();
|
|
|
|
if (rmDll)
|
|
|
|
{
|
|
|
|
auto rainmeterMain = (RainmeterMainFunc)GetProcAddress(rmDll, MAKEINTRESOURCEA(1));
|
|
|
|
if (rainmeterMain)
|
|
|
|
{
|
|
|
|
return rainmeterMain(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WCHAR message[128];
|
|
|
|
wsprintf(
|
|
|
|
message,
|
|
|
|
L"Rainmeter.dll load error %ld.",
|
|
|
|
GetLastError());
|
|
|
|
MessageBox(nullptr, message, L"Rainmeter", MB_OK | MB_ICONERROR);
|
2012-06-11 13:15:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Stub prodecure. If icon resources have been removed, try to launch the actual Rainmeter.exe.
|
|
|
|
HKEY hKey;
|
|
|
|
const REGSAM desiredSam = KEY_QUERY_VALUE | KEY_WOW64_32KEY;
|
|
|
|
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Rainmeter", 0, desiredSam, &hKey) == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
const DWORD size = MAX_PATH;
|
|
|
|
WCHAR buffer[size];
|
|
|
|
DWORD type = 0;
|
2013-05-31 14:28:39 +00:00
|
|
|
if (RegQueryValueEx(hKey, nullptr , nullptr, &type, (LPBYTE)buffer, (LPDWORD)&size) == ERROR_SUCCESS &&
|
2012-06-11 13:15:03 +00:00
|
|
|
type == REG_SZ)
|
|
|
|
{
|
|
|
|
SetCurrentDirectory(buffer);
|
2012-12-23 12:53:37 +00:00
|
|
|
lstrcat(buffer, L"\\Rainmeter.exe");
|
2013-05-31 14:28:39 +00:00
|
|
|
ShellExecute(nullptr, L"open", buffer, args, nullptr, SW_SHOWNORMAL);
|
2012-06-11 13:15:03 +00:00
|
|
|
}
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-23 12:53:37 +00:00
|
|
|
|
|
|
|
return 1;
|
2012-12-23 15:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _DEBUG
|
|
|
|
EXTERN_C int WINAPI Main()
|
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
int result = wWinMain(nullptr, nullptr, nullptr, 0);
|
2012-12-23 15:42:30 +00:00
|
|
|
ExitProcess(result);
|
2012-12-23 15:00:14 +00:00
|
|
|
return 0; // Never reached.
|
|
|
|
}
|
|
|
|
#endif
|