⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 register.bas

📁 通过VB, GPIB协议控制测试仪器, 实现测试自动化, 这个行业的人才有些缺乏, 本人愿意分享
💻 BAS
字号:
Attribute VB_Name = "Register"
Option Explicit

'functions prototypes, constants, and type definitions
'for windows 32-bit Registery API

Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const ERROR_SUCCESS = 0&


'Registery API prototypes

Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String) As Long
Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal Hkey As Long, ByVal lpValueName As String) As Long
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegQueryValue Lib "advapi32.dll" Alias "RegQueryValueA" (ByVal Hkey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal Hkey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long         ' Note that if you declare the lpData parameter as String, you must pass it By Value.
Public Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal Hkey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal Hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long         ' Note that if you declare the lpData parameter as String, you must pass it By Value.

'Unicode nul terminated string
Public Const REG_SZ = 1

'32-bit number
Public Const REG_DWORD = 4


Public Sub SaveKey(Hkey As Long, strPath As String)
Dim keyhand&
Dim r As Long
r = RegCreateKey(Hkey, strPath, keyhand&)
r = RegCloseKey(keyhand&)
End Sub

Public Function GetString(Hkey As Long, strPath As String, strValue As String)
Dim keyhand As Long
Dim lValueType As Long
Dim datatype As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
Dim r As Long

r = RegOpenKey(Hkey, strPath, keyhand)
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)

If lValueType = REG_SZ Then
    strBuf = String(lDataBufSize, " ")
    lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
    If lResult = ERROR_SUCCESS Then
        intZeroPos = InStr(strBuf, Chr$(0))
        If intZeroPos > 0 Then
            GetString = Left$(strBuf, intZeroPos - 1)
        Else
            GetString = strBuf
        End If
        
    End If
End If
    

End Function

Public Sub SaveString(Hkey As Long, strPath As String, strValue As String, strData As String)
Dim keyhand As Long
Dim r As Long

r = RegCreateKey(Hkey, strPath, keyhand)
r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strData, Len(strData))
r = RegCloseKey(keyhand)
End Sub

Public Function GetDword(ByVal Hkey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
Dim lResult As Long
Dim lValueType As Long
Dim lBuf As Long
Dim lDataBufSize As Long
Dim r As Long
Dim keyhand As Long

r = RegOpenKey(Hkey, strPath, keyhand)

'Get length/data type
lDataBufSize = 4

lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)

If lResult = ERROR_SUCCESS Then
    If lValueType = REG_DWORD Then
        GetDword = lBuf
    End If
'Else
'   Call errlog("GetDWord-" & strPath, False)
End If

r = RegCloseKey(keyhand)

End Function

Public Sub SaveDword(ByVal Hkey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long)
Dim lResult As Long
Dim keyhand As Long
Dim r As Long

r = RegCreateKey(Hkey, strPath, keyhand)
lResult = RegSetValueEx(keyhand, strValueName, 0&, REG_DWORD, lData, 4)
If lResult <> ERROR_SUCCESS Then
    'Call errlog("SetDWORD", False)
End If

r = RegCloseKey(keyhand)
End Sub

Public Sub DeleteKey(ByVal Hkey As Long, ByVal strKey As String)
Dim r As Long
    r = RegDeleteKey(Hkey, strKey)
End Sub

Public Sub DeleteValue(ByVal Hkey As Long, ByVal strPath As String, ByVal strValue As String)
Dim r As Long
Dim keyhand As Long

r = RegOpenKey(Hkey, strPath, keyhand)
r = RegDeleteValue(keyhand, strValue)
r = RegCloseKey(keyhand)

End Sub

Public Function CheckPassword() As Boolean
Dim strPassword As String
Dim strCPUID As String
Dim lCPUID As Long
Dim strRegSTRING As String
Dim strRegDWORD As String
Dim strDate As String
Dim lDateDiff As Long
Dim iReg As Integer
Dim PasswordFileName As String
Dim sFilePassword As String

CheckPassword = False
On Error GoTo Err

