?
Solved

Converting this to vbscript ?

Posted on 2005-04-09
20
Medium Priority
?
1,214 Views
Last Modified: 2013-11-13
ok here is the code in visual basic to attain the windows xp cd key :

'======================

'**************************************
'Windows API/Global Declarations for :Vi
'     ew Windows XP CD Key
'**************************************
Option Explicit

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long ' Note that if you declare the lpData parameter as String, you must pass it By Value.
   Private Const REG_BINARY = 3
   Private Const HKEY_LOCAL_MACHINE = &H80000002
   Private Const ERROR_SUCCESS = 0&
'**************************************
' Name: View Windows XP CD Key
' Description:Function: sGetXPCDKey() wi
'     ll return the CD Key for Windows XP in t
'     he format XXXXX-XXXXX-XXXXX-XXXXX-XXXXX.
'
' By: Snytax
'
'
' Inputs:Nothing.
'
' Returns:Your Windows XP CD Key.
'
'Assumes:None
'
'Side Effects:None
'This code is copyrighted and has limite
'     d warranties.
'Please see http://www.Planet-Source-Cod
'     e.com/xq/ASP/txtCodeId.57164/lngWId.1/qx
'     /vb/scripts/ShowCode.htm
'for details.
'**************************************

'sGetXPCDKey() -
'Returns the Windows XP CD Key if succes
'     sful.
'Returns nothing upon failure.

Public Function sGetXPCDKey() As String
   'Read the value of:
   'HKLM\SOFTWARE\MICROSOFT\Windows NT\Curr
   '     entVersion\DigitalProductId
   Dim bDigitalProductID() As Byte
   Dim bProductKey() As Byte
   Dim ilByte As Long
   Dim lDataLen As Long
   Dim hKey As Long
   'Open the registry key: HKLM\SOFTWARE\MI
   '     CROSOFT\Windows NT\CurrentVersion

   If RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\MICROSOFT\Windows NT\CurrentVersion", hKey) = ERROR_SUCCESS Then
       lDataLen = 164
       ReDim Preserve bDigitalProductID(lDataLen)
       'Read the value of DigitalProductID

       If RegQueryValueEx(hKey, "DigitalProductId", 0&, REG_BINARY, bDigitalProductID(0), lDataLen) = ERROR_SUCCESS Then
           'Get the Product Key, 15 bytes long, off
           '     set by 52 bytes
           ReDim Preserve bProductKey(14)

           For ilByte = 52 To 66
               bProductKey(ilByte - 52) = bDigitalProductID(ilByte)
           Next ilByte
       Else
           'ERROR: Could not read "DigitalProductID
           '     "
           sGetXPCDKey = ""
           Exit Function
       End If
   Else
       'ERROR: Could not open "HKLM\SOFTWARE\MI
       '     CROSOFT\Windows NT\CurrentVersion"
       sGetXPCDKey = ""
       Exit Function
   End If
   'Now we are going to 'base24' decode the
   '     Product Key
   Dim bKeyChars(0 To 24) As Byte
   'Possible characters in the CD Key:
   bKeyChars(0) = Asc("B")
   bKeyChars(1) = Asc("C")
   bKeyChars(2) = Asc("D")
   bKeyChars(3) = Asc("F")
   bKeyChars(4) = Asc("G")
   bKeyChars(5) = Asc("H")
   bKeyChars(6) = Asc("J")
   bKeyChars(7) = Asc("K")
   bKeyChars(8) = Asc("M")
   bKeyChars(9) = Asc("P")
   bKeyChars(10) = Asc("Q")
   bKeyChars(11) = Asc("R")
   bKeyChars(12) = Asc("T")
   bKeyChars(13) = Asc("V")
   bKeyChars(14) = Asc("W")
   bKeyChars(15) = Asc("X")
   bKeyChars(16) = Asc("Y")
   bKeyChars(17) = Asc("2")
   bKeyChars(18) = Asc("3")
   bKeyChars(19) = Asc("4")
   bKeyChars(20) = Asc("6")
   bKeyChars(21) = Asc("7")
   bKeyChars(22) = Asc("8")
   bKeyChars(23) = Asc("9")
   Dim nCur As Integer
   Dim sCDKey As String
   Dim ilKeyByte As Long
   Dim ilBit As Long

   For ilByte = 24 To 0 Step -1
       'Step through each character in the CD k
       '     ey
       nCur = 0

       For ilKeyByte = 14 To 0 Step -1
           'Step through each byte in the Product K
           '     ey
           nCur = nCur * 256 Xor bProductKey(ilKeyByte)
           bProductKey(ilKeyByte) = Int(nCur / 24)
           nCur = nCur Mod 24
       Next ilKeyByte
       sCDKey = Chr(bKeyChars(nCur)) & sCDKey
       If ilByte Mod 5 = 0 And ilByte <> 0 Then sCDKey = "-" & sCDKey
   Next ilByte
   sGetXPCDKey = sCDKey
End Function

'======================

What I would like is it converted into vbscript so that I can carry it around on a floppy disk with me or something to that extent, because it would mean no runtime files, no installation or any of that and it would be a lot more useful to me then actually having an exe program to attain the key.

Thanks for the time and help !
0
Comment
Question by:gecko_au2003
  • 9
  • 7
  • 2
  • +1
20 Comments
 
LVL 6

Expert Comment

by:Rafaelkl
ID: 13749796
what kind of help did you want?
wanna someone to write vbscript for you? or you just want to get help to fulfil your needs?
0
 
LVL 6

Expert Comment

by:Rafaelkl
ID: 13749959
Windows Script Host(cscript.exe/wscript.exe) support a WshShell object in script file, whose method  "RegRead/RegWrite/RegDelete" can access registry.
0
 
LVL 23

Author Comment

by:gecko_au2003
ID: 13750969
I want some help converting this to vbscript
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 13752270
I think that you would have a problem, since VBScript cannot make API calls.  You could make an ActiveX control that you could instantiate from VBScript.

Bob
0
 
LVL 23

Author Comment

by:gecko_au2003
ID: 13752404
You can use WSH to read the registry values because I found examples of how to do that I just get stuck on how to read them correctly into the variables and how I would set it up so that it decodes the digitalproductid.

Unless you are reffering to something else that I am missing ?

If making an active x control is the only way, would you be able to help me with that or is that for another question ?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13752420
I am not saying that it is the only way, but it is one way.  Do you have VB6 development environment at your disposal?

Bob
0
 
LVL 23

Author Comment

by:gecko_au2003
ID: 13752460
Not on this laptop but I do have a desktop with visual basic 6 developement enviroment at my disposal yes :)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13752508
Create a DLL project, and put that code into a class module.  Build the DLL.

In VBScript:

Dim CDKeyReader
Dim CDKey

CDKeyReader = CreateObject(<progID of DLL>)

CDKey = CDKeyReader.sGetXPCDKey()

Bob

0
 
LVL 23

Author Comment

by:gecko_au2003
ID: 13752823
Where do I get the progID of DLL ? Also once I have made that, then what do I do with that ?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13752905
A little about ActiveX and ProgID that might help:

What is a ProgID and How Do I Change It?
http://www.vbaccelerator.com/progid.htm#sechow

You might be able to put this on a floppy disk (BTW what is a floppy?  *GRIN*), and reference it without having to register the ActiveX DLL as long as you put everything all in one directory and that constructs are simple.

BTW, I use Flash drives to distribute changes, since everyone here has Windows XP, and I haven't used a floppy in years.

Bob
0
 
LVL 23

Author Comment

by:gecko_au2003
ID: 13753056
once the dll is made, what do I do with that ??
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13753078
You might be able to put this on a floppy disk and reference it without having to register the ActiveX DLL as long as you put it in the same folder as the VBScript file.  Try it.

Bob
0
 
LVL 23

Author Comment

by:gecko_au2003
ID: 13753244
Ive got the dll made and ive got that coding in a vbscript file, however Im not sure it will work because I have no clue on how to get or attain the ProgID, please can you explain step by step how I do that because that site didnt make a bit of sense to me
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13753420
A Visual Basic ProgID is a unique identifier which is constructed through the combination of the DLL's project and object names (ProjectName.ObjectName).

I don't use VB6 anymore (I used it for about 8 years), and COM stuff is slowly slipping from my grasp, so forgive me if this takes longer than usual to answer.

Bob

0
 
LVL 23

Author Comment

by:gecko_au2003
ID: 13756057
I have never done any creating of dll's so in turn have no idea how to find out or attain the ProgID for the piece of coding within the vbscript, I just need to know what id to insert in that coding so that I can get it to work.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13756078
If you had a scenario like this:

Project name = NetworkFunctions
Class name = CDKeyReader

Then, then ProgID should be NetworkFunctions.CDKeyReader.  Also, the CDKeyReader class attribute would need to be marked as Multi-Use.

Bob
0
 
LVL 23

Author Comment

by:gecko_au2003
ID: 13796706
ok that does not help much because I tried doing that and its just not working, any chance you could try and convert it, I mean even if you converted half of it and I will post a new question with the other half of the script to be converted or something !! ?? !!
0
 
LVL 15

Accepted Solution

by:
Colosseo earned 2000 total points
ID: 13796822
Hi

here is the converted script which works on my machine returning my cd key

You can just use msgbox sGetXPCDKey to display the key or return the key to a variable

Public Function sGetXPCDKey()

    Dim bDigitalProductID
    Dim bProductKey()
    Dim bKeyChars(24)
    Dim ilByte
    Dim nCur
    Dim sCDKey
    Dim ilKeyByte
    Dim ilBit
       
    ReDim Preserve bProductKey(14)
   
    Set objShell = CreateObject("WScript.Shell")
   
    bDigitalProductID = objShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\Windows NT\CurrentVersion\DigitalProductId")

    Set objShell = Nothing

    For ilByte = 52 To 66
      bProductKey(ilByte - 52) = bDigitalProductID(ilByte)
    Next
 
    'Possible characters in the CD Key:
    bKeyChars(0) = Asc("B")
    bKeyChars(1) = Asc("C")
    bKeyChars(2) = Asc("D")
    bKeyChars(3) = Asc("F")
    bKeyChars(4) = Asc("G")
    bKeyChars(5) = Asc("H")
    bKeyChars(6) = Asc("J")
    bKeyChars(7) = Asc("K")
    bKeyChars(8) = Asc("M")
    bKeyChars(9) = Asc("P")
    bKeyChars(10) = Asc("Q")
    bKeyChars(11) = Asc("R")
    bKeyChars(12) = Asc("T")
    bKeyChars(13) = Asc("V")
    bKeyChars(14) = Asc("W")
    bKeyChars(15) = Asc("X")
    bKeyChars(16) = Asc("Y")
    bKeyChars(17) = Asc("2")
    bKeyChars(18) = Asc("3")
    bKeyChars(19) = Asc("4")
    bKeyChars(20) = Asc("6")
    bKeyChars(21) = Asc("7")
    bKeyChars(22) = Asc("8")
    bKeyChars(23) = Asc("9")

    For ilByte = 24 To 0 Step -1
     
      nCur = 0

      For ilKeyByte = 14 To 0 Step -1
        'Step through each byte in the Product Key
        nCur = nCur * 256 Xor bProductKey(ilKeyByte)
        bProductKey(ilKeyByte) = Int(nCur / 24)
        nCur = nCur Mod 24
      Next
     
      sCDKey = Chr(bKeyChars(nCur)) & sCDKey
      If ilByte Mod 5 = 0 And ilByte <> 0 Then sCDKey = "-" & sCDKey
    Next
   
    sGetXPCDKey = sCDKey
   
End Function

Cheers

Scott
0
 
LVL 23

Author Comment

by:gecko_au2003
ID: 13797176
Thanks scott, that is exactly what I wanted, this way I dont have to create any exe's or install runtime files or nothing :)

Spot on with that conversion scott :) I was trying to make the msgbox work within the function and it didnt work then I put the msgbox outside of the function and it works a treat !!

Thanks so much !!
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 13797351
No bother and thanks for the grade

Cheers

Scott
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

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