Momergil
12th April 2014, 03:57
Hello!
I want my application to know, in the main(...) method preferentially even before starting the QApplication, if an instance of the same application is already running.
Till now I have being doing this by using the following method:
/*!
* \brief getOthermShare tells if there is another mShare opened in this machine.
* \return
*/
bool getOthermShare()
{
bool mshare_detectado = false;
// Get the list of process identifiers
DWORD aProcesses[1024], cbNeeded, cProcesses;
//Tenta 10 vezes capturar os processos; se nao conseguir, fecha (rever isso aqui!)
for (int aaa = 0; aaa < 10; aaa++)
{
if (EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
break;
else if (aaa == 9)
{
QMessageBox::warning(NULL,QMessageBox::tr("Error"),QMessageBox::tr("A fatal error ocurred. Please close down your application and try again.\nIf the problem persists, enter in contact with "
"the support team."));
return false;
}
}
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Discover the name and process identifier for each process.
for (unsigned int i = 0; i < cProcesses; i++ )
{
if( aProcesses[i] != 0 )
{
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
// Get a handle to the process.
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_ALL_ACCESS | READ_CONTROL |
PROCESS_VM_READ,
FALSE, aProcesses[i] );
// Get the process name.
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
}
#ifdef Q_OS_WIN
#ifdef UNICODE
if (QString::fromUtf16((ushort*)szProcessName).sectio n(".",0,0) == "mShare")
{
if (!mshare_detectado)
mshare_detectado = true;
else
{
CloseHandle( hProcess );
return true;
}
}
#endif
#endif
// Release the handle to the process.
CloseHandle( hProcess );
}
}
}
return false;
}
which obviously have two problems: first, it works only for Windows, and second, its kinda akward :P
So I'ld like to know if there is a "Qt way" of doing the same thing with the advantage of being platform independent. I noticed the solution shown at [http://www.qtcentre.org/wiki/index.php?title=SingleApplication], but I must say I didn't like that way :P Is there another solution? (and one that don't use anything extern to the application, such as a file that is written with "1" when the software is opened or the like)
Thanks,
Momergil
I want my application to know, in the main(...) method preferentially even before starting the QApplication, if an instance of the same application is already running.
Till now I have being doing this by using the following method:
/*!
* \brief getOthermShare tells if there is another mShare opened in this machine.
* \return
*/
bool getOthermShare()
{
bool mshare_detectado = false;
// Get the list of process identifiers
DWORD aProcesses[1024], cbNeeded, cProcesses;
//Tenta 10 vezes capturar os processos; se nao conseguir, fecha (rever isso aqui!)
for (int aaa = 0; aaa < 10; aaa++)
{
if (EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
break;
else if (aaa == 9)
{
QMessageBox::warning(NULL,QMessageBox::tr("Error"),QMessageBox::tr("A fatal error ocurred. Please close down your application and try again.\nIf the problem persists, enter in contact with "
"the support team."));
return false;
}
}
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Discover the name and process identifier for each process.
for (unsigned int i = 0; i < cProcesses; i++ )
{
if( aProcesses[i] != 0 )
{
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
// Get a handle to the process.
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_ALL_ACCESS | READ_CONTROL |
PROCESS_VM_READ,
FALSE, aProcesses[i] );
// Get the process name.
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
}
#ifdef Q_OS_WIN
#ifdef UNICODE
if (QString::fromUtf16((ushort*)szProcessName).sectio n(".",0,0) == "mShare")
{
if (!mshare_detectado)
mshare_detectado = true;
else
{
CloseHandle( hProcess );
return true;
}
}
#endif
#endif
// Release the handle to the process.
CloseHandle( hProcess );
}
}
}
return false;
}
which obviously have two problems: first, it works only for Windows, and second, its kinda akward :P
So I'ld like to know if there is a "Qt way" of doing the same thing with the advantage of being platform independent. I noticed the solution shown at [http://www.qtcentre.org/wiki/index.php?title=SingleApplication], but I must say I didn't like that way :P Is there another solution? (and one that don't use anything extern to the application, such as a file that is written with "1" when the software is opened or the like)
Thanks,
Momergil