简体   繁体   English

无法在Windows 7中获取Windows序列号

[英]Failed to get Windows Serial Number in Windows 7

I am trying to get the Windows Serial Number in Windows 7 with my VB6 application. 我试图用我的VB6应用程序获取Windows 7中的Windows序列号。 However, it always fails to retrieve it. 但是,它始终无法检索它。

SScript.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductId")

It returns the following error: 它返回以下错误:

Unable to open registry key "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductId" for reading.

In Win XP, it will be successful to retrieve the serial number. 在Win XP中,它将成功检索序列号。 I don't know whether Windows 7 has prohibited a VB6 application to get the Windows serial number. 我不知道Windows 7是否禁止VB6应用程序获取Windows序列号。

Please help. 请帮忙。 Thank you! 谢谢!

It is peculiar isn't it? 这是奇特的不是吗? Try adding this const to your delcarations and OR the value to your open registry call. 尝试添加该常量到您的delcarations和OR值您打开注册表调用。 There is a very good explanation in the answer to KEY_WOW64_32KEY and KEY_WOW64_64KEY . KEY_WOW64_32KEY和KEY_WOW64_64KEY的答案中有一个非常好的解释。

Private Const KEY_WOW64_64KEY As Long = &H100& '32 bit app to access 64 bit hive

Private Function GetWindowsProductId() As String
    Dim strReturn As String
    Dim strBuffer As String
    Dim lngType As Long
    Dim lngBufLen As Long
    Dim lngRst As Long
    Dim hKeyHandle As Long

    lngRst = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion", 0, KEY_READ Or KEY_WOW64_64KEY, hKeyHandle)

    If hKeyHandle <> 0 Then
        strBuffer = String(255, vbNullChar)
        lngBufLen = Len(strBuffer)
        lngRst = RegQueryValueEx(hKeyHandle, "ProductId", ByVal 0&, lngType, ByVal strBuffer, lngBufLen)
        If lngRst = 0 Then
            If lngType = REG_SZ Then
                If lngBufLen > 0 Then
                    strReturn = Left$(strBuffer, lngBufLen - 1)
                Else
                    strReturn = "nothing was returned"
                End If
            Else
                strReturn = "there was an error"
            End If
        ElseIf lngRst = 2 Then     'the key does not exist
            strReturn = "the key was not found"
        Else  'if the return is non-zero there was an error
            strReturn = "There was an error " & CStr(lngRst) & " reading the key"
        End If
    End If

    GetWindowsProductId = strReturn

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM