Page 1 of 1

GetWindowsProductKey

Posted: Sun Nov 21, 2010 3:45 pm
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

Re: GetWindowsProductKey

Posted: Sun Nov 21, 2010 5:16 pm
by rsts
Nice.

Thanks for sharing.

cheers

Re: GetWindowsProductKey

Posted: Sun Nov 21, 2010 9:14 pm
by netmaestro
Works great on my Win7 Home Premium 32bit OS. Thanks for this.

Re: GetWindowsProductKey

Posted: Sun Nov 21, 2010 10:41 pm
by eJan
Thanks Thomas.

Re: GetWindowsProductKey

Posted: Mon Nov 22, 2010 1:33 am
by Seldon
It worked fine here (Windows XP Home Edition). Very nice. Thanks !

Re: GetWindowsProductKey

Posted: Mon Nov 22, 2010 8:51 am
by Kwai chang caine
Good on XP PRO SP3 ..thanks :wink:

Re: GetWindowsProductKey

Posted: Tue Nov 23, 2010 6:16 am
by idle
great thanks

Re: GetWindowsProductKey

Posted: Thu Nov 25, 2010 6:14 am
by Alireza
omg :D 7 64bit was OK :wink:
thanks

Re: GetWindowsProductKey

Posted: Sat Dec 04, 2010 12:50 am
by ts-soft
I have made a small change in first thread to work with win 2000.

Greetings
Thomas

Re: GetWindowsProductKey

Posted: Sat Dec 04, 2010 11:44 pm
by HeX0R
Hehehe, you are six years too late ;)

Re: GetWindowsProductKey

Posted: Sat Dec 04, 2010 11:50 pm
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)