home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 April A / Pcwk4a98.iso / PROGRAM / DELPHI16 / Ypasswrd / YRPASSWD.PAS < prev    next >
Pascal/Delphi Source File  |  1995-11-28  |  12KB  |  263 lines

  1. unit YRPasswd;
  2. {------------------------------------------------------------------------------}
  3. { Copyright             : ⌐ Copyright 1995 by Y. Rochat                        }
  4. { Creation Date         : 25/10/1995                                           }
  5. { Last Update           : 28/11/1995                                           }
  6. { Version Number        : 1.60                                                 }
  7. { Programation Language : DELPHI 1.01                                          }
  8. {------------------------------------------------------------------------------}
  9. { PASSWORD.DLL    V. 1.60 :  A Windows DLL that let you manage user's name and   }
  10. {                          password in order to protect your programs.  This   }
  11. {                          DLL, which is FREEWARE ( ⌐ Copyright 1995 by Yves   }
  12. {                          Rochat ), implements one function :                 }
  13. {                                                                              }
  14. {            MotDePasse(var UserN : String ; var PriorityL : Integer ;            }
  15. {                                   IniFileName,SectionName : String) : WordBool ;             }
  16. {                                                                              }
  17. {  IniFileName : The name of the .INI file which keeps the list of users names }
  18. {                and their associated encrypted passwords. If you don't add a  }
  19. {                path to this name, we suppose that it is in the same directory}
  20. {                as the file PASSWORD.DLL                                      }
  21. {  SectionName : The name (without brakets) of the section in IniFileName which}
  22. {                contains the information about users and passwords            }
  23. {                                                                              }
  24. { If you succeed to login in the program, UserN and PriorityL will contain     }
  25. { respectively the name of the user who logs in and it's associated priority   }
  26. { level. Priority is a number which can varies between 1 (lowest priority) to  }
  27. { 9 (highest priority).                                                        }
  28. {                                                                              }
  29. {                Author :  Yves ROCHAT                                         }
  30. {                 Email :  rochat@dma.epfl.ch                                  }
  31. {               Address :  Ch. de la ClergΦre 36                               }
  32. {                          CH-1009 PULLY (Vaud)                                }
  33. {                          SWITZERLAND                                         }
  34. {------------------------------------------------------------------------------}
  35. { TYRPasswd Version 1.6 :  A Freeware component that let you use PASSWORD.DLL  }
  36. {                          You MUST install it in your component palette before}
  37. {                          using it. It will be on the palette named 'YRPasswd'}
  38. {                                                                              }
  39. { HOW TO USE IT : 1) Put a TYRPasswd (by ex. MyPswd) in you Delphi form        }
  40. {                 2) Do the following in the procedure FormCreate :            }
  41. {                                                                              }
  42. { procedure TForm1.FormCreate(Sender: TObject);                                }
  43. { begin                                                                        }
  44. {   If not Mypswd.GetPassword    then                                             }
  45. {   Begin                                                                      }
  46. {       If not Mypswd.ExistDll                                                    }
  47. {       then MessageDlg('File PASSWORD.DLL is missing' + #13#10 + #13#10 +     }
  48. {                                  'You are not authorized to use this program !',        }
  49. {                        mtInformation,[mbOk], 0)                              }
  50. {       else MessageDlg('BAD PASSWORD....', mtInformation,[mbOk], 0);          }
  51. {     Application.Terminate ;                                                  }
  52. {   End else                                                                   }
  53. {   MessageDlg('GOOD PASSWORD....', mtInformation,[mbOk], 0);                  }
  54. { end;                                                                         }
  55. {                                                                              }
  56. { procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);       }
  57. { begin                                                                        }
  58. {   if MessageDlg('Close application ?', mtConfirmation,                       }
  59. {     [mbYes, mbNo], 0) = mrYes then                                           }
  60. {   begin                                                                      }
  61. {     Action := caFree ;                                                       }
  62. {     Mypswd.FreeDllPassword ; <---- TO FREE MEMORY ALLOCATED FOR PASSWORD.DLL }
  63. {   end                                                                        }
  64. {   else                                                                       }
  65. {     Action := caNone;                                                        }
  66. { end;                                                                         }
  67. {                                                                              }
  68. { Properties of the component                                                  }
  69. { ---------------------------                                                  }
  70. {                                                                              }
  71. {  ExistDll            : True if PASSWORD.DLL exists, false otherwise                 }
  72. {  LoadedDll        : True if PASSWORD.DLL is loaded, false otherwise              }
  73. {  OkLogin          : Set it to True to test the Login Dialog at design time       }
  74. {                 Set it to False to unload PASSWORD.DLL from memory           }
  75. {                 ALWAYS SET IT TO FALSE BEFORE CLOSING A PROJECT WHICH        }
  76. {                 CONTAINS A TYRPASSWD COMPONENT                               }
  77. {  UserName         : Name of the user who logs in                                 }
  78. {  UserLevel         : Priority level of the user who logs in                       }
  79. {  IniName             : Name of the .INI file which contains users information       }
  80. {  SectionName     : Name of the Section in the file IniName                      }
  81. {                                                                              }
  82. {------------------------------------------------------------------------------}
  83. {                               IMPORTANT                                      }
  84. {                                                                              }
  85. { You can use this code as is and you are FREE to copy/distribute it provided  }
  86. { that this notice is not modified and included in the distrubution pack.      }
  87. { I'll be more than happy to hear from you for your comments about a real-life }
  88. { use of this code. Please send your comments to " rochat@dma.epfl.ch " with   }
  89. { subject field starting with the words 'TYRPASSWD'.                           }
  90. {------------------------------------------------------------------------------}
  91. {:DISCLAIMER:                                                                  }
  92. {------------                                                                  }
  93. {  THIS SOURCE CODE IS DELIVERED AS IS. THERE IS NO REASON TO THINK THAT IT    }
  94. {  SHOULD NOT WORK AS CLAIMED. BUT JUST IN CASE, LET ME DISCLAIM THAT YVES     }
  95. {  ROCHAT CAN NOT BE HELD LIABLE IF YOU LOOSE TIME OR MONEY USING THIS CODE.   }
  96. {------------------------------------------------------------------------------}
  97. interface
  98.  
  99. uses
  100.   WinTypes, Messages, Classes, Graphics, Controls, Forms{, SysUtils} ;
  101.  
  102. const
  103.     DLLNAME    = 'PASSWORD.DLL' ;
  104.  
  105. var
  106.     Login: function(var UserN : String ; var PriorityL : Integer ;
  107.                                     IniFileName,SectionName : String) : WordBool ;
  108.  
  109. type
  110.   TYRPasswd = class(TComponent)
  111.       private
  112.         { Private field declarations }
  113.       FExistDll            : Boolean ;
  114.       FLoadedDll        : Boolean ;
  115.       FOkLogin            : Boolean ;
  116.       FUserName            : String ;
  117.       FUserLevel        : Integer ;
  118.       FIniName            :    String ;
  119.       FSectionName    : String ;
  120.       FHandleDll        :    Word ;
  121.       function    SetExistDll(AValue : Boolean) : Boolean ;
  122.       function    SetLoadedDll(AValue : Boolean) : Boolean ;
  123.       function    SetOkLogin(AValue : Boolean) : Boolean ;
  124.       function    SetUserName(AStr : String) : String ;
  125.       function    SetUserLevel(AInt : Integer) : Integer ;
  126.       protected
  127.         { Protected method declarations }
  128.       public
  129.         { Public interface declarations }
  130.         constructor Create(AOwner: TComponent); override;
  131.           function  GetPassword : Boolean;
  132.       procedure FreeDllPassword ;
  133.       published
  134.         { Published design declarations }
  135.         property ExistDll            : Boolean read FExistDll        write SetExistDll        default False ;
  136.       property LoadedDll        : Boolean read FLoadedDll     write SetLoadedDll    default False ;
  137.       property OkLogin          : Boolean read FOkLogin            write SetOkLogin        default False ;
  138.         property UserName         : String     read FUserName         write SetUserName ;
  139.         property UserLevel         : Integer    read FUserLevel     write SetUserLevel ;
  140.         property IniName             : String     read FIniName         write FIniName ;
  141.         property SectionName     : String     read FSectionName    write FSectionName ;
  142.   end;
  143.  
  144. procedure Register;
  145.  
  146. implementation
  147.  
  148. {$IFDEF WINDOWS}
  149. uses
  150.     WinProcs, Dialogs, SysUtils ;
  151. const
  152.     SEM_NoOpenFileErrorBox = $8000;
  153. {$ELSE}
  154. uses
  155.     WinAPI, Dialogs, SysUtils ;
  156. {$ENDIF}
  157.  
  158. procedure Register;
  159. begin
  160.   RegisterComponents('YRPasswd', [TYRPasswd]);
  161. end {Register} ;
  162.  
  163. constructor TYRPasswd.Create(AOwner: TComponent);
  164. begin
  165.   inherited Create(AOwner);
  166.   FExistDll            := False ;
  167.   FLoadedDll        := False ;
  168.   FOkLogin            := False ;
  169.   FUserName            := 'Unknown' ;
  170.   FUserLevel        := 0 ;
  171.   FIniName            := 'password.ini' ;
  172.   FSectionName    := 'Users' ;
  173.   FHandleDll        := 0 ;
  174.   SetExistDll(True) ;
  175. end {Create} ;
  176.  
  177. procedure TYRPasswd.FreeDllPassword ;
  178. begin
  179.   If FHandleDLL >= 32    { Password loaded } then
  180.   Begin
  181.     FreeLibrary(FHandleDLL) ;
  182.     FHandleDll    := 0 ;
  183.     FLoadedDll    := False ;
  184.     FUserName        := 'Unknown' ;
  185.     FUserLevel    := 0 ;
  186.   End ;
  187. end ;
  188.  
  189. function TYRPasswd.SetExistDll(AValue : Boolean) : Boolean ;
  190. begin
  191.   FExistDll    := FileExists(DLLNAME) ;
  192. end ;
  193.  
  194. function TYRPasswd.SetLoadedDll(AValue : Boolean) : Boolean ;
  195. begin
  196.   { Nothing to do }
  197. end ;
  198.  
  199. function TYRPasswd.SetOkLogin(AValue : Boolean) : Boolean ;
  200. begin
  201.   If AValue
  202.       then    GetPassword
  203.     else    Begin
  204.             FreeDllPassword ;
  205.                     FOkLogin    := False ;
  206.                 End ;
  207. end ;
  208.  
  209. function TYRPasswd.SetUserName(AStr : String) : String ;
  210. begin
  211.   { Nothing to do }
  212. end ;
  213.  
  214. function TYRPasswd.SetUserLevel(AInt : Integer) : Integer ;
  215. begin
  216.   { Nothing to do }
  217. end ;
  218.  
  219. function TYRPasswd.GetPassword : Boolean;
  220. begin
  221.   Result        := False ;
  222.   FOkLogin    := False ;
  223.   If FHandleDll = 0 then
  224.   Begin
  225.       {$IFDEF WINDOWS}
  226.       SetErrorMode(SEM_NoOpenFileErrorBox);
  227.       {$ENDIF}
  228.       FHandleDll := LoadLibrary(DLLNAME);
  229.     Case FHandleDll of
  230.         2    :    Begin
  231.                   MessageDlg('File ' + DLLNAME + ' was not found', mtInformation,[mbOk], 0);
  232.             FHandleDll := 0 ;
  233.               End ;
  234.       3    : Begin
  235.                   MessageDlg('Path to ' + DLLNAME + ' was not found', mtInformation,[mbOk], 0);
  236.             FHandleDll := 0 ;
  237.               End ;
  238.       8    : Begin
  239.                   MessageDlg('Insufficient memory to start the application !', mtInformation,[mbOk], 0);
  240.             FHandleDll := 0 ;
  241.               End ;
  242.       else
  243.           If FHandleDll >= 32 then { Succes }
  244.               Begin
  245.                 FLoadedDll    := True;
  246.                 @Login            := GetProcAddress(FHandleDll,'MOTDEPASSE');
  247.             End else
  248.         Begin
  249.           MessageDlg(' Error when loading ' + DLLNAME + ' !', mtInformation,[mbOk], 0);
  250.           FHandleDll := 0 ;
  251.         End ;
  252.     End ;
  253.     SetExistDll(True) ;
  254.   End ;
  255.   If FLoadedDll and Login(FUserName,FUserLevel,FIniName,FSectionName)    then
  256.   Begin
  257.     Result        := True ;
  258.       FOkLogin    := True ;
  259.   End ;
  260. end {GetPassword} ;
  261.  
  262. end.
  263.