//昔漏れが作った多重起動防止用ユニット
unit SngPrcs;
interface
uses Windows;

  function IsSecondProcess(const UniqueString:String):Boolean;//二重起動チェック
  procedure ActivatePrevInst(MainFormClass:TClass);//先行プロセスのメインフォームをアクティブにする

implementation

function IsSecondProcess(const UniqueString:String):Boolean;
begin
  
  CreateMutex(NIL,False,pChar(UniqueString) );//みゅーテックスオブジェクトを作成してみる
  Result :=(GetLastError=ERROR_ALREADY_EXISTS);//ミューテックス作成エラーが起きた場合は二重起動とみなす
end;

procedure ActivatePrevInst(MainFormClass:TClass);
var
  Hwnd,HAppWnd:THandle;
begin
  Hwnd := FindWindow(pChar(String(MainFormClass.ClassName)), nil);
  if Bool(Hwnd) then
  begin
    SetForegroundWindow(Hwnd);//メインフォームをアクティブ化
    HAppWnd := GetWindowLong(Hwnd, GWL_HWNDPARENT);//アプリケーションハンドルを取得
    if HAppWnd <> 0 then Hwnd := HAppWnd;
    if IsIconic(Hwnd) then ShowWindow(Hwnd,SW_RESTORE);//アイコン状態なら元に戻す
  end;
end;
{==SAMPLE=======================================================
program Project1;
uses
 Forms,
 SngPrcs in 'SngPrcs.pas',
 wMain in 'wMain.pas' {Main};

const
  UNIQUEID='TEST APPLICATIONS ID';
begin
  if IsSecondProcess(UNIQUEID) then
  begin
    ActivatePrevInst(TMain);//←ここに指定するのはクラス型そのもを指定すること。
    Exit;
  end;
  Application.Initialize;
  Application.CreateForm(TMain, Main);
  Application.Run;
end.

==SAMPLE=======================================================}