PDA

View Full Version : How to limit only one application instance in the same machine



yxmaomao
6th May 2008, 07:36
I want to control there is one only application run in my machine
I know with Qt3 , we can implement this with QtSingleApplication class
But As for Qt4.3.2, how to do this.

Any guys give me some advice is appreciated!

Thanks

aamer4yu
6th May 2008, 07:46
Isnt that class available in Qt 4 series ??

I guess its available only in Commercial edition of Qt

yxmaomao
6th May 2008, 07:58
Yes , I am sure this class is not exist in Qt 4.3.2
my version is commercail version

patrik08
6th May 2008, 08:02
I found more as on way..

one is subclass QApplication



/* target find if bool Apps_Open become true */
windowTitle = QString ( "Layer edit sample" );
Apps_Open = false; /* default not open */
const QString single_key = QString("ID 29385129512818123 by %1").arg(windowTitle);

#if defined Q_WS_WIN
m_mutex = (void *) CreateMutexW( 0, false, (LPCWSTR) single_key.utf16() );
m_prevInstance = ( GetLastError() == ERROR_ALREADY_EXISTS );
if (m_prevInstance) {
Apps_Open = true;
}
#endif

#ifdef Q_OS_UNIX
unix_sock = make_local_app_socket();
Apps_Open = unix_sock == 0 ? true : false;
qDebug() << "### sock connect res " << Apps_Open << unix_sock;
#endif

/* on destroy ~QApplication remove socked file */



unix socked connect function ...




#ifdef Q_OS_UNIX
#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>

/* Unix SOCKET to comunicate on parent apps friend and send file or other msg! */
#define _UNIX_SOCKET_ "/tmp/layeres.sock"


int make_local_app_socket()
{
struct sockaddr_un name;
int sock;
size_t size;
sock = socket (PF_LOCAL, SOCK_DGRAM, 0);
if (sock < 0)
{
return 0;
}
name.sun_family = AF_LOCAL;
strncpy (name.sun_path, _UNIX_SOCKET_, sizeof (name.sun_path));
name.sun_path[sizeof (name.sun_path) - 1] = '\0';
size = (offsetof (struct sockaddr_un, sun_path) + strlen (name.sun_path) + 1);

if (bind (sock, (struct sockaddr *) &name, size) < 0)
{
return 0;
}
return sock;
}
#endif



Focus to preview apps if is Apps_Open having true




/* focus preview process ! */
bool FocusInstance() const
{
#if defined Q_WS_WIN
HWND hWnd = FindWindowW( 0, (LPCWSTR) windowTitle.utf16() );
if( hWnd )
{
long id = GetWindowThreadProcessId( hWnd, 0 );
AttachThreadInput( id, 0, true );
if ( IsIconic( hWnd ) )
ShowWindow( hWnd, SW_RESTORE );
SetForegroundWindow( hWnd );
AttachThreadInput( id, 0, false );
return true;
}
#endif
#ifdef Q_OS_UNIX
QFileInfo sockets(QString(_UNIX_SOCKET_));
qDebug() << "### having " << sockets.exists();
if (unix_sock == 0) {
qDebug() << "### send file to socket parent here... ";
return true;
}
#endif
return false;
}




All in one ...

http://fop-miniscribus.googlecode.com/svn/trunk/GraphicsViewEdit/main.cpp


On mac i not having a solution
IMO one commercial QT version a QtSingleApplication is inside ... but i not know if you can send file or message to the open apps by unix socked is possibel

http://www.google.com/codesearch having a copy of sample commercial "QtSingleApplication"

yxmaomao
6th May 2008, 08:56
Cool !
Thanks patrik08

yxmaomao
6th May 2008, 08:59
hi patrik08
Are you sure CreateMutexW( ) is Qt's function
for my Qt4.3.2 , I can't find it

spud
6th May 2008, 09:30
It's a Windows function. You have to include <windows.h>.

fullmetalcoder
6th May 2008, 15:23
QtSingleApplication is a solution that commercial customers can obtain from Trolltech (may need an additional fee though). If you are looking for something free and open source, the small class I wrote for use with Edyuk may be of some help to you. It uses local sockets under Mac/Linux and (thanks to a recent contribution) named pipes under windows. A couple of projects (apart from Edyuk ;) ) are already using it as it is lightweight, performant and extremely easy to embed. You can get it by dowloading a recent package of Edyuk (or by doing a SVN checkout).

GreyGeek
9th May 2008, 16:54
fullmetalcoder,
Your avatar/icon is NEAT! :)

I'm assuming you are a Linux enthusiast! :D