Windows Product Key

Posted on 2006-04-07
Medium Priority
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
  • 3
  • 3
LVL 23

Accepted Solution

gecko_au2003 earned 1000 total points
ID: 16406054
LVL 29

Assisted Solution

nffvrxqgrcfqvvc earned 1000 total points
ID: 16406550
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
LVL 12

Author Comment

ID: 16414388
Thank you gents.  Both of you saved my life!

Again, thanks a lot!!!
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 12

Author Comment

ID: 16414596
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

ID: 16414629
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

ID: 16423705
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

ID: 16473213
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

850 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