home *** CD-ROM | disk | FTP | other *** search
/ synchro.net / synchro.net.tar / synchro.net / main / COMM / CTA6_SRC.ZIP / relay.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-01-20  |  3.3 KB  |  118 lines

  1. {
  2.   Relay Unit loads and executes plugin DLL's and also
  3.   recieves data from main application and than decides
  4.   where the data belongs.
  5.  
  6.   Programmer    version  Date        Comment on code change
  7.   ------------  -------  ----------  ------------------------------------------------------------
  8.   Shawn         0.0.1    05/20/2000  Initial version
  9.  
  10.   UNFINISHED CODE!  Idea behind this is that eventually
  11.   emulations such as Ansi, Rip, etc and protocals such as
  12.   X, y, Z would be plugin's. 
  13. }
  14. unit relay;
  15.  
  16. interface
  17.  
  18. uses classes, Windows, Forms, SysUtils;
  19.  
  20. type
  21.   //plug in object
  22.   TTestPlugIn = class
  23.     Name: String;
  24.     Address: Integer;
  25.     Call: Pointer;
  26.   end;
  27.   GetNameFunction = function : PChar;
  28.   PlugInInit = procedure (Owner: Integer);
  29.  
  30. procedure SearchFileExt(const Dir, Ext: String; Files: TStrings);
  31. procedure LoadPlugIns;
  32.  
  33. implementation
  34.  
  35. uses Main;
  36.  
  37. var
  38.   Plugins: TList;
  39.   StopSearch: Boolean;
  40.  
  41. procedure SearchFileExt(const Dir, Ext: String; Files: TStrings);
  42. var
  43.   Found: TSearchRec;
  44.   Sub: String;
  45.   i : Integer;
  46.   Dirs: TStrings; //Store sub-directories
  47.   Finished : Integer; //Result of Finding
  48. begin
  49.   StopSearch := False;
  50.   Dirs := TStringList.Create;
  51.   Finished := FindFirst(Dir + '*.*', 63, Found);
  52.   while (Finished = 0) and not (StopSearch) do
  53.     begin
  54.       //Check if the name is valid.
  55.       if (Found.Name[1] <> '.') then
  56.           begin
  57.           //Check if file is a directory
  58.           if (Found.Attr and faDirectory = faDirectory) then
  59.               Dirs.Add(Dir + Found.Name)  //Add to the directories list.
  60.           else if Pos(UpperCase(Ext), UpperCase(Found.Name))>0 then
  61.             Files.Add(Dir + Found.Name);
  62.     end;
  63.     Finished := FindNext(Found);
  64.   end;
  65.   //end the search process.
  66.   FindClose(Found);
  67.   //Check if any sub-directories found
  68.   if not StopSearch then
  69.     for i := 0 to Dirs.Count - 1 do
  70.       //If sub-dirs then search agian ~>~>~> on and on, until it is done.
  71.       SearchFileExt(Dirs[i], Ext, Files);
  72.  
  73.   //Clear the memories.
  74.   Dirs.Free;
  75. end;
  76.  
  77. procedure LoadPlugIns;
  78. var
  79.   Files: TStrings;
  80.   i: Integer;
  81.   TestPlugIn : TTestPlugIn;
  82. begin
  83.   Files := TStringList.Create;
  84.   Plugins := TList.Create;
  85.   //Search what ever is in the app's dir
  86.   SearchFileExt(ExtractFilepath(Application.Exename) + '\', '.dll', Files);
  87.   for i := 0 to Files.Count-1 do
  88.     begin
  89.       //create a new plug in
  90.       TestPlugIn := TTestPlugIn.Create;
  91.       TestPlugIn.Address := LoadLibrary(PChar(Files[i]));
  92.       //Initialize the plugin give your app instance (and the handle if necessary)
  93.       PlugInInit(GetProcAddress(TestPlugIn.Address, 'Init'))(HInstance);
  94.       //get the a menu item
  95.       TestPlugIn.Name := GetNameFunction(GetProcAddress(TestPlugIn.Address, 'GetName'));
  96.       //get the function insert text
  97.       TestPlugIn.Call := GetProcAddress(TestPlugIn.Address, 'InsertText');
  98.       PlugIns.Add(TestPlugIn);
  99.     end;
  100.   Files.Free;
  101. end;
  102.  
  103. procedure FreePlugIns;
  104. var
  105.   i: Integer;
  106. begin
  107.   for i := 0 to PlugIns.Count-1 do
  108.    begin
  109.      //Run finalize function in the plugin before you unload it.
  110.      //Because it is not applicable here so it is ignored.
  111.      //free every loaded plugins
  112.      FreeLibrary(TTestPlugIn(PlugIns[i]).Address);
  113.   end;
  114.   PlugIns.Free;
  115. end;
  116.  
  117. end.
  118.