mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
Additional change to f1e127b
Instance now checked based on used .ini file (rather than .exe path and arguments)
This commit is contained in:
parent
f1e127b718
commit
3ac5aea85a
@ -82,27 +82,17 @@ int RainmeterMain(LPWSTR cmdLine)
|
|||||||
|
|
||||||
int ret = 1;
|
int ret = 1;
|
||||||
|
|
||||||
HANDLE mutex;
|
Rainmeter = new CRainmeter;
|
||||||
if (CRainmeter::CreateInstanceMutex(&mutex, cmdLine))
|
if (Rainmeter)
|
||||||
{
|
{
|
||||||
Rainmeter = new CRainmeter;
|
ret = Rainmeter->Initialize(cmdLine);
|
||||||
if (Rainmeter)
|
if (ret == 0)
|
||||||
{
|
{
|
||||||
ret = Rainmeter->Initialize(cmdLine);
|
ret = Rainmeter->MessagePump();
|
||||||
if (ret == 0)
|
|
||||||
{
|
|
||||||
ret = Rainmeter->MessagePump();
|
|
||||||
}
|
|
||||||
|
|
||||||
delete Rainmeter;
|
|
||||||
Rainmeter = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ReleaseMutex(mutex);
|
delete Rainmeter;
|
||||||
}
|
Rainmeter = NULL;
|
||||||
else
|
|
||||||
{
|
|
||||||
// Rainmeter already started
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@ -672,6 +662,7 @@ 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(),
|
||||||
@ -718,6 +709,7 @@ CRainmeter::~CRainmeter()
|
|||||||
FinalizeLitestep();
|
FinalizeLitestep();
|
||||||
|
|
||||||
if (m_ResourceInstance) FreeLibrary(m_ResourceInstance);
|
if (m_ResourceInstance) FreeLibrary(m_ResourceInstance);
|
||||||
|
if (m_Mutex) ReleaseMutex(m_Mutex);
|
||||||
|
|
||||||
CoUninitialize();
|
CoUninitialize();
|
||||||
|
|
||||||
@ -730,6 +722,60 @@ CRainmeter::~CRainmeter()
|
|||||||
*/
|
*/
|
||||||
int CRainmeter::Initialize(LPCWSTR iniPath)
|
int CRainmeter::Initialize(LPCWSTR iniPath)
|
||||||
{
|
{
|
||||||
|
InitalizeLitestep();
|
||||||
|
|
||||||
|
WCHAR* buffer = new WCHAR[MAX_LINE_LENGTH];
|
||||||
|
GetModuleFileName(m_Instance, buffer, MAX_LINE_LENGTH);
|
||||||
|
|
||||||
|
// Remove the module's name from the path
|
||||||
|
WCHAR* pos = wcsrchr(buffer, L'\\');
|
||||||
|
m_Path.assign(buffer, pos ? pos - buffer + 1 : 0);
|
||||||
|
|
||||||
|
bool bDefaultIniLocation = false;
|
||||||
|
if (*iniPath)
|
||||||
|
{
|
||||||
|
// The command line defines the location of Rainmeter.ini (or whatever it calls it).
|
||||||
|
std::wstring iniFile = iniPath;
|
||||||
|
ExpandEnvironmentVariables(iniFile);
|
||||||
|
|
||||||
|
if (iniFile.empty() || CSystem::IsPathSeparator(iniFile[iniFile.length() - 1]))
|
||||||
|
{
|
||||||
|
iniFile += L"Rainmeter.ini";
|
||||||
|
}
|
||||||
|
else if (iniFile.length() <= 4 || _wcsicmp(iniFile.c_str() + (iniFile.length() - 4), L".ini") != 0)
|
||||||
|
{
|
||||||
|
iniFile += L"\\Rainmeter.ini";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!CSystem::IsPathSeparator(iniFile[0]) && iniFile.find_first_of(L':') == std::wstring::npos)
|
||||||
|
{
|
||||||
|
// Make absolute path
|
||||||
|
iniFile.insert(0, m_Path);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_IniFile = iniFile;
|
||||||
|
bDefaultIniLocation = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_IniFile = m_Path;
|
||||||
|
m_IniFile += L"Rainmeter.ini";
|
||||||
|
|
||||||
|
// If the ini file doesn't exist in the program folder store it to the %APPDATA% instead so that things work better in Vista/Win7
|
||||||
|
if (_waccess(m_IniFile.c_str(), 0) == -1)
|
||||||
|
{
|
||||||
|
m_IniFile = L"%APPDATA%\\Rainmeter\\Rainmeter.ini";
|
||||||
|
ExpandEnvironmentVariables(m_IniFile);
|
||||||
|
bDefaultIniLocation = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsAlreadyRunning())
|
||||||
|
{
|
||||||
|
// Instance already running with same .ini file
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
m_Instance = GetModuleHandle(L"Rainmeter");
|
m_Instance = GetModuleHandle(L"Rainmeter");
|
||||||
|
|
||||||
WNDCLASS wc = {0};
|
WNDCLASS wc = {0};
|
||||||
@ -754,69 +800,14 @@ int CRainmeter::Initialize(LPCWSTR iniPath)
|
|||||||
|
|
||||||
if (!m_Window) return 1;
|
if (!m_Window) return 1;
|
||||||
|
|
||||||
WCHAR* buffer = new WCHAR[MAX_LINE_LENGTH];
|
|
||||||
GetModuleFileName(m_Instance, buffer, MAX_LINE_LENGTH);
|
|
||||||
|
|
||||||
// Remove the module's name from the path
|
|
||||||
WCHAR* pos = wcsrchr(buffer, L'\\');
|
|
||||||
m_Path.assign(buffer, pos ? pos - buffer + 1 : 0);
|
|
||||||
|
|
||||||
InitalizeLitestep();
|
|
||||||
|
|
||||||
bool bDefaultIniLocation = false;
|
|
||||||
|
|
||||||
if (*iniPath)
|
|
||||||
{
|
|
||||||
// The command line defines the location of Rainmeter.ini (or whatever it calls it).
|
|
||||||
std::wstring iniFile = iniPath;
|
|
||||||
ExpandEnvironmentVariables(iniFile);
|
|
||||||
|
|
||||||
if (iniFile.empty() || CSystem::IsPathSeparator(iniFile[iniFile.length() - 1]))
|
|
||||||
{
|
|
||||||
iniFile += L"Rainmeter.ini";
|
|
||||||
}
|
|
||||||
else if (iniFile.length() <= 4 || _wcsicmp(iniFile.c_str() + (iniFile.length() - 4), L".ini") != 0)
|
|
||||||
{
|
|
||||||
iniFile += L"\\Rainmeter.ini";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!CSystem::IsPathSeparator(iniFile[0]) && iniFile.find_first_of(L':') == std::wstring::npos)
|
|
||||||
{
|
|
||||||
// Make absolute path
|
|
||||||
iniFile.insert(0, m_Path);
|
|
||||||
}
|
|
||||||
|
|
||||||
m_IniFile = iniFile;
|
|
||||||
|
|
||||||
// If the ini file doesn't exist, create a default Rainmeter.ini file.
|
|
||||||
if (_waccess(m_IniFile.c_str(), 0) == -1)
|
|
||||||
{
|
|
||||||
CreateDefaultConfigFile();
|
|
||||||
}
|
|
||||||
bDefaultIniLocation = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_IniFile = m_Path;
|
|
||||||
m_IniFile += L"Rainmeter.ini";
|
|
||||||
|
|
||||||
// If the ini file doesn't exist in the program folder store it to the %APPDATA% instead so that things work better in Vista/Win7
|
|
||||||
if (_waccess(m_IniFile.c_str(), 0) == -1)
|
|
||||||
{
|
|
||||||
m_IniFile = L"%APPDATA%\\Rainmeter\\Rainmeter.ini";
|
|
||||||
ExpandEnvironmentVariables(m_IniFile);
|
|
||||||
bDefaultIniLocation = true;
|
|
||||||
|
|
||||||
// If the ini file doesn't exist in the %APPDATA% either, create a default Rainmeter.ini file.
|
|
||||||
if (_waccess(m_IniFile.c_str(), 0) == -1)
|
|
||||||
{
|
|
||||||
CreateDefaultConfigFile();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const WCHAR* iniFile = m_IniFile.c_str();
|
const WCHAR* iniFile = m_IniFile.c_str();
|
||||||
|
|
||||||
|
// Create a default Rainmeter.ini file if needed
|
||||||
|
if (_waccess(iniFile, 0) == -1)
|
||||||
|
{
|
||||||
|
CreateDefaultConfigFile();
|
||||||
|
}
|
||||||
|
|
||||||
// Set file locations
|
// Set file locations
|
||||||
{
|
{
|
||||||
size_t len = m_IniFile.length();
|
size_t len = m_IniFile.length();
|
||||||
@ -1029,7 +1020,7 @@ int CRainmeter::Initialize(LPCWSTR iniPath)
|
|||||||
return 0; // All is OK
|
return 0; // All is OK
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CRainmeter::CreateInstanceMutex(HANDLE* mutex, LPCWSTR iniPath)
|
bool CRainmeter::IsAlreadyRunning()
|
||||||
{
|
{
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
@ -1043,6 +1034,8 @@ bool CRainmeter::CreateInstanceMutex(HANDLE* mutex, LPCWSTR iniPath)
|
|||||||
typedef void (WINAPI * FPMD5UPDATE)(MD5_CTX* context, const unsigned char* input, unsigned int inlen);
|
typedef void (WINAPI * FPMD5UPDATE)(MD5_CTX* context, const unsigned char* input, unsigned int inlen);
|
||||||
typedef void (WINAPI * FPMD5FINAL)(MD5_CTX* context);
|
typedef void (WINAPI * FPMD5FINAL)(MD5_CTX* context);
|
||||||
|
|
||||||
|
bool alreadyRunning = false;
|
||||||
|
|
||||||
// Create MD5 digest from command line
|
// Create MD5 digest from command line
|
||||||
HMODULE cryptDll = CSystem::RmLoadLibrary(L"cryptdll.dll");
|
HMODULE cryptDll = CSystem::RmLoadLibrary(L"cryptdll.dll");
|
||||||
if (cryptDll)
|
if (cryptDll)
|
||||||
@ -1052,12 +1045,12 @@ bool CRainmeter::CreateInstanceMutex(HANDLE* mutex, LPCWSTR iniPath)
|
|||||||
FPMD5FINAL MD5Final = (FPMD5FINAL)GetProcAddress(cryptDll, "MD5Final");
|
FPMD5FINAL MD5Final = (FPMD5FINAL)GetProcAddress(cryptDll, "MD5Final");
|
||||||
if (MD5Init && MD5Update && MD5Final)
|
if (MD5Init && MD5Update && MD5Final)
|
||||||
{
|
{
|
||||||
std::wstring cmdLine = GetCommandLine();
|
std::wstring data = m_IniFile;
|
||||||
_wcsupr(&cmdLine[0]);
|
_wcsupr(&data[0]);
|
||||||
|
|
||||||
MD5_CTX ctx = {0};
|
MD5_CTX ctx = {0};
|
||||||
MD5Init(&ctx);
|
MD5Init(&ctx);
|
||||||
MD5Update(&ctx, (LPBYTE)&cmdLine[0], cmdLine.length() * sizeof(WCHAR));
|
MD5Update(&ctx, (LPBYTE)&data[0], data.length() * sizeof(WCHAR));
|
||||||
MD5Final(&ctx);
|
MD5Final(&ctx);
|
||||||
FreeLibrary(cryptDll);
|
FreeLibrary(cryptDll);
|
||||||
|
|
||||||
@ -1072,17 +1065,18 @@ bool CRainmeter::CreateInstanceMutex(HANDLE* mutex, LPCWSTR iniPath)
|
|||||||
}
|
}
|
||||||
*pos = L'\0';
|
*pos = L'\0';
|
||||||
|
|
||||||
*mutex = CreateMutex(NULL, FALSE, mutexName);
|
m_Mutex = CreateMutex(NULL, FALSE, mutexName);
|
||||||
if (GetLastError() != ERROR_ALREADY_EXISTS)
|
if (GetLastError() == ERROR_ALREADY_EXISTS)
|
||||||
{
|
{
|
||||||
return true;
|
alreadyRunning = true;
|
||||||
|
m_Mutex = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FreeLibrary(cryptDll);
|
FreeLibrary(cryptDll);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return alreadyRunning;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CRainmeter::MessagePump()
|
int CRainmeter::MessagePump()
|
||||||
|
@ -122,7 +122,7 @@ public:
|
|||||||
~CRainmeter();
|
~CRainmeter();
|
||||||
|
|
||||||
int Initialize(LPCWSTR iniPath);
|
int Initialize(LPCWSTR iniPath);
|
||||||
static bool CreateInstanceMutex(HANDLE* mutex, LPCWSTR iniPath);
|
bool IsAlreadyRunning();
|
||||||
int MessagePump();
|
int MessagePump();
|
||||||
|
|
||||||
void SetNetworkStatisticsTimer();
|
void SetNetworkStatisticsTimer();
|
||||||
@ -323,6 +323,7 @@ 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;
|
||||||
|
Loading…
Reference in New Issue
Block a user