Windows Product Key

Is there anybody here can help me how to get/retrieve the "Windows Product Key" in code?

FYI: There is a freeware called "Magical Jelly Bean Keyfinder" that shows the Windows Product Key as well as the product key for Office.  I would like to do that using Visual Basic 6.

LVL 12
jkaiosIT DirectorAsked:
Who is Participating?
Shane Russell2nd Line Desktop SupportCommented:
Here is my base 24 example

Option Explicit

    Long1 As Long
    Long2 As Long
End Type

Private Declare Function ShiftLeft Lib "ntdll" Alias "RtlLargeIntegerShiftLeft" ( _
    ByVal Val1 As Long, _
    ByVal Val2 As Long, _
    ByVal ShiftCount As Long) As LARGE_INTEGER

Private Declare Function RegOpenKeyExW Lib "advapi32.dll" ( _
    ByVal hKey As Long, _
    ByVal lpSubKey As Long, _
    ByVal ulOptions As Long, _
    ByVal samDesired As Long, _
    phkResult As Long) As Long

Private Declare Function RegQueryValueExW Lib "advapi32.dll" ( _
    ByVal hKey As Long, _
    ByVal lpValueName As Long, _
    ByVal lpReserved As Long, _
    lpType As Long, _
    ByVal lpData As Long, _
    lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" ( _
    ByVal hKey As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    lpDest As Any, _
    lpSource As Any, _
    ByVal cbCopy As Long)

Private Const QUERY     As Long = &H1
Private Const MACHINE   As Long = &H80000002
Private Const SUCCESS   As Long = 0
Private Const Low       As Long = 0
Private Const cKey      As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
Private Const cDigi     As String = "DigitalProductId"
Private Const cChrs     As String = "B C D F G H J K M P Q R T V W X Y 2 3 4 6 7 8 9"

    Dim bArr(&HA8)   As Byte
    Dim bKey(14)     As Byte
    Dim sKey(24)     As String
    Dim sChr()       As String
Public Function BinKey() As String

    Dim k As Long
    Dim z As Long
    Dim i As Long
    Dim j As Long
    Dim a As Long
    If RegOpenKeyExW(MACHINE, StrPtr(cKey), 0&, QUERY, k) = SUCCESS Then
        z = &HA8
        If RegQueryValueExW(k, StrPtr(cDigi), 0&, 3, _
            ByVal VarPtr(bArr(0)), z) = SUCCESS Then
            Call CopyMemory(bKey(0), bArr(52), 15)
        End If
    End If
    RegCloseKey k
    sChr() = Split(cChrs, Space$(1))
    For i = UBound(sKey) To Low Step -1
     a = Low
        For j = UBound(bKey) To Low Step -1
           a = ShiftLeft(a, 0, 8).Long1 + bKey(j)
            bKey(j) = (a \ 24)
            a = a Mod 24
        Next j
    sKey(i) = sChr(a)
    Next i
    For i = 0 To 24
        BinKey = BinKey & sKey(i)
    Next i

End Function

Private Sub Command1_Click()
    MsgBox BinKey
End Sub
jkaiosIT DirectorAuthor Commented:
Thank you gents.  Both of you saved my life!

Again, thanks a lot!!!
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

jkaiosIT DirectorAuthor Commented:
Hello experts,

Sorry, I forgot just one more additional question to this:  How can I accomplish this technique on a "remote" machine?

Thanks again for your help.
Shane Russell2nd Line Desktop SupportCommented:
why would you want to do this from a remote machine ( Sounds a bit dodgy like you are trying to get other peoples keys that dont belong to you or something ) ??
jkaiosIT DirectorAuthor Commented:
Well, I have just created a "Network Auditing Tool" that scans all computers on my network for auditing purposes. The data that my program collects are: MachineName, Description, IPAddress, OS,SP, DateInstalled, DomainRole, CurrentUser, SystemModel, BIOSInfo, Processor, Memory, Printers, NIC, Video, SCSI, etc. using WMI.

The only thing I was looking for was how to get the "CD-key" so that I can maintain a list of what XP Product Keys I have used and what I have NOT used.

Anyways, thanks for your help. I found how to do it using the "RegConnectRegistry" API
Shane Russell2nd Line Desktop SupportCommented:
egl1044's version is more elegant by the looks of it with its copymemory API / function or library or whatever it is etc :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.