Re: Notifying already running app

From: Winston Tuck (wtuck_at_yebo.co.za)
Date: 05/24/04


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.



Relevant Pages

  • Re: Create Forms via Parameter
    ... need the classname to retrieve a class registered via RegisterClasses. ... Function ShowFormByName(const formname: String; ... variable, the form reference, and that can be had the way shown above. ... The VCL contains a suitable class registry, ...
    (borland.public.delphi.language.objectpascal)
  • Re: DirectCom.dll: vb sample -> vfp
    ... GETINSTANCE erwartet als String-Parameter jeweils den Pointer ... Dim D As Object, FName As String, ClassName As String ... ClassName = StrConv'nach ANSI ...
    (microsoft.public.de.fox)
  • Re: String problem VS 2005
    ... the console the Classname and the text in the notepad window. ... The first output to the console is "N" and not ... I dont understand how to work with all the C++ string format. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Falsch Typ Erkennung ?
    ... Ich habe auch schon mit NHibernate gearbeitet. ... linke Seite immer ein "object" Typ deklariert. ... foreach(string className in classNames) ... object list = NHibernateMethod(className); ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: Get Path Where A Class Is Located
    ... Call it jwhich. ... And then use the class file compiled from the code below: ... @param className The full name of the class. ... public static String which ...
    (comp.lang.java.programmer)