PDA

View Full Version : find window from another application



mentalmushroom
3rd May 2011, 07:36
hello. i would like to know if there is any way to get access to the window of another application. i want to prevent my application from being started twice, if that happens i'd like to find the main window of another application and bring it to the front. i can achieve this with WinAPI FindWindow, but i'd like it to be platform-independent.

mcosta
3rd May 2011, 10:45
you described is very platform dependent.

however, If you need it do avoid multiple instances of your app you can do it in different way.
For example using QtSingleApplication (https://qt.gitorious.org/qt-solutions/qt-solutions/trees/master/qtsingleapplication) from Qt-Solutions

mentalmushroom
3rd May 2011, 11:02
you described is very platform dependent
what is platform dependent? preventing from starting two instances or bringing a window to the front?

i heard about QtSingleApplication from Qt-Solutions, but not sure whether i can freely use it in the closed-source app. here (http://lists.trolltech.com/qt-interest/2004-08/msg00791.html) someone tells that Qt Enterprise license is needed to use it.

mcosta
3rd May 2011, 11:21
what is platform dependent?

To find Window for other application.

The link you posted is very old.
For Qt-Solution Licensing read here (http://qt.nokia.com/products/qt-addons/solutions-archive/index). Is released with BSD licence

MasterBLB
3rd May 2011, 12:54
there is a better and easier way to archieve that.Inside of your main.cpp add:

//do not allow to running multiple instances of the application
QSharedMemory mem("SomeUniquekeyNameThere");
if(!mem.create(1))
{
QMessageBox::critical(0,"Instance detected!","The application is already running!\nApplication terminating...","Ok");
exit(0);
}

mentalmushroom
3rd May 2011, 12:56
wouldn't you like to share it with us?

squidge
3rd May 2011, 13:13
I wouldn't say that was a better way. It provides no method of notifying the existing application that another instance is trying to start up, provides no way of passing parameters to a already running instance, doesn't cause the already running instance to bring itself to the front, etc.

Unless you have more code you would like to share?

mentalmushroom
3rd May 2011, 13:22
I have a problem of building qtsingleapplication. I ran "configure.bat" with "-library" switch, then called qmake and opened "qtsingleapplication.pro" with Visual Studio Qt Add-in, and when I tried to build it, I got the following error:
Error 2 error C2491: 'QtSingleApplication::staticMetaObject' : definition of dllimport static data member not allowed z:\qt-solutions-qt-solutions-master\qt-solutions-qt-solutions\qtsingleapplication\buildlib\release\moc _qtsingleapplication.cpp 51 QtSolutions_SingleApplication-head

But one project (console.pro) was successfully built.

MasterBLB
3rd May 2011, 13:43
I wouldn't say that was a better way. It provides no method of notifying the existing application that another instance is trying to start up, provides no way of passing parameters to a already running instance, doesn't cause the already running instance to bring itself to the front, etc.
True,but aboves are not a mentalmushroom's goal.I've never needed such detailed solution,but if I have I'd interest in QSessionManager.

@mentalmushroom
Show us all the code please.

squidge
3rd May 2011, 18:53
Its not his complete goal, but I assumed he wanted to at least bring the window of the running instance to the front and do it in a platform independant way. Once you can notify the existing application you can easily ask that application to bring itself to the front (rather than trying to find its windows and force it to the front).

MasterBLB
3rd May 2011, 19:29
Session manager seems not to be the right tool to do so,according to what's written in the assistant.
Well,real inter-process comunnication (I'm mean some that can withstand all conditions mentioned by squidge) provides DBus,QCOPChannel and TCP/IP.But according to mentalmushroom's description of OS something aviable for windows is needed,so it left only TCP/IP for consideration.Well,my idea then is somesuch:


thats still shoud be:
//do not allow to running multiple instances of the application
QSharedMemory mem("SomeUniquekeyNameThere");
if(!mem.create(1))
{
QMessageBox::critical(0,"Instance detected!","The application is already running!\nApplication terminating...","Ok");

//pseudocode now
send UDP datagram with let's say "SomeUniquekeyNameThere"
anyway,using datagram you may invent many cases for many purposes,not only activating currently running app's window.

exit(0);
}
else
{
//pseudocode now
create QUdpSocket on 127.0.0.1 and connect it's readyRead signal to some slot in your main window class
}

yourMainWindowClass::yourSlotReadingUDPDatagrams
{
//you've received SomeUniquekeyNameThere->use activateWindow
//and other desired behaviours
}


Shame on me I didn't saw the sentence about activationg the application window,I tougth the autor wants only prevent the other instances from running :/