1. 程式人生 > >2017-08-22校驗

2017-08-22校驗

() func evel end amp this sre sta key

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

  

2017-08-22校驗