Minor changes to 7cee196

This commit is contained in:
Birunthan Mohanathas 2012-05-24 12:19:32 +03:00
parent fdf73197d4
commit f1e127b718
2 changed files with 38 additions and 49 deletions

View File

@ -63,41 +63,48 @@ int RainmeterMain(LPWSTR cmdLine)
SendMessage(wnd, WM_COPYDATA, NULL, (LPARAM)&cds); SendMessage(wnd, WM_COPYDATA, NULL, (LPARAM)&cds);
return 0; return 0;
} }
else if (_wcsicmp(L"!RainmeterQuit", cmdLine) != 0 &&
_wcsicmp(L"!Quit", cmdLine) != 0)
{
return 1; return 1;
} }
else if (cmdLine[0] == L'"')
{
// Strip quotes
++cmdLine;
WCHAR* pos = wcsrchr(cmdLine, L'"');
if (pos)
{
*pos = L'\0';
}
} }
// Avoid loading a dll from current directory // Avoid loading a dll from current directory
SetDllDirectory(L""); SetDllDirectory(L"");
int ret = 1; int ret = 1;
HANDLE mutex;
if (CRainmeter::CreateInstanceMutex(&mutex, cmdLine))
{
Rainmeter = new CRainmeter; Rainmeter = new CRainmeter;
if (Rainmeter) if (Rainmeter)
{
if (!Rainmeter->IsAlreadyRunning())
{
try
{ {
ret = Rainmeter->Initialize(cmdLine); ret = Rainmeter->Initialize(cmdLine);
}
catch (CError& error)
{
MessageBox(NULL, error.GetString().c_str(), APPNAME, MB_OK | MB_TOPMOST | MB_ICONERROR);
}
if (ret == 0) if (ret == 0)
{ {
ret = Rainmeter->MessagePump(); ret = Rainmeter->MessagePump();
} }
}
delete Rainmeter; delete Rainmeter;
Rainmeter = NULL; Rainmeter = NULL;
} }
ReleaseMutex(mutex);
}
else
{
// Rainmeter already started
}
return ret; return ret;
} }
@ -665,7 +672,6 @@ CRainmeter::CRainmeter() :
m_Logging(false), m_Logging(false),
m_CurrentParser(), m_CurrentParser(),
m_Window(), m_Window(),
m_Mutex(),
m_Instance(), m_Instance(),
m_ResourceInstance(), m_ResourceInstance(),
m_ResourceLCID(), m_ResourceLCID(),
@ -711,7 +717,6 @@ CRainmeter::~CRainmeter()
FinalizeLitestep(); FinalizeLitestep();
if (m_Mutex) ReleaseMutex(m_Mutex);
if (m_ResourceInstance) FreeLibrary(m_ResourceInstance); if (m_ResourceInstance) FreeLibrary(m_ResourceInstance);
CoUninitialize(); CoUninitialize();
@ -723,10 +728,8 @@ CRainmeter::~CRainmeter()
** The main initialization function for the module. ** The main initialization function for the module.
** **
*/ */
int CRainmeter::Initialize(LPCWSTR szPath) int CRainmeter::Initialize(LPCWSTR iniPath)
{ {
int result = 0;
m_Instance = GetModuleHandle(L"Rainmeter"); m_Instance = GetModuleHandle(L"Rainmeter");
WNDCLASS wc = {0}; WNDCLASS wc = {0};
@ -756,29 +759,16 @@ int CRainmeter::Initialize(LPCWSTR szPath)
// Remove the module's name from the path // Remove the module's name from the path
WCHAR* pos = wcsrchr(buffer, L'\\'); WCHAR* pos = wcsrchr(buffer, L'\\');
m_Path.assign(buffer, pos ? pos - buffer + 1 : 0); m_Path.assign(buffer, pos ? pos - buffer + 1 : 0);
InitalizeLitestep(); InitalizeLitestep();
bool bDefaultIniLocation = false; bool bDefaultIniLocation = false;
if (*szPath) if (*iniPath)
{ {
// The command line defines the location of Rainmeter.ini (or whatever it calls it). // The command line defines the location of Rainmeter.ini (or whatever it calls it).
std::wstring iniFile = szPath; std::wstring iniFile = iniPath;
if (iniFile[0] == L'"')
{
if (iniFile.length() == 1)
{
iniFile.clear();
}
else if (iniFile[iniFile.length() - 1] == L'"')
{
iniFile.assign(iniFile, 1, iniFile.length() - 2);
}
}
ExpandEnvironmentVariables(iniFile); ExpandEnvironmentVariables(iniFile);
if (iniFile.empty() || CSystem::IsPathSeparator(iniFile[iniFile.length() - 1])) if (iniFile.empty() || CSystem::IsPathSeparator(iniFile[iniFile.length() - 1]))
@ -889,7 +879,8 @@ int CRainmeter::Initialize(LPCWSTR szPath)
m_ResourceLCID = 1033; m_ResourceLCID = 1033;
if (!m_ResourceInstance) if (!m_ResourceInstance)
{ {
throw CError(L"Unable to load language library"); MessageBox(NULL, L"Unable to load language library", APPNAME, MB_OK | MB_TOPMOST | MB_ICONERROR);
return 1;
} }
} }
@ -1035,10 +1026,10 @@ int CRainmeter::Initialize(LPCWSTR szPath)
CheckUpdate(); CheckUpdate();
} }
return result; // All is OK return 0; // All is OK
} }
bool CRainmeter::IsAlreadyRunning() bool CRainmeter::CreateInstanceMutex(HANDLE* mutex, LPCWSTR iniPath)
{ {
typedef struct typedef struct
{ {
@ -1081,10 +1072,9 @@ bool CRainmeter::IsAlreadyRunning()
} }
*pos = L'\0'; *pos = L'\0';
m_Mutex = CreateMutex(NULL, FALSE, mutexName); *mutex = CreateMutex(NULL, FALSE, mutexName);
if (GetLastError() == ERROR_ALREADY_EXISTS) if (GetLastError() != ERROR_ALREADY_EXISTS)
{ {
m_Mutex = NULL;
return true; return true;
} }
} }

View File

@ -121,8 +121,8 @@ public:
CRainmeter(); CRainmeter();
~CRainmeter(); ~CRainmeter();
int Initialize(LPCWSTR szPath); int Initialize(LPCWSTR iniPath);
bool IsAlreadyRunning(); static bool CreateInstanceMutex(HANDLE* mutex, LPCWSTR iniPath);
int MessagePump(); int MessagePump();
void SetNetworkStatisticsTimer(); void SetNetworkStatisticsTimer();
@ -323,7 +323,6 @@ private:
HWND m_Window; HWND m_Window;
HANDLE m_Mutex;
HINSTANCE m_Instance; HINSTANCE m_Instance;
HMODULE m_ResourceInstance; HMODULE m_ResourceInstance;
LCID m_ResourceLCID; LCID m_ResourceLCID;