unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  // Поток для работы с DLL 
  TShowServerInfo_Thread = class(TThread)
  private
  { Private declarations }
  public
  protected
    procedure Execute; override;
  end;

var
  Form1: TForm1;
  ServerInfoWindows: Integer;
  ServerInfoHandels: THandle;

implementation

{$R *.dfm}

procedure ServerInfo_Show;
var
  ShowInfo: procedure(Win: Integer); stdcall;
  FormClosed: function(Win: Integer): Integer;
  SetMainFrmHandle: procedure(SAppHandle, SAppFormHandle: THandle); stdcall;
  Win: Integer;
begin
  if ServerInfoWindows = 0 then ServerInfoHandels := LoadLibrary(PChar(ExtractFilePath(Application.ExeName) + 'dll\dll.dll'));
  if ServerInfoHandels >= 32 then
  begin
    @ShowInfo := GetProcAddress(ServerInfoHandels, 'ShowInfo');
    @FormClosed := GetProcAddress(ServerInfoHandels, 'FormClosed');
    @SetMainFrmHandle := GetProcAddress(ServerInfoHandels, 'SetMainFrmHandle');
    if (@SetMainFrmHandle <> nil) then SetMainFrmHandle(Application.Handle, Form1.Handle);
    if (@ShowInfo <> nil) and (@FormClosed <> nil) then
    begin
      Inc(ServerInfoWindows);
      Win := ServerInfoWindows;
      ShowInfo(Win);
      while FormClosed(Win) = 0 do
      begin
        Sleep(10);
        Application.ProcessMessages;
      end;
      while FormClosed(Win) = 0 do Sleep(50);
      Dec(ServerInfoWindows);
      if ServerInfoWindows = 0 then FreeLibrary(ServerInfoHandels);
    end;
  end;
end;

procedure ServerInfo_ShowThread;
var
  ShowInfoThread: TShowServerInfo_Thread;
begin
  ShowInfoThread := TShowServerInfo_Thread.Create(True);
  ShowInfoThread.FreeOnTerminate := True;
  ShowInfoThread.Resume;
end;

procedure TShowServerInfo_Thread.Execute;
begin
  FreeOnTerminate := True;
  ServerInfo_Show;
  Terminate;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ServerInfo_ShowThread;
end;

end.
