Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to restore my application instead of running a new instance?

Status
Not open for further replies.

julych

Programmer
Dec 21, 2002
14
BG
How to restore (maximize) my application instead of running a new instance when another instance is already running?
 
This is a piece of one of my servers. "IonFilipski-db2orb-v3-server" is just a name for a segment which could be any other names. The segment with this name is shared and variables inside it are accessible to any applications using this segment. If the flag isRunningAlready is true, show server window and exit application. So will be shown only the first instance of application. You should add explicitly /section:IonFilipski-db2orb-v3-server,rws to the linker. RWS means read/write/shared.


.....
#pragma data_seg("IonFilipski-db2orb-v3-server")
//begin of segment
static bool isRunningAlready = false;
HWND hServerWnd = 0;
#pragma data_seg() //end of segment
....
//linker:/section:IonFilipski-db2orb-v3-server,rws

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
if(!isRunningAlready)
{
isRunningAlready = true;
}else
{
if(hServerWnd)
{
ShowWindow(hServerWnd, SW_SHOW);
SetForegroundWindow(hServerWnd);
}
return 0;
}
.....
...
}

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
I had to do the same thing. I chose to use a CMutex instead. If you do use the CMutex route, you will need an Enum Windows proc to find your windows and a message handler to return the HWND. With Ion's way, the data_seg is system wide and the only problem I could see with it is the rare case that the app is launched twice before either sets the data_seg members. I say go the easy way with Ion's post but keep the mutex in mind just incase :)

Matt
 
yes, sinchronise this piece of code with a mutex:
if(!isRunningAlready)
{
isRunningAlready = true;
}else
{
if(hServerWnd)
{
ShowWindow(hServerWnd, SW_SHOW);
SetForegroundWindow(hServerWnd);
}
return 0;
}

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top