To implement software registration function, first need to know a few issues to be involved in the registration mechanism: 1. How to join the registration test, determine if the software is registered; 2, how to generate a registration code, how to ensure that a user name is only generated Registration code;
First, there should be an algorithm that generates a registration code. The following is my simple algorithm that generates 15 registration code:
/ / This function returns a 15-bit registration code of a cString type, and the entrance parameter is the username.
CString GetRegpasswd (CString & Dirname)
{
// Reconize the user name into 15 registration code
Long Num1, Num2, Num3
CHAR SN [16] = {0};
CString P;
INT I, LEN;
Num1 = 0;
Num2 = 0;
Num3 = 0;
Len = int (Strlen));
IF (len! = 0)
{
For (i = 1; i <= le; i )
{
// First step algorithm
Num1 = (long (Num1 (INT (DiRName [i-1]) * i * i) * (i * sqrt (DiRName [i-1]) 1)% 100000;
// Second step algorithm
Num2 = (Num2 * i (long (POW ((int) DIRNAME [I-1], 2) * i)))))% 100000;
// third step algorithm
Num3 = (Num2 (long) SQRT (NUM1))% 100000;
}
// The following three characters are generated separately, 15 characters respectively.
For (i = 0; i <5; i )
SN [I] = (int) (NUM1 31 I * I * I)% 128;
For (i = 5; i <10; i )
SN [i] = (int) (NUM2 31 I * I * I)% 128;
FOR (i = 10; i <15; i )
SN [I] = (int) (Num3 31 i * i * i)% 128;
Sn [15] = 0;
// The following loop converts all generated characters to 0 --- 9, A --- Z, A ---- Z
FOR (i = 0; i <15; i )
{
While ((SN [I] <'0' || Sn [I]> '9') && (SN [i] <'a' || SN [I]> 'Z') && (SN [i] < 'A' || Sn [I]> 'Z')))))
{
SN [I] = (SN [I] 31 7 * I)% 128;
}
}
// assign a value to a CString variable, use the function return value
P.Format ("% s", SN);
}
Return P;
}
/ / Check the function registered if the software is registered
Bool getRegflag (void)
{
HKEY HKEY = NULL;
BYTE I;
CString Str;
Str.LoadString (IDS_REG_KEY); // ids_reg_key is a sub-directory string in the registry
IF (REGEY_CURRENT_USER, STR, & HKEY)! = Error_Success) Return False;
DWORD CBA;
CBA = SIZEOF (int);
IF (RegQueryValueex (HKEY, SZMIMA ", NULL, NULL, & I, & CBA)! = Error_Success) Return False;
BYTE J = I;
IF (j == 0) // 0 represents software has been registered, you can use it normally
{
RegcloseKey (HKEY);
Return True;
}
Else
{
RegcloseKey (HKEY);
Return False;
}
Return False;
}
/ / Set the function of the software has registered the logo
Bool SetRegflag (Void)
{
HKEY HKEY = NULL;
BYTE I;
CString Str;
Str.LoadString (IDS_REG_KEY); // ids_reg_key is a sub-directory string in the registry
IF (REGEY_CURRENT_USER, STR, & HKEY)! = Error_Success) Return False;
BYTE J = 0; // 0 representative has been registered
IF (RegSetValueex (HKey, "Szmima", 0, Reg_binary, & J, 4)! = Error_Success
{
AFXMessageBox ("Setting the registry data failed!");
Return False;
}
Return False;
}
The above three functions can realize the software registration mechanism, just add the following sentences when the program is initialized.
Bool breg = getRegflag ();
IF (! BREG)
{
/ / Add to limit function or reject the code used
}
If the user registration only needs to add the following code
// This is a registration interface in my program, enter the user name and the registration code submitting code to check if the registration code is correct
Void CRegeDit :: OnbnclicKedok ()
{
if (! Updatedata ()) Return; // Get the contents of the edit box, and assign the value to the class variable
m_reguser.trimleft (); // m_reguser is the username
m_reguser.trimright ();
IF (m_reguser.isempty ())
{
AFXMessageBox ("The username cannot be empty, please re-enter.");
Getdlgitem (IDC_REGUSER) -> setfocus ();
Return;
}
m_regpasswd.trimleft (); m_regpasswd is the registration code
m_regpasswd.trimright ();
IF (m_regpasswd.isempty ())
{
AfxMessageBox ("Registration Code cannot be empty, please re-enter.");
Getdlgitem (IDC_REGPASSWD) -> setfocus ();
Return;
}
Cstring passwd;
Passwd = getRegpasswd (m_reguser); // Call the algorithm to get the registration code of the username
IF (passwd == m_regpasswd) / / Compare with the registration input by the user
{
SetRegflag (); // Set registration mark
ONOK ();
}
Else
AFXMessageBox ("Registration code error, please re-enter.");
Updatedata (FALSE);
}