Re: Notifying already running app
From: Winston Tuck (wtuck_at_yebo.co.za)
Date: 05/24/04
- Next message: Maarten Wiltink: "Re: OO Starter"
- Previous message: Moses Palmér: "Re: OO Starter"
- In reply to: J French: "Re: Notifying already running app"
- Next in thread: J French: "Re: Notifying already running app"
- Reply: J French: "Re: Notifying already running app"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 24 May 2004 22:39:35 +0200
Hi here it is. I looks like it is not finding the other window, the
foundwindow stays 0 after the loop, but I am not to sure and will appreciate
your help.
Thanks to Mastering Delphi6 the following
SERVER APP:
Project File
var
hMutexS: THandle;
FoundWndS: THandle;
ModuleNameS: string;
function EnumWndProc (hwnd: THandle;
Param: Cardinal): Bool; stdcall;
var
ClassName, WinModuleName: string;
WinInstance: THandle;
begin
Result := True;
SetLength (ClassName, 100);
GetClassName (hwnd, PChar (ClassName), Length (ClassName));
ClassName := PChar (ClassName);
if ClassName = TMAinForm.ClassName then
begin
// get the module name of the target window
SetLength (WinModuleName, 200);
WinInstance := GetWindowLong (hwnd, GWL_HINSTANCE);
GetModuleFileName (WinInstance,
PChar (WinModuleName), Length (WinModuleName));
WinModuleName := PChar(WinModuleName); // adjust length
// compare module names
if WinModuleName = ModuleNameS then
begin
FoundWndS := Hwnd;
Result := False; // stop enumeration
end;
end;
end;
begin
// check if mutex already exists
HMutexS := CreateMutex (nil, False, 'MutexS');
if WaitForSingleObject (hMutexS, 0) <> wait_TimeOut then
begin
Application.Initialize;
Application.CreateForm(TMainform, Mainform);
Application.Run;
end
else
begin
// get the current module name
SetLength (ModuleNameS, 200);
GetModuleFileName (HInstance,
PChar (ModuleNameS), Length (ModuleNameS));
ModuleNameS := PChar (ModuleNameS); // adjust length
// find window of previous instance
EnumWindows (@EnumWndProc, 0);
if FoundWndS <> 0 then
begin
// show the window, eventually
if not IsWindowVisible (FoundWndS) then
PostMessage (FoundWndS, wm_User+59, 0, 0);
SetForegroundWindow (FoundWndS);
end;
end;
end.
Server MainForm
public
procedure wmuser(var msg: TMEssage); message wm_user + 59;
procedure TMainForm.wmuser(var msg: TMEssage);
begin
Application.Restore;
end;
CLIENT APP:
PROJECT FILE:
var
hMutexC: THandle;
FoundWnd: THandle;
ModuleName: string;
function EnumWndProc (hwnd: THandle;
Param: Cardinal): Bool; stdcall;
var
ClassName, WinModuleName: string;
WinInstance: THandle;
begin
Result := True;
SetLength (ClassName, 100);
GetClassName (hwnd, PChar (ClassName), Length (ClassName));
ClassName := PChar (ClassName);
if ClassName = TMainForm.ClassName then
begin
// get the module name of the target window
SetLength (WinModuleName, 200);
WinInstance := GetWindowLong (hwnd, GWL_HINSTANCE);
GetModuleFileName (WinInstance,
PChar (WinModuleName), Length (WinModuleName));
WinModuleName := PChar(WinModuleName); // adjust length
// compare module names
if WinModuleName = ModuleName then
begin
FoundWnd := Hwnd;
Result := False; // stop enumeration
end;
end;
end;
begin
// check if mutex already exists
HMutexC := CreateMutex (nil, False, 'MutexClient');
if WaitForSingleObject (hMutexC, 0) <> wait_TimeOut then
begin
Application.Initialize;
Application.CreateForm(TMainform, Mainform);
Application.Run;
end
else
begin
// get the current module name
SetLength (ModuleName, 200);
GetModuleFileName (HInstance,
PChar (ModuleName), Length (ModuleName));
ModuleName := PChar (ModuleName); // adjust length
// find window of previous instance
EnumWindows (@EnumWndProc, 0);
if FoundWnd <> 0 then
begin
// show the window, eventually
if not IsWindowVisible (FoundWnd) then
PostMessage (FoundWnd, wm_User + 1, 0, 0);
SetForegroundWindow (FoundWnd);
end;
end;
end.
Client Mainform
public
procedure wmuser(var msg: TMEssage); message wm_user + 1;
procedure TMainForm.wmuser(var msg: TMEssage);
begin
Application.Restore;
end;
"J French" <erewhon@nowhere.com> wrote in message
news:40b1ec41.11475712@news.btclick.com...
> On Mon, 24 May 2004 12:35:10 +0200, "WT" <wtuck@yebo.co.za> wrote:
>
> >Hi there
> >
> >I have 2 applications, a APPServer.exe and APPClient.exe.
> >
> >The Appserver.exe acts as a "server" application handling all requests on
> >the network from the Appclient.exe's.
> >
> >I am using a mutex to detect that a running app is already running. This
> >works and I send a message to restore the already running application.
This
> >works fine until I open both the AppServer.exe and AppClient.exe then my
> >messages does not work anymore. The already running detection still
works.
> >
> >What can the problem be?
>
> You would not be using the same 'Unique String' for the Mutex and
> RegisterWindowMessage for both Client and Server ?
>
> Best show us the code you are using for detecting the previous
> instance and communicating with it.
- Next message: Maarten Wiltink: "Re: OO Starter"
- Previous message: Moses Palmér: "Re: OO Starter"
- In reply to: J French: "Re: Notifying already running app"
- Next in thread: J French: "Re: Notifying already running app"
- Reply: J French: "Re: Notifying already running app"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|