GetWindowsProductKey

Share your advanced PureBasic knowledge/code with the community.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

GetWindowsProductKey

Post by ts-soft »

Code: Select all

; Original Code in XProfan by frank abbing
; http://www.paules-pc-forum.de/forum/dlls-includes-units-prozeduren/134802-windows-product-key-auslesen.html

; rewritten to work with purebasic by ts-soft

; Plattform: windows only
; Supports 32 and 64 bit OS
; Supports Ascii and Unicode
; Requires PureBasic 4.40 and higher

EnableExplicit

#KEY_WOW64_64KEY = $100

Procedure.s GetWindowsProductKey()
  Protected hKey, Res, size = 280
  Protected i, j, x, Result.s
  Protected *mem = AllocateMemory(size)
  Protected *newmem = AllocateMemory(size)
  Protected *digits = AllocateMemory(25)
  
  PokeS(*digits, "BCDFGHJKMPQRTVWXY2346789", -1, #PB_Ascii)
  If OSVersion() <= #PB_OS_Windows_2000
    Res = RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows NT\CurrentVersion", 0, #KEY_READ, @hKey)
  Else
    Res = RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows NT\CurrentVersion", 0, #KEY_READ | #KEY_WOW64_64KEY, @hKey)
  EndIf
  If Res = #ERROR_SUCCESS
    RegQueryValueEx_(hKey, "DigitalProductID", 0, 0, *mem, @size)
    RegCloseKey_(hKey)
    If size <> 280
      For i = 24 To 0 Step -1
        x = 0
        For j = 66 To 52 Step -1
          x = (x << 8) + PeekA(*mem + j)
          PokeA(*mem + j, x / 24)
          x % 24
        Next
        PokeA(*newmem + i, PeekA(*digits + x))
      Next
      For i = 0 To 15 Step 5
        Result + PeekS(*newmem + i, 5, #PB_Ascii) + "-"
      Next
      Result + PeekS(*newmem + 20, 5, #PB_Ascii)
    EndIf
  EndIf
  FreeMemory(*mem) : FreeMemory(*newmem) : FreeMemory(*digits)
  ProcedureReturn Result
EndProcedure

Debug GetWindowsProductKey()

greetings
Thomas
Last edited by ts-soft on Sat Dec 04, 2010 12:48 am, edited 1 time in total.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: GetWindowsProductKey

Post by rsts »

Nice.

Thanks for sharing.

cheers
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: GetWindowsProductKey

Post by netmaestro »

Works great on my Win7 Home Premium 32bit OS. Thanks for this.
BERESHEIT
eJan
Enthusiast
Enthusiast
Posts: 366
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Re: GetWindowsProductKey

Post by eJan »

Thanks Thomas.
Image
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Re: GetWindowsProductKey

Post by Seldon »

It worked fine here (Windows XP Home Edition). Very nice. Thanks !
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: GetWindowsProductKey

Post by Kwai chang caine »

Good on XP PRO SP3 ..thanks :wink:
ImageThe happiness is a road...
Not a destination
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: GetWindowsProductKey

Post by idle »

great thanks
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Alireza
Enthusiast
Enthusiast
Posts: 143
Joined: Sat Aug 16, 2008 2:02 pm
Location: Iran

Re: GetWindowsProductKey

Post by Alireza »

omg :D 7 64bit was OK :wink:
thanks
PB v 5.6 :D
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GetWindowsProductKey

Post by ts-soft »

I have made a small change in first thread to work with win 2000.

Greetings
Thomas
User avatar
HeX0R
Addict
Addict
Posts: 1205
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: GetWindowsProductKey

Post by HeX0R »

Hehehe, you are six years too late ;)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: GetWindowsProductKey

Post by ts-soft »

HeX0R wrote:Hehehe, you are six years too late ;)
No, your code is old, doesn't work on my 64-bit system :wink:
(haven't seen before)
Post Reply