Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » U++ Developers corner » Open file in already opened app instead of launching a second copy
Open file in already opened app instead of launching a second copy [message #25970] Mon, 22 March 2010 17:39 Go to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
I wonder how that can be done...
I have a multi-document application; when it's already running I'd like, if double clicking on another document, to have the app loading the second document instead of launching a new app instance.
On Linux I did (some time ago) with my Bazaar Signals package, to tell opened app instance to load the new file before exiting.
On Windows I've no hints about....

Ciao

Max
Re: Open file in already opened app instead of launching a second copy [message #25971 is a reply to message #25970] Mon, 22 March 2010 19:36 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
mdelfede wrote on Mon, 22 March 2010 17:39

I wonder how that can be done...
I have a multi-document application; when it's already running I'd like, if double clicking on another document, to have the app loading the second document instead of launching a new app instance.
On Linux I did (some time ago) with my Bazaar Signals package, to tell opened app instance to load the new file before exiting.
On Windows I've no hints about....

Ciao

Max



Hello Massimo

From this post.

First version (Windows only):

#include <CtrlLib/CtrlLib.h>
using namespace Upp;

class UniqueWindow : public TopWindow
{
public:
	typedef UniqueWindow CLASSNAME;
	UniqueWindow() {
		Title("SingleApp Test");
	}
	bool IsSingleApp() {
		if(::FindWindow(NULL, GetTitle().ToString())) 
			return false;
		return true;
	}
};

GUI_APP_MAIN
{
	if (!UniqueWindow().IsSingleApp()) {
		Exclamation("Another instance of application already exists!");
		return;
	}	
	UniqueWindow().Run();
}



Second version (Windows and Linux). It requires SysInfo:

#include <CtrlLib/CtrlLib.h>
#include <SysInfo/SysInfo.h>
using namespace Upp;

class UniqueWindow : public TopWindow
{
public:
	typedef UniqueWindow CLASSNAME;
	UniqueWindow() {
		Title("SingleApp Test");
	}
	bool IsSingleApp() {
		if(GetWindowIdFromCaption(GetTitle().ToString()) > 0) 
			return false;
		return true;
	}
};

GUI_APP_MAIN
{
	if (!UniqueWindow().IsSingleApp()) {
		Exclamation("Another instance of application already exists!");
		return;
	}	
	UniqueWindow().Run();
}


Only change is to add #include <SysInfo/SysInfo.h> and instedad of FindWindow() I have used GetWindowIdFromCaption().


Best regards
Iñaki
Re: Open file in already opened app instead of launching a second copy [message #25973 is a reply to message #25971] Mon, 22 March 2010 20:49 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
koldo wrote on Mon, 22 March 2010 19:36

mdelfede wrote on Mon, 22 March 2010 17:39

I wonder how that can be done...
I have a multi-document application; when it's already running I'd like, if double clicking on another document, to have the app loading the second document instead of launching a new app instance.
On Linux I did (some time ago) with my Bazaar Signals package, to tell opened app instance to load the new file before exiting.
On Windows I've no hints about....

Ciao

Max



Hello Massimo

From this post.

First version (Windows only):

#include <CtrlLib/CtrlLib.h>
using namespace Upp;

class UniqueWindow : public TopWindow
{
public:
	typedef UniqueWindow CLASSNAME;
	UniqueWindow() {
		Title("SingleApp Test");
	}
	bool IsSingleApp() {
		if(::FindWindow(NULL, GetTitle().ToString())) 
			return false;
		return true;
	}
};

GUI_APP_MAIN
{
	if (!UniqueWindow().IsSingleApp()) {
		Exclamation("Another instance of application already exists!");
		return;
	}	
	UniqueWindow().Run();
}



Second version (Windows and Linux). It requires SysInfo:

#include <CtrlLib/CtrlLib.h>
#include <SysInfo/SysInfo.h>
using namespace Upp;

class UniqueWindow : public TopWindow
{
public:
	typedef UniqueWindow CLASSNAME;
	UniqueWindow() {
		Title("SingleApp Test");
	}
	bool IsSingleApp() {
		if(GetWindowIdFromCaption(GetTitle().ToString()) > 0) 
			return false;
		return true;
	}
};

GUI_APP_MAIN
{
	if (!UniqueWindow().IsSingleApp()) {
		Exclamation("Another instance of application already exists!");
		return;
	}	
	UniqueWindow().Run();
}


Only change is to add #include <SysInfo/SysInfo.h> and instedad of FindWindow() I have used GetWindowIdFromCaption().

This could be improved using GetWindowsList(), so it is possible to compare our program file name and caption with all running processes.


Best regards
Iñaki
Re: Open file in already opened app instead of launching a second copy [message #25978 is a reply to message #25973] Mon, 22 March 2010 23:25 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
Thank you, Koldo Smile

But now the problem is that second application should "send" to first one it's command line, so if I doubleclick a file it'll open on the first running instance.

Example, filea a.myapp and b.myapp

I doubleclick on a.myapp, the application opens and loads a.myapp file

then I doubleclick on b.myapp, the app start, discovers that another instance is running, "sends" its command line to the running instance and leave, so running instance can open the second file.
I can do on Linux, but I'd like a portable way....

Ciao

Max
Re: Open file in already opened app instead of launching a second copy [message #25989 is a reply to message #25978] Tue, 23 March 2010 21:58 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
mdelfede wrote on Mon, 22 March 2010 23:25

Thank you, Koldo Smile

But now the problem is that second application should "send" to first one it's command line, so if I doubleclick a file it'll open on the first running instance.

Example, filea a.myapp and b.myapp

I doubleclick on a.myapp, the application opens and loads a.myapp file

then I doubleclick on b.myapp, the app start, discovers that another instance is running, "sends" its command line to the running instance and leave, so running instance can open the second file.
I can do on Linux, but I'd like a portable way....

Ciao

Max


Well, at least you can know which is the application you have to communicate with Smile


Best regards
Iñaki
Re: Open file in already opened app instead of launching a second copy [message #25991 is a reply to message #25989] Tue, 23 March 2010 23:16 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
koldo wrote on Tue, 23 March 2010 21:58



Well, at least you can know which is the application you have to communicate with Smile


Eheheh.. right.
I found 2 solutions, one for windows and one for linux, but they've quite different approaches and requires some low-level CtrlCore stuffs.

IMHO the end solution would be to have a call to PostCallback() with the command line of second instance fired on first instance.
That would allow any handling without disturbing the app in the middle of some operations.

Ciao

Max
Re: Open file in already opened app instead of launching a second copy [message #25992 is a reply to message #25991] Tue, 23 March 2010 23:34 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi Max,

Simple IPC class I wrote some time ago could solve your problem. There were some problems I don't have time to fix right now... But most of them are in the both way version and your problem can be solved in simple client-server mode. If you are adventurous enough, have a look in this thread Wink

Regards,
Honza
Re: Open file in already opened app instead of launching a second copy [message #25994 is a reply to message #25992] Wed, 24 March 2010 13:02 Go to previous message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
dolik.rce wrote on Tue, 23 March 2010 23:34

Hi Max,

Simple IPC class I wrote some time ago could solve your problem. There were some problems I don't have time to fix right now... But most of them are in the both way version and your problem can be solved in simple client-server mode. If you are adventurous enough, have a look in this thread Wink

Regards,
Honza


Thank you, honza Smile
Maybe I'll take a look today and try to implement a bazaar class for it.

Ciao

Max
Previous Topic: libpng1.4
Next Topic: [SOLVED] Xmlize LineEdit with line breaks inside
Goto Forum:
  


Current Time: Fri Mar 29 13:14:03 CET 2024

Total time taken to generate the page: 0.01260 seconds