Windows Product Key

Posted on 2006-04-07
Last Modified: 2012-05-05
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.

Question by:jkaios
    LVL 23

    Accepted Solution

    LVL 29

    Assisted Solution

    Here is my base 24 example

    Option Explicit

    Private Type LARGE_INTEGER
        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
    LVL 12

    Author Comment

    Thank you gents.  Both of you saved my life!

    Again, thanks a lot!!!
    LVL 12

    Author Comment

    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.
    LVL 23

    Expert Comment

    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 ) ??
    LVL 12

    Author Comment

    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
    LVL 23

    Expert Comment

    egl1044's version is more elegant by the looks of it with its copymemory API / function or library or whatever it is etc :)

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Populating a combobox from a text file 8 78
    Help in WHSCRIPT 9 31
    Use Multiple Forms 4 30
    Put text in a picture ASP.NET C# 2 27
    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now