Board index » delphi » PrevInstance

PrevInstance

Does anyone know how to activate a program that it already running?
Here's the scenario...I have a program that is already running
(iconized) and the user selects 'Run' from program manager and types
in the program name and a parameter.  The problem is how do I activate
the program already running and give it the parameter?

Thanks!!!

Jim

 

Re:PrevInstance


Re:PrevInstance


Quote
j...@cc.bellcore.com (Jim Howard) wrote:
>Does anyone know how to activate a program that it already running?
>Here's the scenario...I have a program that is already running
>(iconized) and the user selects 'Run' from program manager and types
>in the program name and a parameter.  The problem is how do I activate
>the program already running and give it the parameter?
>Thanks!!!
>Jim

Hi

const
  wm_MyMessage : WORD = 0;

var
  MemHdl : THandle;
  MemPtr : Pointer;
  MemLen : Word;
  OldWnd : HWND;
begin
{
  To be compatible with Win32 where hPrevInst is always 0 (also using
of hPrevInst in PostAppMessage cause GPF is USER.EXE... Why?) you
should use

Quote
}

  wm_MyMessage := RegisterWindowMessage('ThisIsMYMessage');
  OldWnd := FindWindow('YourApplicationClassName',
                       'YourOldWindowNameOrNIL_IfYouDontKnow');
  if OldWnd <> 0 then
    begin
{ should use Strings; 1 for terminating zero }
      MemLen := StrLen( CmdLine) + 1;
{ Allocate memory from windows }
      MemHdl := GlobalAlloc( GMEM_MOVEABLE, MemLen);
{ Lock it }
      MemPtr := GlobalLock( MemHdl);
{ Copy command line }
      StrCopy( PChar( MemPtr), CmdLine);
{ Unlock memory }
      GlobalUnlock( MemHdl);
{ Post message to previous instance }
      PostMessage( OldWnd, wm_MyMessage, MemHdl, 0);
      Exit;
    end;  

and in your WindowProc there will be

  WM_COMMAND:
    begin
      case wParam of
          {...}
          else
           if Message = wm_MyMessage then
            begin
              if IsIconic( Window) then
                ShowWindow( Window, SW_RESTORE)
              else
                begin
                  SetFocus( Window); { brings it to top }
 { Force window redraw }                  
                  InvalidateRect( Window, nil, TRUE);
                end;
              MemHdl := THandle( wParam);
              MemPtr := GlobalLock( MemHdl);
              ProcessArguments( MemPtr);
              GlobalUnlock( MemHdl);
              GlobalFree( MemHdl);
            end;
        end;
    end;

Should work... In attachment ther is a working example...

Cheers
  Dmitri

--------------------------------------------------------------
        Shit happens, but why it always happens to us?
--------------------------------------------------------------
Dmitri Poujlivyi            mailto:  dmi...@god.bel.alcatel.be
                            http://www.dma.be/p/bewoner/dmitri

begin 644 testact.pas
<uuencoded_portion_removed>
).PT*96YD+@T*
`
end

Other Threads