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 » Extra libraries, Code snippets, applications etc. » U++ users applications in progress and useful code snippets, including reference examples! » Just one instance of application running (SingleApp)
Just one instance of application running (SingleApp) [message #24603] Mon, 25 January 2010 14:42 Go to next message
Sc0rch is currently offline  Sc0rch
Messages: 99
Registered: February 2008
Location: Russia, Rubtsovsk
Member

Header:
#ifndef SINGLE_APP_H
#define SINGLE_APP_H

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

class UniqueWindow : public TopWindow
{
public:
	typedef UniqueWindow CLASSNAME;
	UniqueWindow(){}
};

inline bool CreateSingleApp(String name, String unique, bool message = true)
{
#ifdef PLATFORM_WIN32
	name << unique;
	if(::FindWindow(NULL, name))
	{
		if (message)
			Exclamation("Another instance of application already exists!");
		return false;
	}
	Single<UniqueWindow>().SetRect(-1, -1, 1, 1);
	Single<UniqueWindow>().Hide();
	Single<UniqueWindow>().Title(name);
	Single<UniqueWindow>().Open();
#endif
	return true;
}

#endif // SINGLE_APP_H


Using:
GUI_APP_MAIN
{
	if (!CreateSingleApp("SingleApp Test", "##SingleApp##1.0"))
		return;

	MainWindow().Run();
}
Re: Just one instance of application running (SingleApp) [message #24609 is a reply to message #24603] Mon, 25 January 2010 16:11 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Sc0rch wrote on Mon, 25 January 2010 14:42

Header:
#ifndef SINGLE_APP_H
#define SINGLE_APP_H

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

class UniqueWindow : public TopWindow
{
public:
	typedef UniqueWindow CLASSNAME;
	UniqueWindow(){}
};

inline bool CreateSingleApp(String name, String unique, bool message = true)
{
#ifdef PLATFORM_WIN32
	name << unique;
	if(::FindWindow(NULL, name))
	{
		if (message)
			Exclamation("Another instance of application already exists!");
		return false;
	}
	Single<UniqueWindow>().SetRect(-1, -1, 1, 1);
	Single<UniqueWindow>().Hide();
	Single<UniqueWindow>().Title(name);
	Single<UniqueWindow>().Open();
#endif
	return true;
}

#endif // SINGLE_APP_H


Using:
GUI_APP_MAIN
{
	if (!CreateSingleApp("SingleApp Test", "##SingleApp##1.0"))
		return;

	MainWindow().Run();
}



Hello ScOrch

If you use GetWindowIdFromCaption() (SysInfo package) instead of FindWindow, this code will serve you for Linux too.

Sorry for the propaganda Smile

Best regards
Koldo



Best regards
Iñaki
Re: Just one instance of application running (SingleApp) [message #24617 is a reply to message #24609] Mon, 25 January 2010 18:09 Go to previous messageGo to next message
Sc0rch is currently offline  Sc0rch
Messages: 99
Registered: February 2008
Location: Russia, Rubtsovsk
Member

koldo wrote on Mon, 25 January 2010 21:11


Hello Sc0rch

If you use GetWindowIdFromCaption() (SysInfo package) instead of FindWindow, this code will serve you for Linux too.

Sorry for the propaganda Smile

Best regards
Koldo



Hmm, function not work, what I'm doing wrong?

if (GetWindowIdFromCaption(name, true) >= 0)
	{
		if (message)
			Exclamation("Another instance of application already exists!");
		return false;
	}
Re: Just one instance of application running (SingleApp) [message #24627 is a reply to message #24617] Mon, 25 January 2010 22:52 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Sc0rch wrote on Mon, 25 January 2010 18:09

koldo wrote on Mon, 25 January 2010 21:11


Hello Sc0rch

If you use GetWindowIdFromCaption() (SysInfo package) instead of FindWindow, this code will serve you for Linux too.

Sorry for the propaganda Smile

Best regards
Koldo



Hmm, function not work, what I'm doing wrong?

if (GetWindowIdFromCaption(name, true) >= 0)
	{
		if (message)
			Exclamation("Another instance of application already exists!");
		return false;
	}



Hello Sc0rch

GetWindowIdFromCaption(name, true) tries to find a window with title == name.

GetWindowIdFromCaption(name, false) will get the window handle of the window with a title that just contains name.

If it does not work could you give me more details ?.

Best regards
Koldo


Best regards
Iñaki
Re: Just one instance of application running (SingleApp) [message #24634 is a reply to message #24627] Tue, 26 January 2010 05:41 Go to previous messageGo to next message
Sc0rch is currently offline  Sc0rch
Messages: 99
Registered: February 2008
Location: Russia, Rubtsovsk
Member

You said:
koldo wrote on Tue, 26 January 2010 03:52


Hello Sc0rch

GetWindowIdFromCaption(name, true) tries to find a window with title == name.

GetWindowIdFromCaption(name, false) will get the window handle of the window with a title that just contains name.

If it does not work could you give me more details ?.

Best regards
Koldo



I've tested both variants, works well for direct use, like:
TopWindow wnd;
wnd.Title("Test");
wnd.Open();

DUMP(GetWindowIdFromCaption("Test"));


But, not working in this code:
#include <CtrlLib/CtrlLib.h>
#include <SysInfo/SysInfo.h>
using namespace Upp;

class UniqueWindow : public TopWindow
{
public:
	UniqueWindow() {}
};

inline bool CreateSingleApp(const String& unique, const String& message)
{
	if (GetWindowIdFromCaption(unique, false) >= 0)
	{
		if (!message.IsEmpty())
			Exclamation(message);
		return false;
	}

	Single<UniqueWindow>().Title(unique).SetRect(-1, -1, 1, 1);
	Single<UniqueWindow>().Hide();
	Single<UniqueWindow>().Open();
	return true;
}

GUI_APP_MAIN
{
	if (!CreateSingleApp("SingleApp Test##SingleApp##1.0", "Another instance of application "
		"already exists!"))
	{
		return;
	}

	TopWindow wnd;
	wnd.SetRect(Size(200, 100));
	wnd.Run();
}


Test, and say, please, maybe it not works only for me.
Re: Just one instance of application running (SingleApp) [message #24638 is a reply to message #24634] Tue, 26 January 2010 09:37 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello Sc0rch

I have compiled your original version and final version and none of them work for me Sad

I have changed them a little and now they work for me:

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):
#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
Koldo


Best regards
Iñaki
Re: Just one instance of application running (SingleApp) [message #24644 is a reply to message #24603] Tue, 26 January 2010 11:11 Go to previous messageGo to next message
Sc0rch is currently offline  Sc0rch
Messages: 99
Registered: February 2008
Location: Russia, Rubtsovsk
Member

Eurika, Koldo! Function GetWindowIdFromCaption works only for visible windows! So your variant is better, thanks!

Code is clear, but I think this:
	bool IsSingleApp() {
		return GetWindowIdFromCaption(GetTitle().ToString()) <= 0;
	}

looks a little bit better than:

	bool IsSingleApp() {
		if(GetWindowIdFromCaption(GetTitle().ToString()) > 0) 
			return false;
		return true;
	}

or not? Rolling Eyes

Best regards,
Anton

[Updated on: Tue, 26 January 2010 11:11]

Report message to a moderator

Re: Just one instance of application running (SingleApp) [message #24645 is a reply to message #24644] Tue, 26 January 2010 11:17 Go to previous message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Sc0rch wrote on Tue, 26 January 2010 11:11

Eurika, Koldo! Function GetWindowIdFromCaption works only for visible windows! So your variant is better, thanks!

Code is clear, but I think this:
	bool IsSingleApp() {
		return GetWindowIdFromCaption(GetTitle().ToString()) <= 0;
	}

looks a little bit better than:

	bool IsSingleApp() {
		if(GetWindowIdFromCaption(GetTitle().ToString()) > 0) 
			return false;
		return true;
	}

or not? Rolling Eyes

Best regards,
Anton


Of course your is better Very Happy


Best regards
Iñaki
Previous Topic: Port giFT (a great but abandoned P2P project) to U++?
Next Topic: array vs. array's items.id sorting/searching
Goto Forum:
  


Current Time: Thu Mar 28 15:43:04 CET 2024

Total time taken to generate the page: 0.01525 seconds