2017-08-22校验

Const ADDIN_NAME As String = "AddInFunction"
Const SETTING_ITEM As String = "AddInSetting"
Const KEY_NAME As String = "RegisterCpu"
Sub TestRegister()
    Debug.Print IsRegistered
End Sub
Public Function IsRegistered() As Boolean
    Dim CpuID As String
    Dim RegisterInfo As String
    Dim Rng As Range, RngInfo As String
    RegisterInfo = Get_Settings
    If RegisterInfo <> "" Then
        If HasRegisterSheet Then
            Set Rng = ThisWorkbook.Worksheets("Register").Range("IV65536")
            RngInfo = Rng.Text
            If Len(RngInfo) > 0 Then
                If RngInfo = RegisterInfo Then
                    '校验成功
                    IsRegistered = True
                Else
                    IsRegistered = False
                End If
            Else
                IsRegistered = False
            End If
        Else
            IsRegistered = False
        End If
    Else
        If HasRegisterSheet Then
            Set Rng = ThisWorkbook.Worksheets("Register").Range("IV65536")
            RngInfo = Rng.Text
            If Len(RngInfo) > 0 Then
                IsRegistered = False
            Else
                '首次注册,写入注册表
                CpuID = Get_CpuId
                Rng.Value = "'" & CpuID
                Save_Settings CpuID
                IsRegistered = True
            End If
        Else
            IsRegistered = False
        End If
    End If
    Set Rng = Nothing
End Function
Private Function Get_Settings() As String
    Get_Settings = GetSetting(ADDIN_NAME, SETTING_ITEM, KEY_NAME)
End Function
Private Function Save_Settings(ByVal RegInfo As String)
    SaveSetting ADDIN_NAME, SETTING_ITEM, KEY_NAME, RegInfo
End Function
Private Function Get_CpuId() As String
    Dim CpuSet
    Dim OneCpu
    Set CpuSet = GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("Win32_Processor")
    For Each OneCpu In CpuSet
        Get_CpuId = OneCpu.ProcessorId
    Next
End Function
Private Function HasRegisterSheet(Optional SheetName As String = "Register") As Boolean
    Dim HasRegSht As Boolean
    Dim Sht As Worksheet
    HasRegisterSheet = False
    For Each Sht In ThisWorkbook.Worksheets
        If Sht.Name = SheetName Then
            HasRegisterSheet = True
            Exit For
        End If
    Next Sht
End Function

  

posted @ 2017-08-22 14:36  wangway  阅读(112)  评论(0编辑  收藏  举报