您的批评和鼓励都是我把编程无限办好的动力! 您编程时遇到困难,或遇到不顺心的事想发发牢骚尽管到这里来吧! 虽然本网力求全面,但也不能包罗万象,这些我筛选出的优秀网站连接就是对本站最好的补充! 学习编程当然也离不开书本了,这里收集大量编程书籍! 编程无限之源码超市,这里收集的代码令你意想不到的全面! 欢迎光临编程网校,这里专门收集VB/CB入门文章及技术文章! 欢迎光临编程无限!
     
       
 
当前位置:英文资料 >> Other
Creating Registration Number
  资料类型: Other 上传时间: 2001-02-21 阅读次数: 1268



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.

推荐给朋友 点 评( 0 ) 返回前页 关闭此页
   
  本类最热文章排名:
  1.Changing The Control Box Menu
2.Force SubMenus To St...
3.Prompt to enter a ne...
4.Defining An Array Property
5.Creating Cascading M...
6.Do the passwords math to save
7.This module contains...
8.Registry Functions
9.This class provides ...
10.Reading Writing any Registry Key
   
   
  评论:
 
 
 

 

关于本站 版权声明 联系方法
编程无限 V4.1 Copyright © 1999-2009 21code.com

京ICP备05006938号