Use the registry to add password _ registry to the application

Source: Internet
Author: User
Tags trim

Almost every application system has the appropriate rights management function. For large multi-user applications, the permissions settings are typically saved in the database. However, for small single-user applications and non database applications, it is not necessary to keep permission settings in the database, but it is neither convenient nor safe to save files because they are likely to be deleted or destroyed. In fact, the flexible use of the features of the Windows registry, you can easily set the user name and password for the application system, this method is not only simple programming, and high security. The example below shows that the routine is debugged in a windows98,delphi4 environment.

The design process is: first to determine whether the first use, if for the first time, you need to set a new user name and password, the system will save the user name and password, if not the first time, the user name and password required to log in.

In the Delphi development environment, create a new project Project1, add a form to the Project1 Form1; add two buttons on Form1, one for OK and another for cancel Add two text boxes Edituser and Editpass to the Form1, respectively, for user name and password input box. Defines the form-level global variable regf:tregistry. and add registry to the uses of the interface.

Add function Wsz_iffirst, judge whether it is the first time to use, add process Wsz_saveuse, save user name and password, add function wsz_checkuser, judge user name and password is correct. The three functions and processes are then called in the Form1 Formshow event and the determination of the New button event to meet the design requirements.

Judge whether it is the first time to use

function Wsz_iffirst:boolean;
Var
suser:string;
spass:string;
Begin
Result: = false;
Regf:=tregistry.create;
Regf.rootkey: =hkey_local_machine;
Regf.openkey ("software\microsoft\whh726", TRUE);
Suser: = regf.readstring ("user");
Spass: = regf.readstring ("Pass");
if (length (suser) <=0) and (Length (Spass) <=0)) Then
Result: = true;
Regf.free;
End

Determine if the user name and password are correct
function Wsz_checkuser (myuser:string; mypass:string): Boolean;
Var
suser:string;
spass:string;
Begin
Result:= false;
Regf:=tregistry.create;
Regf.rootkey: =hkey_local_machine;
Regf.openkey ("software\microsoft\whh726", TRUE);
Suser: = regf.readstring ("user");
Spass: = regf.readstring ("Pass");
if (Suser=myuser) and (Spass=mypass) then
Result: = true;
Regf.free;
End

Save user name and password
Procedure Wsz_saveuser (myuser:string; mypass:string);
Begin
Regf:=tregistry.create;
Regf.rootkey: =hkey_local_machine;
Regf.openkey ("software\microsoft\whh726", TRUE);
Regf.writestring ("User", MyUser);
Regf.writestring ("Pass", Mypass);
Regf.free;
End

Cancel the push button event
Procedure Twszloginform.bitbtncloseclick (Sender:tobject);
Begin
Close;
End

Determine the button event
Procedure Twszloginform.bitbtnyesclick (Sender:tobject);
Var
suser:string;
spass:string;
Begin
Suser: = Trim (Edituser.text);
Spass: = Trim (Editpass.text);
if (length (suser) <=0) or (Length (spass) <=0) Then
Begin
Application.messagebox ("username and password must be entered!") , "System prompts",
MB_OK + mb_iconinformation);
Edituser. SetFocus;
Exit
End
If Wsz_iffirst Then
Begin

Process user name and password for first logon
If Application.messagebox (
"You are using the software for the first time, please remember your username and password so that you can log in next time." Are you entering the system now? ",
"System Prompts",
Mb_okcancel + mb_iconquestion) = Idok Then
Begin

Save
Wsz_saveuser (Suser,spass);
End
Else
Begin
Edituser. SetFocus;
Exit
End
End Else
Begin
If not Wsz_checkuser (Suser,spass) Then
Begin
Application.messagebox ("Sorry, username and password entered incorrectly!") ",
"System Prompts",
MB_OK + mb_iconinformation);
Edituser.setfocus;
Exit
End
End

Global variables
Gb_wsz_user: = Suser;
Gb_wsz_pass: = Spass;
Close;
End

Show events for forms
Procedure Twszloginform.formshow (Sender:tobject);
Begin
If Wsz_iffirst Then
Begin
Application.messagebox (
"This is the first time you have used the software, you can enter the user name and password arbitrarily." ",
"System Prompts",
MB_OK + mb_iconinformation);
End
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.