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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shane Russell2nd Line Desktop SupportCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!!!
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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 :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.