The following code I use with Visual Basic Professional 3.0. This procedure reads a INI file and looks for two entries, 'UserName' and 'UserNumber'. If they are existing UserName will be recalculated by the same routine it was created and compared to UserNumber. If they are right, a program may goes on. In case of wrong or absent entries the routine will detect this and the program may show a reminder screen or anything else. I use this routine in my shareware programs. The user may register by entering his name and a number that must be created by the author of the program. For this purposes I wrote an own routine (in a separate program!) that uses the same procedure as the controlling code in the main program:
Code in the load procedure of the start form. First read the INI file and look for the entries of the variables UserName and UserNumber:
Dim LpRetStr As String
LpRetStr = String(255, Chr(0))
UserName = Left(LpRetStr, GetPrivateProfileString(APP_NAME, ByVal USER_NAME, "", LpRetStr, Len(LpRetStr), INI_FILENAME))
UserNumber = GetPrivateProfileInt(APP_NAME, USER_NUMBER, 0, INI_FILENAME)
Second after getting the UserName from the INI file read it char to char and creates a number from it. This will be done and stored in the variable TestCode. You may change the code of this line. Be sure to use the same procedure when you are creating the numbers:
For i% = 1 To Len(UserName)
Zeichen = Mid$(UserName, i%, 1)
pchar% = Asc(Zeichen)
TestCode =(TestCode1 + pchar%) + 2 * ( pchar% + 7) + 343
Next i%
Now compare the built number to number read from the INI file:
If Format$(TestCode) = Format$(UserNumber) Then
'Calculated and read number are identically
'Enter here the code to go on with the program
Else
'Numbers are not identically!
'Enter here the code to show the Copyright, Shareware reminder or do anything else
End If
Of course you must declare in the BAS module:
Declare Function GetPrivateProfileString Lib "Kernel" (ByVal Appname As String, KeyName As Any, ByVal lpDefault As String, ByVal ReturnedString As String, ByVal nSize As Integer, ByVal FileName As String) As Integer
Declare Function GetPrivateProfileInt Lib "Kernel" (ByVal Appname As String, ByVal KeyName As String, ByVal DEFAULT1 As Integer, ByVal Filename As String) As Integer
The following variables do not need to be declared as 'Global'. This is necessary only if you need them in different modules and forms. INI_FILENAME is the name for the INI file in the Windows directory, APP_NAME the name for the 'Section' of the INI file. Code APP_NAME and INI_FILENAME as listed below. The section name must appear inside the INI file in [brackets].
Global Const INI_FILENAME = "SAMPLE.INI"
Global Const APP_NAME = "SectionName"
Global Const USER_NAME = "UserName"
Global Const USER_NUMBER = "UserNumber"
Global UserName As String
Global UserNumber As Integer
If you want to able the user to register the program you must declare a function to write the entries into the INI file. The declaration must be done in the BAS module:
Declare Function WritePrivateProfileString Lib "Kernel" (ByVal Appname As String, ByVal KeyName As String, ByVal NewString As String, ByVal Filename As String) As Integer
The code to write the entries for UserName and UserNumber is:
Z = WritePrivateProfileString(APP_NAME, USER_NAME, Format$(Text1.Text), INI_FILENAME)
Z = WritePrivateProfileString(APP_NAME, USER_NUMBER, Format$(Text2.Text), INI_FILENAME)
These two lines above creates the entries in the INI file and also writes the brackets for you. These lines can be positioned at any place of your program. The user must enter the name and number he received from me in two text boxes called Text1 and Text2. After controlling this entries (with the routine above) they will be written to the INI file if they are correct. At least you must have the routine (in a separate program!) to build the UserNumber. Please notify that line 5 (TestCode) is identically with the corresponding lines in the test routine of the main program. This MUST be.
Sub Command1_Click ()
For i% = 1 To Len(Text1.Text)
Zeichen = Mid$(Text1.Text, i%, 1)
pchar% = Asc(Zeichen)
TestCode = (TestCode + pchar%) + 2 * ( pchar% + 7) + 343
Next i%
Text2.Text = TestCode
End Sub
Put the users name in the text field (Text1.Text) and press the button Command1. The built number will be shown in a second text box (Text2). Name and number should be send to the user who wants to register. enjoy and modify the code as you want.
|