Results 1 to 9 of 9

Thread: How to limit only one application instance in the same machine

  1. #1
    Join Date
    May 2008
    Posts
    58
    Thanks
    2

    Default How to limit only one application instance in the same machine

    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
    Last edited by yxmaomao; 6th May 2008 at 08:56. Reason: misword

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to limit only one application instance in the same machine

    Isnt that class available in Qt 4 series ??

    I guess its available only in Commercial edition of Qt

  3. #3
    Join Date
    May 2008
    Posts
    58
    Thanks
    2

    Default Re: How to limit only one application instance in the same machine

    Yes , I am sure this class is not exist in Qt 4.3.2
    my version is commercail version

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to limit only one application instance in the same machine

    I found more as on way..

    one is subclass QApplication

    Qt Code:
    1. /* target find if bool Apps_Open become true */
    2. windowTitle = QString ( "Layer edit sample" );
    3. Apps_Open = false; /* default not open */
    4. const QString single_key = QString("ID 29385129512818123 by %1").arg(windowTitle);
    5.  
    6. #if defined Q_WS_WIN
    7. m_mutex = (void *) CreateMutexW( 0, false, (LPCWSTR) single_key.utf16() );
    8. m_prevInstance = ( GetLastError() == ERROR_ALREADY_EXISTS );
    9. if (m_prevInstance) {
    10. Apps_Open = true;
    11. }
    12. #endif
    13.  
    14. #ifdef Q_OS_UNIX
    15. unix_sock = make_local_app_socket();
    16. Apps_Open = unix_sock == 0 ? true : false;
    17. qDebug() << "### sock connect res " << Apps_Open << unix_sock;
    18. #endif
    19.  
    20. /* on destroy ~QApplication remove socked file */
    To copy to clipboard, switch view to plain text mode 

    unix socked connect function ...


    Qt Code:
    1. #ifdef Q_OS_UNIX
    2. #include <stddef.h>
    3. #include <stdio.h>
    4. #include <errno.h>
    5. #include <stdlib.h>
    6. #include <string.h>
    7. #include <sys/socket.h>
    8. #include <sys/un.h>
    9.  
    10. /* Unix SOCKET to comunicate on parent apps friend and send file or other msg! */
    11. #define _UNIX_SOCKET_ "/tmp/layeres.sock"
    12.  
    13.  
    14. int make_local_app_socket()
    15. {
    16. struct sockaddr_un name;
    17. int sock;
    18. size_t size;
    19. sock = socket (PF_LOCAL, SOCK_DGRAM, 0);
    20. if (sock < 0)
    21. {
    22. return 0;
    23. }
    24. name.sun_family = AF_LOCAL;
    25. strncpy (name.sun_path, _UNIX_SOCKET_, sizeof (name.sun_path));
    26. name.sun_path[sizeof (name.sun_path) - 1] = '\0';
    27. size = (offsetof (struct sockaddr_un, sun_path) + strlen (name.sun_path) + 1);
    28.  
    29. if (bind (sock, (struct sockaddr *) &name, size) < 0)
    30. {
    31. return 0;
    32. }
    33. return sock;
    34. }
    35. #endif
    To copy to clipboard, switch view to plain text mode 

    Focus to preview apps if is Apps_Open having true


    Qt Code:
    1. /* focus preview process ! */
    2. bool FocusInstance() const
    3. {
    4. #if defined Q_WS_WIN
    5. HWND hWnd = FindWindowW( 0, (LPCWSTR) windowTitle.utf16() );
    6. if( hWnd )
    7. {
    8. long id = GetWindowThreadProcessId( hWnd, 0 );
    9. AttachThreadInput( id, 0, true );
    10. if ( IsIconic( hWnd ) )
    11. ShowWindow( hWnd, SW_RESTORE );
    12. SetForegroundWindow( hWnd );
    13. AttachThreadInput( id, 0, false );
    14. return true;
    15. }
    16. #endif
    17. #ifdef Q_OS_UNIX
    18. QFileInfo sockets(QString(_UNIX_SOCKET_));
    19. qDebug() << "### having " << sockets.exists();
    20. if (unix_sock == 0) {
    21. qDebug() << "### send file to socket parent here... ";
    22. return true;
    23. }
    24. #endif
    25. return false;
    26. }
    To copy to clipboard, switch view to plain text mode 


    All in one ...

    http://fop-miniscribus.googlecode.co...wEdit/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"

  5. The following user says thank you to patrik08 for this useful post:

    yxmaomao (6th May 2008)

  6. #5
    Join Date
    May 2008
    Posts
    58
    Thanks
    2

    Default Re: How to limit only one application instance in the same machine

    Cool !
    Thanks patrik08

  7. #6
    Join Date
    May 2008
    Posts
    58
    Thanks
    2

    Default Re: How to limit only one application instance in the same machine

    hi patrik08
    Are you sure CreateMutexW( ) is Qt's function
    for my Qt4.3.2 , I can't find it

  8. #7
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to limit only one application instance in the same machine

    It's a Windows function. You have to include <windows.h>.

  9. #8
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to limit only one application instance in the same machine

    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).
    Current Qt projects : QCodeEdit, RotiDeCode

  10. #9
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: How to limit only one application instance in the same machine

    fullmetalcoder,
    Your avatar/icon is NEAT!

    I'm assuming you are a Linux enthusiast!

Similar Threads

  1. Only let one instance of the application run
    By chrisdsimpson in forum Newbie
    Replies: 1
    Last Post: 9th April 2008, 22:30
  2. Replies: 5
    Last Post: 16th January 2006, 06:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.