Thread: Focus initial instance from a secondary instance

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    70

    Focus initial instance from a secondary instance

    Hi,

    I want to recreate a feature I have seen before, but I can't find any reference of how to implement it.

    I have a dropbox application (custom, encrypted, ftp-like client), and I want to make sure that only one instance can be run at a time. I have a lock file which makes sure of this.

    But what I'd like is if a second instance is run, and finds the first instance still running, that it gives focus to the first instance and then the second instance exits.

    I think it might be handled by the window manager rather than the program itself, but to me it's worth asking about. Can someone please shed some light by point me in the right direction?

    Ty.

    EDIT*:
    Maybe the communication can be done using shared memory? But this still doesn't help with the focus of a non-child window.
    Last edited by Yonut; 2 Weeks Ago at 10:56 AM.

  2. #2
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    229
    Quote Originally Posted by Yonut View Post
    Hi,

    I want to recreate a feature I have seen before, but I can't find any reference of how to implement it.

    I have a dropbox application (custom, encrypted, ftp-like client), and I want to make sure that only one instance can be run at a time. I have a lock file which makes sure of this.

    But what I'd like is if a second instance is run, and finds the first instance still running, that it gives focus to the first instance and then the second instance exits.

    I think it might be handled by the window manager rather than the program itself, but to me it's worth asking about. Can someone please shed some light by point me in the right direction?

    Ty.

    EDIT*:
    Maybe the communication can be done using shared memory? But this still doesn't help with the focus of a non-child window.
    It's been a while since I've done any WIN32 programming (I assume that what you're talking about here) so not 100% sure this is correct but from what I remember anyway. If the WM_CREATE message CREATESTRUCT "hInstance" parameter equals GetModuleHandle(NULL) then there is only one instance currently running.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    70
    Quote Originally Posted by Sir Galahad View Post
    It's been a while since I've done any WIN32 programming (I assume that what you're talking about here) so not 100% sure this is correct but from what I remember anyway. If the WM_CREATE message CREATESTRUCT "hInstance" parameter equals GetModuleHandle(NULL) then there is only one instance currently running.
    Hi, thanks for the reply.

    It is actually for Linux and then (down the road) Mac OS.

    For an example, just in case I wasn't making sense...

    I can load a pdf with `evince`, scroll down a bit so it's not at the beginning, and minimize the application. Then I can run the same command I previously ran, and evince will not spawn another instance, and it makes the existing instance un-minimized and focused.

    I have figured out a system of the lock file for Linux using `flock`. But I don't know how I would be able to make the initial instance un-minimized and, if possible, have focus.

  4. #4
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    229
    Ah, in that case maybe the wmctrl command? (Example usage discussed here.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can i do instance polymorphism?
    By joaquim in forum C++ Programming
    Replies: 17
    Last Post: 02-03-2014, 03:33 PM
  2. FindWindowEx - By INSTANCE not by name
    By Devils Child in forum C# Programming
    Replies: 5
    Last Post: 02-27-2010, 03:19 AM
  3. multiple instance of ADT
    By Kinasz in forum C Programming
    Replies: 4
    Last Post: 04-07-2004, 06:49 AM
  4. Deleted instance
    By Boksha in forum C++ Programming
    Replies: 3
    Last Post: 05-28-2002, 11:15 AM
  5. How do you only allow one instance of an app?
    By genghis in forum Windows Programming
    Replies: 6
    Last Post: 01-24-2002, 08:31 PM

Tags for this Thread