strRegSTRING = ""
strCPUID = GetCPUID
strRegSTRING = GetRegKeyString
strRegDWORD = GetRegKeyDWORD

If strRegSTRING = "" Then
    MsgBox "No Permission, Contact Leo Peng by 13632913628"
    End
End If

lCPUID = CalSum(strCPUID)
lCPUID = lCPUID * CLng(strRegDWORD)
strCPUID = Str(lCPUID)
strCPUID = Right(strCPUID, 5)

PasswordFileName = App.Path & "\Password.pal"

strDate = GetFileDate(PasswordFileName)
lDateDiff = DateDiff("d", DateValue("October 10,1998"), strDate)
lDateDiff = lDateDiff * 98
strDate = Str(lDateDiff)
strDate = Right(strDate, 6)

strPassword = strCPUID & strDate
strPassword = strPassword & Right(Str(CalSum(strPassword)), 1)
'strPassword = ConvertString(strPassword)


Open PasswordFileName For Input As #2
Line Input #2, sFilePassword
Close #2

sFilePassword = UnConvertString(sFilePassword)

If InStr(strPassword, sFilePassword) = 1 Then
    CheckPassword = True
Else
    CheckPassword = False
End If

Err:
End Function


Public Function GetCPUID() As String
Dim strString As String

strString = GetString(HKEY_LOCAL_MACHINE, "HARDWARE\DESCRIPTION\System\CentralProcessor\0", "Identifier")
GetCPUID = strString
End Function

Public Function GetRegKeyString() As String
Dim strString As String

strString = GetString(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Telephony", "Compatiable")
'strString = GetString(HKEY_LOCAL_MACHINE, "HARDWARE\DESCRIPTION\System\CentralProcessor\0", "Compatiable")
GetRegKeyString = strString
End Function

Public Function GetRegKeyDWORD() As String
Dim lngDword As String
'Get a String out the Registry
lngDword = GetDword(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Telephony", "Code")
'lngDword = GetDword(HKEY_LOCAL_MACHINE, "HARDWARE\DESCRIPTION\System\CentralProcessor\0", "Code")
GetRegKeyDWORD = lngDword
End Function

Public Function GetFileDate(ByVal strFileName As String) As String
Dim fso As New FileSystemObject, fil As File
Dim sTemp As String
Dim strDate As String

On Error GoTo Err
Set fil = fso.GetFile(strFileName)

'MsgBox fil.DateCreated

'strDate = DateValue(fil.DateCreated)
strDate = DateValue(fil.DateLastModified)

GetFileDate = strDate

'GetFileDate = DateDiff("d", DateValue("October 10,1998"), strDate)
Err:
End Function

Public Function ConvertString(ByVal strInput As String) As String
Dim i As Integer
Dim sChar As String
Dim iChar As Integer
Dim strConvert As String

strConvert = ""

For i = 1 To Len(strInput)
    sChar = Mid(strInput, i, 1)
    iChar = Asc(sChar)
    iChar = iChar Xor &H5A
    iChar = iChar - 8
    strConvert = strConvert & Chr(iChar)
Next

ConvertString = strConvert

End Function

Public Function UnConvertString(ByVal strInput As String) As String
Dim i As Integer
Dim sChar As String
Dim iChar As Integer
Dim strConvert As String

strConvert = ""

For i = 1 To Len(strInput)
    sChar = Mid(strInput, i, 1)
    iChar = Asc(sChar)
    iChar = iChar + 8
    iChar = iChar Xor &H5A
    strConvert = strConvert & Chr(iChar)
Next

UnConvertString = strConvert

End Function

Public Function CalSum(ByVal strInput As String) As Long
Dim i As Integer
Dim sChar As String
Dim iChar As Integer
Dim lSum As Long

lSum = 0

For i = 1 To Len(strInput)
    sChar = Mid(strInput, i, 1)
    iChar = Asc(sChar)
    lSum = lSum + iChar
Next

CalSum = lSum

End Function

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -