library FINDSELF;
{ This routine will locate any other APPs running with the same window
  CAPTION and if found, activate the other APP so that VBASIC can
  abort this instance
}
{These are the units used}
uses Strings, WinTypes;

{These are global variables to the DLL}
var
        ThWnd:integer;
      	Capt:Array[0..255] of Char;


{These are the API functions used}
procedure SetFocus (Wnd:hWnd); far;
	External 'USER' index 22;

procedure GetWindowText (Wnd:hWnd; Buff:PChar; BufSize:word); far;
	External 'USER' index 36;
procedure SendMessage (Wnd, Msg, wParam:word;lParam:PChar); far;
	External 'USER' index 111;
procedure EnumWindows (Func:TFarProc; lParam:PChar);far;
	External 'USER' index 54;


{This is the call-back function that is sent the hWnd of every window}
function EnumFunc (hWnd:hWnd; Search:PChar):Bool;export;
	Begin
      {First retrieve the caption of the Window}
  		GetWindowText (hWnd, @Capt, 256);
      {StrPos is identical to Instr. We cap both strings to be insensitive}
   	if StrPos (StrUpper (@Capt), StrUpper(Search)) <> Nil then
  			Begin
                           { APPActivate "SEARCH" }
                              SetFocus(hWnd);
                           {ThWnd = ThWnd + 1}
           	             Inc (ThWnd);

                        end;
  		   EnumFunc := TRUE;
   End;

function FindAppSelf (Search:PChar):Integer;export;
	Begin
      {Set counter to zero}
      ThWnd := 0;
      {Tell Windows to start sending hWnds to EnumFunc}
      EnumWindows (@EnumFunc, Search);
      {When all windows have been examined, return total to VB}
      FindAppSelf := ThWnd div 2;
                               { remove the DOUBLEing }
   end;



Exports
   EnumFunc,
   {The resident keyword makes access from VB quicker}
	FindAppSelf resident;


{No startup code but we need the empty routine anyway}
Begin
End.
